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 PaymentRequiredError

  • Returns 410 for PaymentExpiredError

  • Returns 403 for PaymentVerificationError

  • Returns 502 for TransactionBroadcastError

  • Returns 400 for InvalidPaymentRequestError

  • Returns 500 for other errors

Manual Error Handling

Error Response Format

Standard error response format:

Example Responses

Best Practices

  1. Handle all error types: Catch and handle specific error types

  2. Provide helpful messages: Return clear error messages to clients

  3. Log errors: Log errors for debugging and monitoring

  4. Don't expose sensitive info: Don't expose internal details in error messages

  5. Use error codes: Use error codes for programmatic error handling

  6. Retry appropriately: Retry transient errors, not permanent ones

  7. Monitor errors: Set up monitoring and alerts for errors

Error Monitoring

Next Steps

Last updated