Crates.io | axum-error-handler |
lib.rs | axum-error-handler |
version | 0.1.1 |
source | src |
created_at | 2024-10-10 09:36:16.537778 |
updated_at | 2024-10-10 09:42:01.086081 |
description | A simple error handler for axum |
homepage | |
repository | https://github.com/MRDavidYen/axum-error-handler |
max_upload_size | |
id | 1403630 |
size | 10,945 |
Please notice that this is a expiremental project.
This proc-macro depends on
axum
,thiserror
andserde_json
crates.
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);
}
{
"result": null,
"error": {
"code": "INTERNAL_SERVER_ERROR",
"message": "Internal server error foo"
}
}