Skip to main content

ErrorCode

Trait ErrorCode 

Source
pub trait ErrorCode:
    StdError
    + Send
    + Sync
    + 'static {
    // Required method
    fn kind(&self) -> ErrorKind;

    // Provided methods
    fn code(&self) -> &str { ... }
    fn http_status(&self) -> u16 { ... }
    fn is_retryable(&self) -> bool { ... }
}
Expand description

Error-contract trait.

Implement this trait once and any error type gains the ability to:

§Examples

use eventide_domain::error::{ErrorCode, ErrorKind};
use thiserror::Error;

#[derive(Debug, Error)]
#[error("order has been cancelled")]
struct OrderCancelled;

impl ErrorCode for OrderCancelled {
    fn kind(&self) -> ErrorKind {
        ErrorKind::InvalidState
    }

    fn code(&self) -> &str {
        "ORDER_CANCELLED"
    }
}

let err = OrderCancelled;
assert_eq!(err.kind(), ErrorKind::InvalidState);
assert_eq!(err.code(), "ORDER_CANCELLED");
assert_eq!(err.http_status(), 422);

Required Methods§

Source

fn kind(&self) -> ErrorKind

Return the error’s category.

Provided Methods§

Source

fn code(&self) -> &str

Return the error code (defaults to ErrorKind::default_code).

Source

fn http_status(&self) -> u16

Return the HTTP status code (defaults to ErrorKind::http_status).

Source

fn is_retryable(&self) -> bool

Whether the error is retryable (defaults to ErrorKind::is_retryable).

Implementors§