10 releases

0.2.7 Jun 16, 2024
0.2.6 Jun 14, 2024
0.2.1 Mar 10, 2024
0.2.0 Jun 13, 2023
0.1.7 Apr 26, 2023

#25 in #thiserror

Download history 4127/week @ 2024-06-21 2063/week @ 2024-06-28 3190/week @ 2024-07-05 2965/week @ 2024-07-12 3009/week @ 2024-07-19 3475/week @ 2024-07-26 3597/week @ 2024-08-02 5714/week @ 2024-08-09 3185/week @ 2024-08-16 953/week @ 2024-08-23 2571/week @ 2024-08-30 3049/week @ 2024-09-06 4252/week @ 2024-09-13 3259/week @ 2024-09-20 2815/week @ 2024-09-27 1790/week @ 2024-10-04

12,928 downloads per month
Used in actix-web-thiserror

MIT license

20KB
443 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 ERROR Response error: invalid image format
    Base64ImageError(InvalidImageFormat), place: example/src/handler.rs:5 example::handler

Dependencies

~16–26MB
~460K SLoC