pub struct AppError {
pub status: StatusCode,
pub code: String,
pub title: String,
pub detail: Option<String>,
pub errors: Option<Box<BTreeMap<String, Vec<String>>>>,
pub headers: Box<HeaderMap>,
pub source: Option<BoxError>,
}Expand description
An HTTP error. Handlers, extractors, guards and layers return it; it
renders in Navree’s JSON error shape, as application/json:
{"statusCode": 404, "message": "Deal not found", "error": "Not Found", "code": "not_found"}statusCodeis the HTTP status.messageis thedetailwhen there is one, else thetitle(the reason phrase by default, and"Internal server error"for 500s). For validation errors it is every field message, flattened in field order.erroris the reason phrase of the status ("Not Found").codeis the machine-readablecodea front end maps to a translation key: one of the framework’s stablecodes, or an app’s own (with_code).http.errors.code = falseleaves it out.errors(validation errors only) maps fields to their messages:
{"statusCode": 400, "message": ["email must be a valid email address"],
"error": "Bad Request", "code": "validation_failed",
"errors": {"email": ["email must be a valid email address"]}}The request id is in the X-Request-Id response header, not the body.
Any error type converts into a 500 with ? (its message is logged,
never sent); modules turn their own error types into specific statuses
with ModuleBuilderHttpExt::map_error.
AppError deliberately does not implement std::error::Error, which is
what makes the blanket From<E: Into<BoxError>> conversion possible.
Fields§
§status: StatusCodeThe HTTP status.
code: StringThe machine-readable code, rendered as code ("not_found",
"validation_failed", "campaign_already_running"): lower snake
case, stable across releases, what a front end maps to a translation
key. Empty leaves the member out.
title: StringThe default message, rendered as message when there is no
detail: the reason phrase for statuses built
with from_status.
detail: Option<String>A human-readable explanation of this occurrence, rendered as
message.
errors: Option<Box<BTreeMap<String, Vec<String>>>>Field errors ({"field.path": ["message"]}), for 400 validation
responses. Boxed, like headers, to keep Result<T, AppError> small.
headers: Box<HeaderMap>Extra response headers (Retry-After, WWW-Authenticate, …).
source: Option<BoxError>The underlying error, logged for 5xx responses and never sent.
Implementations§
Source§impl AppError
impl AppError
Sourcepub fn new(
status: StatusCode,
code: impl Into<String>,
title: impl Into<String>,
) -> Self
pub fn new( status: StatusCode, code: impl Into<String>, title: impl Into<String>, ) -> Self
An error with an explicit status, code and title (the default message).
Sourcepub fn from_status(status: StatusCode) -> Self
pub fn from_status(status: StatusCode) -> Self
The default error for status: its reason phrase as the title, and
a code derived from it.
Sourcepub fn bad_request(detail: impl Into<String>) -> Self
pub fn bad_request(detail: impl Into<String>) -> Self
400 with a message.
Sourcepub fn invalid_filter(detail: impl Into<String>) -> Self
pub fn invalid_filter(detail: impl Into<String>) -> Self
400 invalid_filter with a message: a list filter that names an
unknown field or has a malformed value.
Sourcepub fn unauthenticated() -> Self
pub fn unauthenticated() -> Self
401: the request is not authenticated.
Sourcepub fn two_factor_required() -> Self
pub fn two_factor_required() -> Self
401 two_factor_required "Two-factor authentication required":
the password was right, and the account also needs a second factor.
Sourcepub fn limit_reached(detail: impl Into<String>) -> Self
pub fn limit_reached(detail: impl Into<String>) -> Self
409 limit_reached with a message: a plan or quota limit stops the
change (seats, projects, monthly sends).
Sourcepub fn demo_read_only() -> Self
pub fn demo_read_only() -> Self
403 demo_read_only "Demo workspaces are read-only.": a write in a
workspace that only shows sample data.
Sourcepub fn csrf_mismatch() -> Self
pub fn csrf_mismatch() -> Self
403 "Invalid CSRF token": the CSRF token is missing or does not
match the session.
Sourcepub fn page_expired() -> Self
pub fn page_expired() -> Self
419 page_expired "Page Expired": the page was open too long, or the
session it belongs to changed, so what it sends can’t be trusted. The
user reloads and tries again.
Sourcepub fn validation(errors: BTreeMap<String, Vec<String>>) -> Self
pub fn validation(errors: BTreeMap<String, Vec<String>>) -> Self
400 with field errors ({"field.path": ["message"]}): message is
every field message, flattened in field order, and errors the map.
Sourcepub fn payload_too_large(limit: usize) -> Self
pub fn payload_too_large(limit: usize) -> Self
413: the request body is larger than limit bytes.
Sourcepub fn too_many_requests(retry_after: Duration) -> Self
pub fn too_many_requests(retry_after: Duration) -> Self
429 with a Retry-After header (whole seconds, at least 1).
503 with a message.
Sourcepub fn internal(err: impl Into<BoxError>) -> Self
pub fn internal(err: impl Into<BoxError>) -> Self
500 caused by err. The client sees "Internal server error"; the
error is logged.
Sourcepub fn with_detail(self, detail: impl Into<String>) -> Self
pub fn with_detail(self, detail: impl Into<String>) -> Self
Sets the message.
Sourcepub fn with_code(self, code: impl Into<String>) -> Self
pub fn with_code(self, code: impl Into<String>) -> Self
Sets the machine-readable code (lower snake case, such as
campaign_already_running); the status and the message stay.
Sourcepub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self
pub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self
Adds a response header.
Sourcepub fn with_source(self, source: impl Into<BoxError>) -> Self
pub fn with_source(self, source: impl Into<BoxError>) -> Self
Records the underlying error (logged for 5xx responses, never sent).
Sourcepub fn body(&self) -> Value
pub fn body(&self) -> Value
The response body:
{"statusCode", "message", "error", "code"}, plus "errors" for
validation errors.
Sourcepub fn take_from(response: &mut Response) -> Option<AppError>
pub fn take_from(response: &mut Response) -> Option<AppError>
Takes the error out of a response rendered by
into_response, so a pipeline other
than the HTTP server’s (navree-micro’s message handlers) can map and
log it the way the HTTP error layer does. None for any other
response, and once the error has been taken.
Trait Implementations§
Source§impl<E: Into<BoxError>> From<E> for AppError
Any error converts into a 500 (? in handlers), like anyhow.
impl<E: Into<BoxError>> From<E> for AppError
Any error converts into a 500 (? in handlers), like anyhow.
Source§impl IntoResponse for AppError
The rendered response carries the error itself, so the pipeline can map
and log it.
impl IntoResponse for AppError
The rendered response carries the error itself, so the pipeline can map and log it.