Error

Struct Error 

Source
pub struct Error {
    pub code: Code,
    pub message: String,
    pub details: Option<Value>,
    pub trace_id: Option<String>,
    pub retryable: bool,
    pub status: u16,
    pub retry_after: Option<Duration>,
    /* private fields */
}
Expand description

Structured error envelope for HTTP APIs.

Fields§

§code: Code

Machine-readable error code.

§message: String

Human-readable error message.

§details: Option<Value>

Optional structured details (e.g., field-level validation errors).

§trace_id: Option<String>

Optional trace ID for distributed tracing.

§retryable: bool

Whether the client should retry this request.

§status: u16

HTTP status code.

§retry_after: Option<Duration>

Optional retry-after duration for rate limiting.

Implementations§

Source§

impl Error

Source

pub fn new(code: Code, status: u16, message: impl Into<String>) -> Self

Creates a new error with the given code, status, and message.

Source

pub fn newf(code: Code, status: u16, message: impl Into<String>) -> Self

Creates a new error with a formatted message.

This is a semantic alias for new() that signals the message is typically constructed with format!().

§Example
use error_envelope::{Error, Code};
let user_id = 123;
let err = Error::newf(Code::NotFound, 404, format!("user {} not found", user_id));
Source

pub fn wrap( code: Code, status: u16, message: impl Into<String>, cause: impl Error, ) -> Self

Creates a new error that wraps an underlying cause.

Source

pub fn with_details(self, details: Value) -> Self

Adds structured details to the error.

Source

pub fn with_trace_id(self, trace_id: impl Into<String>) -> Self

Adds a trace ID for distributed tracing.

Source

pub fn with_retryable(self, retryable: bool) -> Self

Sets whether the error is retryable.

Source

pub fn with_status(self, status: u16) -> Self

Overrides the HTTP status code.

Source

pub fn with_retry_after(self, duration: Duration) -> Self

Sets the retry-after duration for rate-limited responses.

Source

pub fn cause(&self) -> Option<&str>

Returns the cause message if available.

Source

pub fn status(&self) -> u16

Returns the HTTP status code.

Source§

impl Error

Helper constructors for common error types.

Source

pub fn internal(message: impl Into<String>) -> Self

Creates an internal server error (500).

Source

pub fn bad_request(message: impl Into<String>) -> Self

Creates a bad request error (400).

Source

pub fn validation(message: impl Into<String>) -> Self

Creates a validation error (400).

Source

pub fn unauthorized(message: impl Into<String>) -> Self

Creates an unauthorized error (401).

Source

pub fn forbidden(message: impl Into<String>) -> Self

Creates a forbidden error (403).

Source

pub fn not_found(message: impl Into<String>) -> Self

Creates a not found error (404).

Source

pub fn method_not_allowed(message: impl Into<String>) -> Self

Creates a method not allowed error (405).

Source

pub fn request_timeout(message: impl Into<String>) -> Self

Creates a request timeout error (408).

Source

pub fn conflict(message: impl Into<String>) -> Self

Creates a conflict error (409).

Source

pub fn gone(message: impl Into<String>) -> Self

Creates a gone error (410).

Source

pub fn payload_too_large(message: impl Into<String>) -> Self

Creates a payload too large error (413).

Source

pub fn unprocessable_entity(message: impl Into<String>) -> Self

Creates an unprocessable entity error (422).

Source

pub fn rate_limited(message: impl Into<String>) -> Self

Creates a rate limited error (429).

Source

pub fn timeout(message: impl Into<String>) -> Self

Creates a timeout error (504).

Source

pub fn unavailable(message: impl Into<String>) -> Self

Creates an unavailable error (503).

Source

pub fn downstream(service: impl Into<String>, cause: impl Error) -> Self

Creates a downstream error (502).

Source

pub fn downstream_timeout(service: impl Into<String>, cause: impl Error) -> Self

Creates a downstream timeout error (504).

Trait Implementations§

Source§

impl Clone for Error

Source§

fn clone(&self) -> Error

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for Error

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl Serialize for Error

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Error

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnwindSafe for Error

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.