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.
The authentication.Error type provides several helper methods:
authentication/authentication_error.go
// Error formats the error into a string representationfunc (a *Error) Error() string { return fmt.Sprintf("%d %s: %s", a.StatusCode, a.Err, a.Message)}// GetMFAToken returns the MFA token associated with the error, if anyfunc (a *Error) GetMFAToken() string { if a == nil || a.MFAToken == "" { return "" } return a.MFAToken}// Status returns the status code of the errorfunc (a *Error) Status() int { return a.StatusCode}
user, err := mgmt.Users.Get(ctx, userID)if err != nil { var notFoundErr *management.NotFoundError if errors.As(err, ¬FoundErr) { // User doesn't exist, create a new one return createNewUser(ctx, mgmt) } return err}
createRequest := &management.CreateClientRequestContent{ Name: "My Application", AppType: &management.ClientAppTypeEnumSpa,}client, err := mgmt.Clients.Create(ctx, createRequest)if err != nil { var conflictErr *management.ConflictError if errors.As(err, &conflictErr) { fmt.Println("Client with this name already exists") // Try with a different name or update existing return nil } return err}
Both Authentication and Management API errors support Go’s error unwrapping:
import "errors"// Check if an error is or wraps a specific typeif errors.Is(err, someSpecificError) { // Handle specific error}// Extract the underlying errorvar apiErr *core.APIErrorif errors.As(err, &apiErr) { fmt.Printf("Status code: %d\n", apiErr.StatusCode)}
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
Check for TooManyRequestsError and implement exponential backoff or respect the X-RateLimit-Reset header to avoid overwhelming the API.
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
When working with error fields that may be nil, use the provided getter methods (e.g., GetMFAToken()) to avoid nil pointer panics.