Skip to main content

Overview

Monzoh provides a comprehensive set of exception types to help you handle different error conditions that can occur when interacting with the Monzo API. Proper error handling ensures your application can gracefully respond to network issues, authentication problems, and API limitations.

Exception Hierarchy

All Monzoh exceptions inherit from the base MonzoError class:

Exception Types

MonzoError (Base Exception)

The base exception class that all other Monzoh exceptions inherit from.

MonzoNetworkError

Raised when network connectivity issues occur:
Common causes:
  • No internet connection
  • DNS resolution failures
  • Connection timeouts
  • SSL/TLS handshake failures

MonzoAuthenticationError

Raised when authentication fails:
Common causes:
  • Expired access token with invalid refresh token
  • Revoked access token
  • Invalid OAuth2 credentials
  • Missing authentication headers

MonzoValidationError

Raised when request parameters fail validation:
Common causes:
  • Invalid parameter formats (e.g., malformed IDs)
  • Missing required parameters
  • Parameter values out of allowed range
  • Invalid data types

MonzoBadRequestError

Raised for HTTP 400 Bad Request errors:
Common causes:
  • Malformed request data
  • Invalid parameter combinations
  • Business logic violations
  • Insufficient permissions for the operation

MonzoNotFoundError

Raised when requested resources don’t exist (HTTP 404):
Common causes:
  • Invalid or non-existent resource IDs
  • Resources that have been deleted
  • Attempting to access resources you don’t have permission for

MonzoRateLimitError

Raised when API rate limits are exceeded (HTTP 429):
Common causes:
  • Making too many requests in a short time period
  • Exceeding daily/hourly request quotas
  • Multiple clients using the same credentials

MonzoServerError

Raised for server-side errors (HTTP 5xx):
Common causes:
  • Temporary API server issues
  • Database connectivity problems
  • Internal server errors

Best Practices

1. Specific Exception Handling

Handle specific exceptions rather than catching all errors:

2. Retry Logic with Exponential Backoff

Implement intelligent retry logic for transient errors:

3. Graceful Degradation

Implement fallback behavior when API calls fail:

4. Context Managers for Resource Cleanup

Use context managers to ensure proper resource cleanup:

5. Logging and Monitoring

Implement comprehensive logging for error tracking:

Error Response Details

Many Monzoh exceptions include additional details from the API response:

Testing Error Handling

Test your error handling with mock mode:

Summary

Proper error handling is crucial for building robust applications with Monzoh:
  1. Use specific exception types to handle different error conditions appropriately
  2. Implement retry logic with exponential backoff for transient errors
  3. Provide graceful degradation with fallback mechanisms
  4. Log errors comprehensively for monitoring and debugging
  5. Test error scenarios to ensure your handling works correctly
By following these practices, your application will be resilient to various failure modes and provide a better user experience.