feat: implement typed error taxonomy and custom error classes (#18)#38
Conversation
|
@TheDEV111 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
The error hierarchy ( git fetch origin
git rebase origin/develop
git push --force-with-leaseKeep |
|
The error hierarchy + typed-throw migration sweep + tests are the right shape for issue #18. Conflicts in 4 files, all overlap with #14's JSDoc additions:
git fetch origin
git rebase origin/develop
git push --force-with-leaseFor each conflict: keep develop's JSDoc blocks intact and graft your typed-throw migrations on top (replacing Thanks @TheDEV111. |
|
@truthixify fixed conflict, pls review |
|
Clean rebase, test merge into develop is clean. Error hierarchy, typed-throw sweep across per-chain modules, and tests all land. Merging. Thanks @TheDEV111. |
closes #18
📝 Overview & Summary
Currently, the SDK throws plain, generic
Errorinstances throughout its modules. This forces consumers to use brittle string-matching patterns intry/catchblocks to programmatically distinguish between specific error conditions (e.g. invalid meta-addresses, RPC failures, or view-tag mismatches).This PR resolves Issue #18 by implementing a granular, typed hierarchy of custom error classes under
src/errors.ts(re-exported from the SDK root entry point). These custom classes extend the native JavaScriptErrorclass (guaranteeing runtime backwards-compatibility) while carrying enumerable, structured metadata payloads, stable error codes, and dynamic documentation links.🛠️ Detailed Scope of Changes
1. The Error Hierarchy (
src/errors.ts)We have established a robust abstract taxonomy dividing errors into five primary functional domains:
2. Properties & Serialization Support
Every subclass instances includes:
name: Automatically matches the class name.code: A stable serializable constant string (e.g."WRAITH/INPUT/INVALID_SIGNATURE") to retain class identity across network boundaries.context: A structured object containing domain-specific parameters (e.g.statusCode,expectedLength,actualLength).docsLink: A direct link referencing the specific error details in the documentation guide.toJSON()onWraithErrorto ensure custom properties serialize cleanly withJSON.stringify(error).3. Codebase-wide Refactoring
throw new Error(...)calls with their typed equivalents.@throwscomments across public functions to document specific error behavior.4. Testing & QA
test/errors.test.ts): Added 17 unit tests verifyinginstanceofcorrectness (including ancestor matching likeWraithInputError), name/code/docsLink generation, property preservation, and serialization.InvalidSignatureErrorexceptions.5. Documentation
docs/errors.md): Wrote a complete, premium-quality developer guide containing the class tree, property mappings, stable codes reference table, and richtry/catchintegration examples.CHANGELOG.md): Noted the change in a new changelog file, warning developers about message string formatting adjustments.Errorinstances remain unaffected because all custom classes inherit fromError.error.message(e.g..toThrow('Expected 65-byte signature')) will break. Integrators must update their catch statements to use moderninstanceofchecking:✅ Verification & Testing Done
pnpm test, verifying that all 151 unit and E2E tests successfully pass.ESM dist/index.js 5.10 KB CJS dist/index.cjs 7.89 KB DTS dist/index.d.ts 8.62 KB ⚡️ Build success in 582ms