feat: propagate structured error classes across payment module#267
Open
mehmetkr-31 wants to merge 1 commit intobase:masterfrom
Open
feat: propagate structured error classes across payment module#267mehmetkr-31 wants to merge 1 commit intobase:masterfrom
mehmetkr-31 wants to merge 1 commit intobase:masterfrom
Conversation
Collaborator
🟡 Heimdall Review Status
|
Introduce ValidationError and PaymentError classes and apply them
consistently across the entire payment module, replacing raw
throw new Error() calls with structured, actionable errors.
Changes:
- Add sdkErrors.ts with ValidationError and PaymentError classes
- Replace 18 raw Error throws across 10 payment files:
- validation.ts: ValidationError with field metadata
- sendUserOpAndWait.ts: PaymentError with error codes
- validateUSDCBasePermission.ts: ValidationError for chain/token
- getPaymentStatus.ts: PaymentError for RPC and transfer errors
- getExistingSmartWalletOrThrow.ts: PaymentError for wallet lookup
- sdkManager.ts: PaymentError for response format issues
- subscribe.ts: ValidationError + PaymentError
- createCdpClientOrThrow.ts: PaymentError for CDP init
- prepareCharge.ts / prepareRevoke.ts: PaymentError
- getSubscriptionStatus.ts: reuse validateUSDCBasePermission()
- getOrCreateSubscriptionOwnerWallet.ts: PaymentError
- Export both error classes from public API entry points
- Refactor getSubscriptionStatus to reuse validateUSDCBasePermission
instead of duplicating validation logic inline
- Update all affected tests to match new error messages
Developers can now use instanceof checks for programmatic error
handling:
import { PaymentError, ValidationError } from '@base-org/account';
try { await pay(...) } catch (e) {
if (e instanceof ValidationError) { /* fix input */ }
if (e instanceof PaymentError) { /* retry or report */ }
}
Addresses base#231
45d1bd7 to
566664c
Compare
This was referenced Apr 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ValidationErrorandPaymentErrorcustom error classes and apply them consistently across the entire payment module, replacing 18 rawthrow new Error()calls in 10 filesinstanceofchecks for programmatic error handlinggetSubscriptionStatusto reusevalidateUSDCBasePermission()instead of duplicating validation logic inlineMotivation
Currently, the payment module uses generic
throw new Error(message)throughout, making it impossible for developers to programmatically distinguish between input validation errors and runtime payment failures. This forces consumers to parse error message strings, which is fragile and not a good DX.This PR introduces two structured error classes:
Error classification
ValidationErrorfield,providedValue,expectedFormatPaymentErrorcode,retryableBoth extend
Error, so existingcatchblocks continue to work — zero breaking changes.Files Changed
New
core/error/sdkErrors.ts—ValidationErrorandPaymentErrorclassesUpdated (error propagation)
utils/validation.ts—ValidationErrorwith field metadatautils/sendUserOpAndWait.ts—PaymentErrorwith error codes and retryabilityutils/validateUSDCBasePermission.ts—ValidationErrorfor chain/token mismatchesgetPaymentStatus.ts—PaymentErrorfor RPC and transfer parsing errorsutils/getExistingSmartWalletOrThrow.ts—PaymentErrorfor wallet lookup failuresutils/sdkManager.ts—PaymentErrorfor unexpected response formatssubscribe.ts—ValidationError+PaymentErrorfor wallet responsesutils/createCdpClientOrThrow.ts—PaymentErrorfor CDP initializationprepareCharge.ts/prepareRevoke.ts—PaymentErrorfor subscription not foundgetSubscriptionStatus.ts— Refactored to callvalidateUSDCBasePermission()(removed 25 lines of duplicated inline validation)getOrCreateSubscriptionOwnerWallet.ts—PaymentErrorfor CDP init and wallet creationUpdated (exports)
interface/payment/index.ts— Export error classes (browser)interface/payment/index.node.ts— Export error classes (Node.js)src/index.ts/src/index.node.ts— Re-export from top-levelUpdated (tests)
utils/validation.test.ts— Updated assertions + addedinstanceofchecksutils/sendUserOpAndWait.test.ts— Updated error messages +PaymentErrorcheckscharge.test.ts— Updated error message assertionspay.test.ts— Updated dataSuffix error assertionVerification
createSmartAccount.test.tsdue to network/viem AbortSignal issue — unrelated)Addresses #231