Skip to main content

Crate alien_error

Crate alien_error 

Source
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 information

Structs§

AlienError
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.
GenericError
A special marker type for generic/standard errors that don’t have specific metadata

Traits§

AlienErrorData
Data every public-facing error variant must expose.
Context
Extension trait for adding context to AlienError Results
ContextError
Extension trait for adding context directly to AlienError instances
IntoAlienError
Extension trait for converting standard errors to AlienError
IntoAlienErrorDirect
Extension trait for converting standard errors directly to AlienError

Type Aliases§

Result
Alias for the common Result type used throughout an application. This is now generic over the error type for better type safety.

Derive Macros§

AlienErrorData