#enums

macro error-http

Procedural macro for associating error enums with HTTP codes

5 releases

0.2.2 Dec 22, 2023
0.2.1 Dec 8, 2023
0.1.2 Dec 8, 2023

#580 in Procedural macros

Download history 3/week @ 2024-07-24 29/week @ 2024-07-31 3/week @ 2024-09-18 5/week @ 2024-09-25

59 downloads per month

MIT license

9KB
119 lines

error-http

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.

Supported web servers

This crate only allows choosing exactly one of avaliable implementations. Avaliable implementation:

  • actix-web
  • axum
  • rocket

Usage example

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,
}

Future development

Possible expansion of this crate consists of:

  • content type and JSON body
  • tracing/logging

Dependencies

~1.5MB
~38K SLoC