https://github.com/mehcode/axum-error-object
Provides a Result<T> type and related utility types that can be used to holistically provide object response errors.
https://github.com/mehcode/axum-error-object
anyhow axum error
Last synced: 7 months ago
JSON representation
Provides a Result<T> type and related utility types that can be used to holistically provide object response errors.
- Host: GitHub
- URL: https://github.com/mehcode/axum-error-object
- Owner: mehcode
- License: apache-2.0
- Created: 2025-04-24T16:41:09.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-05-18T23:19:32.000Z (10 months ago)
- Last Synced: 2025-06-19T04:20:27.306Z (8 months ago)
- Topics: anyhow, axum, error
- Language: Rust
- Homepage:
- Size: 26.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# axum-error-object
Provides a `Result` type and related utility types
that can be used to holistically provide object response errors.
## Install
```toml
axum-error-object = "0.0.1"
```
## Usage
```rust
use axum_error_object::{Result, Status, Context, IntoErrorResponse};
use serde::Serialize;
use derive_more::Display;
fn call_fallible() -> std::result::Result { /* ... */ }
fn call_maybe() -> Option { /* ... */ }
#[derive(Display, ErrorResponse, Serialize)]
#[serde(rename_all = "snake_case", tag = "code", content = "meta")]
enum AppError {
#[display("Whoa there that is {that}")]
#[response(status = 420)]
WhoaThere { that: u8 },
#[display("Oh No")]
#[response(status = 501)]
OhNo,
}
async fn handler() -> Result {
// will return an opaque 500 on any unhandled error or None
call_fallible()?;
call_maybe()?;
// will return an opaque 404 on None or error
call_fallible().status(StatusCode::NOT_FOUND)?;
call_maybe().status(StatusCode::NOT_FOUND)?;
// will return the following 420 response:
// {
// "code": "whoa_there",
// "title": "Whoa there that is 10",
// "meta": { "that": 10 }
// }
// preserves the original error information as source (does not return from the API)
// useful for logs and inspection
call_fallible().with_context(|| AppError::WhoaThere { that: 30 })?;
call_fallible().context(AppError::OhNo)?;
call_maybe().context(AppError::OhNo)?;
// return ok
Ok(StatusCode::OK)
}
```
## License
Licensed under either of
Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
**Contribution**
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.