| Crates.io | axum-service-errors |
| lib.rs | axum-service-errors |
| version | 0.3.3 |
| created_at | 2025-06-01 13:18:18.053833+00 |
| updated_at | 2025-07-03 09:45:14.757688+00 |
| description | A crate that provides an easy way for structured error responses using axum |
| homepage | https://github.com/adiepenbrock/axum-service-errors |
| repository | https://github.com/adiepenbrock/axum-service-errors |
| max_upload_size | |
| id | 1697158 |
| size | 48,360 |
A Rust crate that provides structured error responses for Axum web applications.
Cow<'a, str> for efficient string handlingIntoResponse for seamless use in Axum handlersjson feature flagAdd this to your Cargo.toml:
[dependencies]
axum-service-errors = "0.3.3"
# Enable JSON support (optional)
axum-service-errors = { version = "0.3.3", features = ["json"] }
use axum_service_errors::{ServiceError, JsonResponseBuilder, set_default_response_builder};
use axum::{routing::get, Router};
async fn handler() -> Result<String, ServiceError<'static>> {
// Return an error that will be automatically converted to an HTTP response
Err(ServiceError::new(1001, "VALIDATION_ERROR", 400, "Invalid input provided"))
}
#[tokio::main]
async fn main() {
// Set a global default response builder (optional)
set_default_response_builder(JsonResponseBuilder::new());
let app = Router::new().route("/", get(handler));
// ... start server
}
use axum_service_errors::ServiceError;
// Create a basic error
let error = ServiceError::new(1001, "VALIDATION_ERROR", 400, "Invalid input");
// Error with parameterized message
let error = ServiceError::new(1001, "VALIDATION_ERROR", 400, "Invalid {0} for field {1}")
.bind("email address")
.bind("user.email");
// Results in: "Invalid email address for field user.email"
// Add optional parameters for additional context
let error = ServiceError::new(1001, "VALIDATION_ERROR", 400, "Invalid input")
.parameter("field", "email")
.parameter("reason", "malformed");
use axum_service_errors::{ServiceError, JsonResponseBuilder, set_default_response_builder};
// Set global default at application startup
set_default_response_builder(JsonResponseBuilder::new());
// Now all errors automatically use JSON format
let error = ServiceError::new(1001, "VALIDATION_ERROR", 400, "Invalid input")
.parameter("field", "email");
// No need to call .with_response_builder() - uses JSON by default!
use axum_service_errors::{ServiceError, JsonResponseBuilder, PlainTextResponseBuilder};
// Set JSON as global default
set_default_response_builder(JsonResponseBuilder::new());
// This error will use JSON (global default)
let json_error = ServiceError::new(1001, "VALIDATION_ERROR", 400, "Invalid input");
// This error overrides to use plain text
let text_error = ServiceError::new(1002, "SYSTEM_ERROR", 500, "System failure")
.with_response_builder(PlainTextResponseBuilder::new());
use axum_service_errors::{ServiceError, ResponseBuilder, set_default_response_builder};
#[derive(Debug)]
struct CustomBuilder;
impl ResponseBuilder for CustomBuilder {
fn build(&self, error: &ServiceError) -> (String, &'static str) {
let body = format!("Custom error: {}", error.message);
(body, "text/plain")
}
}
// Set as global default
set_default_response_builder(CustomBuilder);
// Or use per-error
let error = ServiceError::new(1001, "CUSTOM_ERROR", 500, "Something went wrong")
.with_response_builder(CustomBuilder);
Enable with features = ["json"] in your Cargo.toml:
[dependencies]
axum-service-errors = { version = "0.3.3", features = ["json"] }
Provides:
JsonResponseBuilder for JSON-formatted error responsesset_default_response_builder(JsonResponseBuilder::new())The ServiceError struct contains:
code: Internal error code (u32)name: Error type name (e.g., "VALIDATION_ERROR")http_status: HTTP status code for the responsemessage: Human-readable error messagearguments: Values for message formatting (not serialized)parameters: Optional key-value pairs for additional contextresponse_builder: Optional custom response formatter (not serialized)cargo build
# Run all tests
cargo test
# Run tests with JSON feature
cargo test --features json
cargo fmt
cargo clippy
This project is licensed under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.