axum-error-handler

Crates.ioaxum-error-handler
lib.rsaxum-error-handler
version0.1.1
sourcesrc
created_at2024-10-10 09:36:16.537778
updated_at2024-10-10 09:42:01.086081
descriptionA simple error handler for axum
homepage
repositoryhttps://github.com/MRDavidYen/axum-error-handler
max_upload_size
id1403630
size10,945
David Yen (MRDavidYen)

documentation

README

Axum Error Handler

Please notice that this is a expiremental project.

This proc-macro depends on axum, thiserror and serde_json crates.

Basic Usage

use axum_error_handler::AxumErrorResponse;
    use thiserror::Error;

    #[derive(Debug, Error, AxumErrorResponse)]
    pub enum TestError {
        #[error("Bad request: {0}")]
        #[status_code("400")]
        #[code("BAD_REQUEST")]
        BadRequest(String),
        #[error("Internal server error {0}")]
        #[status_code("500")]
        #[code("INTERNAL_SERVER_ERROR")]
        AnotherNoStringError(#[from] InnerError),
    }

    #[derive(Debug, Error)]
    pub enum InnerError {
        #[error("Bad request: {0}")]
        BadRequest(String),
        #[error("Internal server error {0}")]
        AnotherNoStringError(axum::Error),
    }

    #[tokio::test]
    async fn is_correct_body() {
        let err = TestError::AnotherNoStringError(InnerError::BadRequest("foo".to_string()));

        let resp = err.into_response();
        let body = resp.into_body();

        let bytes = to_bytes(body, 10485760).await.unwrap();
        let body_str = String::from_utf8_lossy(&bytes).to_string();

        println!("{:?}", body_str);
    }

Output

{
  "result": null,
    "error": {
        "code": "INTERNAL_SERVER_ERROR",
        "message": "Internal server error foo"
    }
}
Commit count: 6

cargo fmt