Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/enzious/actix-web-thiserror
A crate that extends the thiserror crate functionality to automatically return a proper actix-web response.
https://github.com/enzious/actix-web-thiserror
Last synced: 5 days ago
JSON representation
A crate that extends the thiserror crate functionality to automatically return a proper actix-web response.
- Host: GitHub
- URL: https://github.com/enzious/actix-web-thiserror
- Owner: enzious
- License: mit
- Created: 2023-04-23T03:24:44.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-16T19:59:46.000Z (5 months ago)
- Last Synced: 2024-10-31T12:03:24.409Z (15 days ago)
- Language: Rust
- Homepage:
- Size: 38.1 KB
- Stars: 2
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# actix-web-thiserror
[![License](https://img.shields.io/github/license/enzious/actix-web-thiserror)](https://github.com/enzious/actix-web-thiserror/blob/master/LICENSE.md)
[![Contributors](https://img.shields.io/github/contributors/enzious/actix-web-thiserror)](https://github.com/enzious/actix-web-thiserror/graphs/contributors)
[![GitHub Repo stars](https://img.shields.io/github/stars/enzious/actix-web-thiserror?style=social)](https://github.com/enzious/actix-web-thiserror)
[![crates.io](https://img.shields.io/crates/v/actix-web-thiserror.svg)](https://crates.io/crates/actix-web-thiserror)A crate that extends the [thiserror] crate functionality to automatically
return a proper [actix-web] response.## Documentation
- [API Documentation](https://docs.rs/actix-web-thiserror)
## Error definition
```rust
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
```rust
pub async fn error_test() -> Result {
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`][response_transform]. To specify a custom ResponseTransform, implement [`ResponseTransform`][response_transform] 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
```[thiserror]: https://docs.rs/thiserror
[actix-web]: https://docs.rs/actix-web
[response_transform]: crate::ResponseTransform