awred

Crates.ioawred
lib.rsawred
version0.2.0
sourcesrc
created_at2021-09-25 09:44:57.518952
updated_at2021-09-26 20:16:48.274532
descriptionA convenient derive macro for actix_web::ResponseError trait
homepage
repositoryhttps://github.com/ondra05/awred
max_upload_size
id456143
size10,809
(ondra05)

documentation

README

awred

A convenient derive macro for actix_web::ResponseError trait.

Example

With Enum

use awred::ResponseError;
use serde::Serialize;
use thiserror::Error;

#[derive(Debug, Error, ResponseError, Serialize)]
pub enum AnError {
    #[error("Requested resource was not found")]
    #[status_code(NOT_FOUND)]
    ResourceNotFound,

    #[error("Forbidden: {reason}")]
    #[status_code(FORBIDDEN)]
    Forbidden { reason: String },

    // Internal Server Error
    #[error(transparent)]
    #[serde(skip)]
    IoError(#[from] std::io::Error),
}

With Struct

#[derive(Debug, Error, ResponseError, Serialize)]
#[error("Invalid username or password")]
#[status_code(BAD_REQUEST)]
pub struct InvalidCredentials;

Details

  • Status codes (from actix_web::http::StatusCode) are specified with #[status_code(...)] attribute
  • Variants/structs without #[status_code(...)] attribute return Internal Server Error with empty body
  • Response body consists of serialised error and message (error.to_string())

Error response body format

{
    "error": "error",
    "message": "error.to_string()",
}
Commit count: 0

cargo fmt