#error-response #actix-web #http-response #error #web #response #http

actix-web-thiserror

Extend thiserror crate functionality for actix-web

8 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

#129 in HTTP server

Download history 4858/week @ 2024-08-08 4463/week @ 2024-08-15 1273/week @ 2024-08-22 2423/week @ 2024-08-29 2519/week @ 2024-09-05 4504/week @ 2024-09-12 2991/week @ 2024-09-19 3024/week @ 2024-09-26 2292/week @ 2024-10-03 2771/week @ 2024-10-10 4481/week @ 2024-10-17 3193/week @ 2024-10-24 3888/week @ 2024-10-31 6666/week @ 2024-11-07 4792/week @ 2024-11-14 3769/week @ 2024-11-21

19,403 downloads per month

MIT license

12KB
116 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

~15–26MB
~436K SLoC