Skip to main content

Module error

Module error 

Source
Expand description

Module Error Trait

Provides a unified error trait for all Backbone modules. This enables consistent error handling and API responses across modules.

§Example

ⓘ
use backbone_core::ModuleError;

#[derive(Debug, thiserror::Error)]
pub enum UserError {
    #[error("User not found: {0}")]
    NotFound(String),

    #[error("Email already exists: {0}")]
    EmailExists(String),

    #[error("Invalid password")]
    InvalidPassword,
}

impl ModuleError for UserError {
    fn error_code(&self) -> &str {
        match self {
            Self::NotFound(_) => "USER_NOT_FOUND",
            Self::EmailExists(_) => "EMAIL_EXISTS",
            Self::InvalidPassword => "INVALID_PASSWORD",
        }
    }

    fn status_code(&self) -> u16 {
        match self {
            Self::NotFound(_) => 404,
            Self::EmailExists(_) => 409,
            Self::InvalidPassword => 401,
        }
    }

    fn user_message(&self) -> &str {
        match self {
            Self::NotFound(_) => "The requested user was not found",
            Self::EmailExists(_) => "This email is already registered",
            Self::InvalidPassword => "Invalid password provided",
        }
    }
}

Structs§

ErrorResponse
Standard error response for API endpoints.

Enums§

CommonError
Common module errors that can be reused.
ErrorCategory
Error category for monitoring and alerting.

Traits§

ModuleError
Unified error trait for Backbone modules.