Overview
The Auth0 Go SDK provides typed error handling for both the Authentication API and Management API, allowing you to programmatically detect and handle specific error conditions.Authentication API Errors
The Authentication API uses theauthentication.Error type, which includes the HTTP status code, error code, and descriptive message.
Error Structure
authentication/authentication_error.go
Basic Error Handling
Handling Specific Error Codes
You can check theErr field to handle specific error conditions:
Error Methods
Theauthentication.Error type provides several helper methods:
authentication/authentication_error.go
Management API Errors
The Management API provides typed errors for different HTTP status codes. All errors embed thecore.APIError type.
Base Error Type
management/core/api_error.go
Typed Error Types
The Management API defines specific error types for each HTTP status code:management/errors.go
Handling Management API Errors
Common Error Scenarios
Handling Not Found Errors
Handling Rate Limiting
Handling Permission Errors
Handling Conflict Errors
Error Unwrapping
Both Authentication and Management API errors support Go’s error unwrapping:Best Practices
Always check for typed errors first
Always check for typed errors first
Use type assertions or
errors.As to check for specific error types before falling back to generic error handling. This allows you to provide better user feedback and handle recoverable errors appropriately.Handle rate limiting gracefully
Handle rate limiting gracefully
Check for
TooManyRequestsError and implement exponential backoff or respect the X-RateLimit-Reset header to avoid overwhelming the API.Log error details for debugging
Log error details for debugging
When logging errors, include the status code, error message, and any relevant headers. This helps with troubleshooting production issues.
Use safe getter methods
Use safe getter methods
When working with error fields that may be nil, use the provided getter methods (e.g.,
GetMFAToken()) to avoid nil pointer panics.Next Steps
Authentication API
Learn about the Authentication API client
Management API
Learn about the Management API client