axum-service-errors
A Rust crate that provides structured error responses for Axum web applications.
Features
- Structured Error Handling: Define errors with error codes, names, HTTP status codes, and messages
- Zero-Copy Strings: Uses
Cow<'a, str>for efficient string handling - Message Formatting: Support for parameterized messages with argument binding
- Pluggable Response Builders: Customize response formats with global defaults or per-error overrides
- Axum Integration: Implements
IntoResponsefor seamless use in Axum handlers - Serialization Support: Serde support for error serialization
- Optional JSON Feature: Enable JSON responses with the
jsonfeature flag
Installation
Add this to your Cargo.toml:
[]
= "0.3.3"
# Enable JSON support (optional)
= { = "0.3.3", = ["json"] }
Quick Start
use ;
use ;
async
async
Usage Examples
Basic Error Creation
use ServiceError;
// Create a basic error
let error = new;
Message Formatting with Arguments
// Error with parameterized message
let error = new
.bind
.bind;
// Results in: "Invalid email address for field user.email"
Adding Parameters
// Add optional parameters for additional context
let error = new
.parameter
.parameter;
Global Default Response Builder
use ;
// Set global default at application startup
set_default_response_builder;
// Now all errors automatically use JSON format
let error = new
.parameter;
// No need to call .with_response_builder() - uses JSON by default!
Per-Error Response Builder Override
use ;
// Set JSON as global default
set_default_response_builder;
// This error will use JSON (global default)
let json_error = new;
// This error overrides to use plain text
let text_error = new
.with_response_builder;
Custom Response Builder
use ;
;
// Set as global default
set_default_response_builder;
// Or use per-error
let error = new
.with_response_builder;
Features
Default Features
- Plain text response formatting (fallback when no global default is set)
- Basic error structure with code, name, status, and message
- Message formatting with arguments
- Optional parameters support
- Global default response builder configuration
JSON Feature
Enable with features = ["json"] in your Cargo.toml:
[]
= { = "0.3.3", = ["json"] }
Provides:
JsonResponseBuilderfor JSON-formatted error responses- Automatic JSON serialization of error data
- Can be set as global default with
set_default_response_builder(JsonResponseBuilder::new())
Error Structure
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)
Development
Building
Running Tests
# Run all tests
# Run tests with JSON feature
Formatting and Linting
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.