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:
- report its category via
ErrorCode::kind; - report its error code via
ErrorCode::code; - map to an HTTP status via
ErrorCode::http_status; - report whether it is retryable via
ErrorCode::is_retryable.
§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§
Provided Methods§
Sourcefn code(&self) -> &str
fn code(&self) -> &str
Return the error code (defaults to ErrorKind::default_code).
Sourcefn http_status(&self) -> u16
fn http_status(&self) -> u16
Return the HTTP status code (defaults to ErrorKind::http_status).
Sourcefn is_retryable(&self) -> bool
fn is_retryable(&self) -> bool
Whether the error is retryable (defaults to ErrorKind::is_retryable).