Create Custom Errors
Basic Custom Error
class NotFoundError < Azu::Response::Error
def initialize(resource : String, id : String | Int64)
super("#{resource} with id #{id} not found", 404)
end
end
# Usage
raise NotFoundError.new("User", params["id"])Error with Context
class ValidationError < Azu::Response::Error
getter errors : Array(FieldError)
def initialize(@errors : Array(FieldError))
super("Validation failed", 422)
end
def to_json(io : IO)
{
error: message,
details: errors.map { |e| {field: e.field, message: e.message} }
}.to_json(io)
end
end
record FieldError, field : String, message : StringDomain-Specific Errors
Error Hierarchy
Error Responses
Error Handler Integration
Using Custom Errors in Endpoints
Error Documentation
See Also
Last updated
Was this helpful?
