Crates.io | actix-web-thiserror |
lib.rs | actix-web-thiserror |
version | 0.2.7 |
source | src |
created_at | 2023-04-23 20:56:54.90832 |
updated_at | 2024-06-16 20:02:49.681027 |
description | Extend thiserror crate functionality for actix-web. |
homepage | |
repository | https://github.com/enzious/actix-web-thiserror |
max_upload_size | |
id | 846836 |
size | 12,540 |
A crate that extends the thiserror crate functionality to automatically return a proper actix-web response.
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,
}
pub async fn error_test() -> Result<HttpResponse, Error> {
Err(Base64ImageError::InvalidImageFormat)?
}
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"
}
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