5 releases

0.2.1 Mar 10, 2024
0.2.0 Jun 13, 2023
0.1.7 Apr 26, 2023
0.1.6 Apr 24, 2023
0.1.0 Apr 23, 2023

#22 in #thiserror

Download history 2628/week @ 2024-01-10 2887/week @ 2024-01-17 2278/week @ 2024-01-24 1730/week @ 2024-01-31 764/week @ 2024-02-07 3091/week @ 2024-02-14 1596/week @ 2024-02-21 1222/week @ 2024-02-28 1545/week @ 2024-03-06 2705/week @ 2024-03-13 2718/week @ 2024-03-20 1105/week @ 2024-03-27 1770/week @ 2024-04-03 1720/week @ 2024-04-10 3010/week @ 2024-04-17 2330/week @ 2024-04-24

9,250 downloads per month
Used in actix-web-thiserror

MIT license

16KB
311 lines

actix-web-thiserror

License Contributors GitHub Repo stars crates.io

A crate that extends the thiserror crate functionality to automatically return a proper actix-web response.

Documentation

Error definition

use actix_web_thiserror::ResponseError;
use thiserror::Error;

#[derive(Debug, Error, ResponseError)]
pub enum Base64ImageError {
  #[response(reason = "INVALID_IMAGE_FORMAT")]
  #[error("invalid image format")]
  InvalidImageFormat,
  #[response(reason = "INVALID_STRING")]
  #[error("invalid string")]
  InvalidString,
}

Error implementation

pub async fn error_test() -> Result<HttpResponse, Error> {
  Err(Base64ImageError::InvalidImageFormat)?
}

Error response

The reason is a string that may be given to the client in some form to explain the error, if appropriate. Here it is as an enum that can be localized.

Note: This response has been formatted by a ResponseTransform. To specify a custom ResponseTransform, implement ResponseTransform and add #[response(transform = custom)] under your derive.

{
    "result": 0,
    "reason": "INVALID_IMAGE_FORMAT"
}

Error logging

The error text automatically prints to the log when the error is returned out through a http response.

Apr 23 02:19:35.211 ERRO Response error: invalid image format
    Base64ImageError(InvalidImageFormat), place: example/src/handler.rs:5 example::handler

Dependencies

~17–32MB
~543K SLoC