Crate actix_web_error

source ·
Expand description

Error responses for actix-web made easy.

This crate will make it easy implementing actix_web::ResponseError for errors. It’s best used in combination with thiserror.

Error Responses

  • Json will respond with JSON in the form of { "error": <Display representation> } (application/json).
  • Text will respond with the Display representation of the error (text/plain).

Example

#[derive(Debug, thiserror::Error, actix_web_error::Json)]
#[status(BAD_REQUEST)] // default status for all variants
enum MyError {
    #[error("Missing: {0}")]
    MissingField(&'static str),
    #[error("Malformed Date")]
    MalformedDate,
    #[error("Internal Server Error")]
    #[status(500)] // specific override
    Internal,
}

#[derive(Debug, thiserror::Error, actix_web_error::Text)]
#[error("Item not found")]
#[status(404)]
struct MyOtherError;

Derive Macros