Skip to main content

CqrsErrorCode

Trait CqrsErrorCode 

Source
pub trait CqrsErrorCode:
    Debug
    + Display
    + Clone
    + MaybeSend
    + MaybeSync
    + 'static {
    // Required methods
    fn domain() -> &'static str;
    fn domain_prefix() -> u16;
    fn error_index(&self) -> u16;
    fn http_status(&self) -> StatusCode;

    // Provided methods
    fn internal_code(&self) -> u16 { ... }
    fn code_string(&self) -> String { ... }
    fn error(&self, message: impl Into<String>) -> CqrsError
       where Self: Sized { ... }
}
Expand description

Trait that all domain error codes must implement.

Each domain defines its own enum implementing this trait. The trait provides the contract for error code metadata.

§Example

use cqrs_rust_lib::define_domain_errors;

define_domain_errors! {
    domain: "plan",
    prefix: 4,
    errors: {
        NotFound => (1, StatusCode::NOT_FOUND, "NOT_FOUND"),
        SlugExists => (2, StatusCode::CONFLICT, "SLUG_EXISTS"),
    }
}

Required Methods§

Source

fn domain() -> &'static str

The domain this error belongs to (e.g., “tenant”, “license”)

Source

fn domain_prefix() -> u16

Domain prefix for internal codes (0-9) Each domain gets a unique prefix.

Source

fn error_index(&self) -> u16

Unique error index within the domain (0-999)

Source

fn http_status(&self) -> StatusCode

HTTP status code for this error

Provided Methods§

Source

fn internal_code(&self) -> u16

Full internal code: domain_prefix * 1000 + error_index Example: Tenant (4) + NotFound (1) = 4001

Source

fn code_string(&self) -> String

String representation of the error code for JSON serialization Format: DOMAIN_ERROR_NAME (e.g., “PLAN_NOT_FOUND”)

Source

fn error(&self, message: impl Into<String>) -> CqrsError
where Self: Sized,

Create a CqrsError from this code with a message

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§