Errors
Azu provides a comprehensive error handling system with type-safe error responses, automatic error recovery, and development-friendly error pages.
Azu::Response::Error
Base class for all Azu errors.
Properties
message : String- Error messagestatus_code : Int32- HTTP status codedetails : Hash(String, String)- Additional error details
Methods
initialize(message : String, status_code : Int32 = 500, details : Hash(String, String) = {} of String => String)
initialize(message : String, status_code : Int32 = 500, details : Hash(String, String) = {} of String => String)Create a new error with message, status code, and optional details.
error = Azu::Response::Error.new("User not found", 404)to_json : String
to_json : StringConvert error to JSON format.
error = Azu::Response::Error.new("Invalid input", 400)
json = error.to_json
# {"error": "Invalid input", "status": 400}Built-in Error Types
The following error classes are available in Azu::Response:
Azu::Response::BadRequest (400)
Bad Request error for invalid client requests.
Azu::Response::AuthenticationError (401)
Authentication required error.
Azu::Response::AuthorizationError (403)
Authorization/permission error.
Azu::Response::Forbidden (403)
Legacy alias for AuthorizationError.
Azu::Response::NotFound (404)
Resource not found error.
Azu::Response::TimeoutError (408)
Request timeout error.
Azu::Response::ValidationError (422)
Validation error with field-specific error messages.
Azu::Response::RateLimitError (429)
Rate limit exceeded error with optional retry-after.
Azu::Response::Error (500)
Base error class, defaults to 500 Internal Server Error.
Azu::Response::DatabaseError (500)
Database-related error.
Azu::Response::ExternalServiceError (502)
External service unavailable error.
Custom Error Classes
Basic Custom Error
Error with Context
Error with Stack Trace
Error Handling in Endpoints
Basic Error Handling
Error with Details
Error Recovery
Error Middleware
Automatic Error Handling
Error Logging
Error Responses
JSON Error Response
HTML Error Response
Error Page Template
Error Validation
Input Validation
Business Logic Validation
Error Testing
Unit Testing
Integration Testing
Error Monitoring
Error Tracking
Error Metrics
Error Recovery Strategies
Retry Logic
Fallback Responses
Next Steps
Learn about Performance Optimization
Explore Monitoring and Logging
Understand Security Best Practices
Last updated
Was this helpful?
