#[non_exhaustive]pub struct CodedError<E> {
pub error: E,
pub code: String,
pub retryable: Option<bool>,
pub fatal: bool,
pub status: Option<u16>,
}Expand description
An error with an associated error code.
Marked #[non_exhaustive] so future minor releases can add new
fields without breaking callers. External code must not
construct CodedError via struct-literal syntax; use
CodedError::new or the WithErrorCode::with_code
extension method.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.error: EThe original error
code: StringThe error code
retryable: Option<bool>Per-instance override for retryability
fatal: boolWhether this error is fatal
status: Option<u16>Per-instance override for status code
Implementations§
Source§impl<E> CodedError<E>
impl<E> CodedError<E>
Sourcepub fn new(error: E, code: impl Into<String>) -> Self
pub fn new(error: E, code: impl Into<String>) -> Self
Wrap an error with a stable code.
The code is not auto-registered in the global registry.
Pre-register the code at startup with
register_error_code if you want documentation URLs,
per-code descriptions, or retryability metadata to flow
through CodedError::code_info / CodedError::is_retryable.
§Behaviour change since 1.0.0
Prior 0.9.x releases auto-registered the code from inside
CodedError::new, which took a write lock on the global
registry on the first occurrence of every new code per
process. That lazy-registration step is gone in 1.0 — the
hot path is now a single allocation (the String from
code.into()) and zero locking. Code metadata that was
pre-registered via register_error_code continues to be
consulted via CodedError::code_info / is_retryable.
Sourcepub fn code_info(&self) -> Option<ErrorCodeInfo>
pub fn code_info(&self) -> Option<ErrorCodeInfo>
Get information about this error code from the registry
Sourcepub fn with_retryable(self, retryable: bool) -> Self
pub fn with_retryable(self, retryable: bool) -> Self
Set whether this error is retryable
Sourcepub fn with_fatal(self, fatal: bool) -> Self
pub fn with_fatal(self, fatal: bool) -> Self
Set whether this error is fatal
Sourcepub fn with_status(self, status: u16) -> Self
pub fn with_status(self, status: u16) -> Self
Set the HTTP status code for this error
Trait Implementations§
Source§impl<E: Debug> Debug for CodedError<E>
impl<E: Debug> Debug for CodedError<E>
Source§impl<E: Display> Display for CodedError<E>
impl<E: Display> Display for CodedError<E>
Source§impl<E: Error + 'static> Error for CodedError<E>
impl<E: Error + 'static> Error for CodedError<E>
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()