Expand description
Alien-Error – minimal clean version with context-based API.
Provides:
• AlienErrorMetadata trait (implemented by enums via #[derive(AlienError)])
• AlienError<T> container (generic over error type)
• .context() extension method for AlienError Results
• .into_alien_error() for converting std errors
• Result<T> alias
• OpenAPI schema generation (with openapi feature)
• Axum IntoResponse implementation (with axum feature)
Use .context(YourError::Variant { ... }) on AlienError Results to wrap errors.
Use .into_alien_error() on std::error::Error Results to convert them first.
§OpenAPI Schema Generation
When the openapi feature is enabled, the AlienError struct implements
utoipa::ToSchema, allowing it to be used in OpenAPI documentation:
use utoipa::OpenApi;
use alien_error::AlienError;
#[derive(OpenApi)]
#[openapi(components(schemas(AlienError)))]
struct ApiDoc;§Axum Integration
When the axum feature is enabled, AlienError implements axum::response::IntoResponse,
allowing it to be returned directly from Axum handlers. By default, the IntoResponse
implementation uses external response behavior (sanitizes internal errors).
For different use cases, you can choose between:
§External API Responses (Default)
use axum::response::IntoResponse;
use alien_error::{AlienError, AlienErrorData};
// Default behavior - sanitizes internal errors for security
async fn api_handler() -> Result<String, AlienError<MyError>> {
Err(AlienError::new(MyError::InternalDatabaseError {
credentials: "secret".to_string()
}))
}
// Returns: HTTP 500 with {"code": "GENERIC_ERROR", "message": "Internal server error"}§Explicit External Responses
async fn api_handler() -> impl IntoResponse {
let error = AlienError::new(MyError::InternalDatabaseError {
credentials: "secret".to_string()
});
error.into_external_response() // Explicitly sanitize
}§Internal Service Communication
async fn internal_handler() -> impl IntoResponse {
let error = AlienError::new(MyError::InternalDatabaseError {
credentials: "secret".to_string()
});
error.into_internal_response() // Preserve all details
}
// Returns: HTTP 500 with full error details including sensitive informationStructs§
- Alien
Error - Canonical error container that provides a structured way to represent errors with rich metadata including error codes, human-readable messages, context, and chaining capabilities for error propagation.
- Generic
Error - A special marker type for generic/standard errors that don’t have specific metadata
Traits§
- Alien
Error Data - Data every public-facing error variant must expose.
- Context
- Extension trait for adding context to AlienError Results
- Context
Error - Extension trait for adding context directly to AlienError instances
- Into
Alien Error - Extension trait for converting standard errors to AlienError
- Into
Alien Error Direct - Extension trait for converting standard errors directly to AlienError
Type Aliases§
- Result
- Alias for the common
Resulttype used throughout an application. This is now generic over the error type for better type safety.