#alien #openapi #axum #struct #platform

alien-error

Error types for the Alien platform

8 stable releases

new 1.0.16 Mar 15, 2026
0.1.0 Mar 14, 2026

#4 in #alien

Download history 121/week @ 2026-03-11

121 downloads per month
Used in 12 crates

MIT license

22KB
334 lines

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

Dependencies

~0.5–2.1MB
~43K SLoC