Crates.io | error-http |
lib.rs | error-http |
version | 0.2.2 |
source | src |
created_at | 2023-12-07 13:57:52.451872 |
updated_at | 2023-12-22 14:03:32.185677 |
description | Procedural macro for associating error enums with HTTP codes. |
homepage | https://github.com/majchrzamemil/error-http |
repository | https://github.com/majchrzamemil/error-http |
max_upload_size | |
id | 1061019 |
size | 12,541 |
The error-http crate defines a macro that implements proper HTTP responders for an enum with user-defined HTTP response codes and error messages.
#[derive(ToResponse)]
macro is web server orthogonal, which means for once defined enum only by switching feature
appropriate responder will be implemented for the chosen web server.
#[code(XXX)]
defined for a given enum variant will result in XXX
HTTP code being returned for it. Any variant without #[code(XXX)]
will default to 500
. Any invalid HTTP error code will default to 500
.
#[body(message)]
defined for a given variant will add a body to the response. body
can be any expression that
produces String
or &str
. Currently, there is no option to change the content type.
This crate only allows choosing exactly one of avaliable implementations. Avaliable implementation:
actix-web
axum
rocket
use error_http::ToResponse;
struct SomeStruct {
_a: i32,
_b: u32,
}
#[derive(ToResponse)]
enum Error {
#[code(400)]
First {
_a: i32,
_b: u32,
},
#[code(404)]
#[body("custom error message")]
Blah(SomeStruct, String),
Third,
#[code(99)]
Invalid,
}
Possible expansion of this crate consists of: