Error Handling
Comprehensive guide to error handling in Shade402.
Error Classes
Shade402 defines specific error types for different scenarios:
X402Error
Base error class for all X402 errors:
class X402Error extends Error {
code: string;
details: ErrorDetails;
message: string;
}PaymentRequiredError
Thrown when payment is required:
class PaymentRequiredError extends X402Error {
paymentRequest: any;
code: 'PAYMENT_REQUIRED';
}PaymentExpiredError
Thrown when payment request has expired:
InsufficientFundsError
Thrown when wallet has insufficient balance:
PaymentVerificationError
Thrown when payment verification fails:
TransactionBroadcastError
Thrown when transaction broadcast fails:
InvalidPaymentRequestError
Thrown when payment request is invalid:
Error Codes Reference
Client-side Error Handling
Explicit Client
Automatic Client
Server-side Error Handling
Express Middleware
The error middleware automatically:
Returns 402 for
PaymentRequiredErrorReturns 410 for
PaymentExpiredErrorReturns 403 for
PaymentVerificationErrorReturns 502 for
TransactionBroadcastErrorReturns 400 for
InvalidPaymentRequestErrorReturns 500 for other errors
Manual Error Handling
Error Response Format
Standard error response format:
Example Responses
Best Practices
Handle all error types: Catch and handle specific error types
Provide helpful messages: Return clear error messages to clients
Log errors: Log errors for debugging and monitoring
Don't expose sensitive info: Don't expose internal details in error messages
Use error codes: Use error codes for programmatic error handling
Retry appropriately: Retry transient errors, not permanent ones
Monitor errors: Set up monitoring and alerts for errors
Error Monitoring
Next Steps
Review Best Practices for error handling patterns
Check out Troubleshooting for common issues
See Examples for error handling examples
Last updated
