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

actix-web-thiserror

Extend thiserror crate functionality for actix-web

6 releases

0.2.1 Mar 10, 2024
0.2.0 Jun 13, 2023
0.1.7 Apr 26, 2023

#156 in HTTP server

Download history 1/week @ 2023-12-22 1561/week @ 2023-12-29 2857/week @ 2024-01-05 2468/week @ 2024-01-12 2971/week @ 2024-01-19 1300/week @ 2024-01-26 1808/week @ 2024-02-02 1506/week @ 2024-02-09 2548/week @ 2024-02-16 1723/week @ 2024-02-23 842/week @ 2024-03-01 2124/week @ 2024-03-08 2990/week @ 2024-03-15 1946/week @ 2024-03-22 934/week @ 2024-03-29 1974/week @ 2024-04-05

8,215 downloads per month

MIT license

11KB
91 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–31MB
~533K SLoC