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 message

  • status_code : Int32 - HTTP status code

  • details : Hash(String, String) - Additional error details

Methods

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

Convert 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

Azu::Response::BadRequest

400 Bad Request error.

Azu::Response::Unauthorized

401 Unauthorized error.

Azu::Response::Forbidden

403 Forbidden error.

Azu::Response::NotFound

404 Not Found error.

Azu::Response::MethodNotAllowed

405 Method Not Allowed error.

Azu::Response::Conflict

409 Conflict error.

Azu::Response::UnprocessableEntity

422 Unprocessable Entity error.

Azu::Response::InternalServerError

500 Internal Server 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

Last updated

Was this helpful?