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: CodeMachine-readable error code.
message: StringHuman-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: boolWhether the client should retry this request.
status: u16HTTP status code.
retry_after: Option<Duration>Optional retry-after duration for rate limiting.
Implementations§
Source§impl Error
impl Error
Sourcepub fn new(code: Code, status: u16, message: impl Into<String>) -> Self
pub fn new(code: Code, status: u16, message: impl Into<String>) -> Self
Creates a new error with the given code, status, and message.
Sourcepub fn newf(code: Code, status: u16, message: impl Into<String>) -> Self
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));Sourcepub fn wrap(
code: Code,
status: u16,
message: impl Into<String>,
cause: impl Error,
) -> Self
pub fn wrap( code: Code, status: u16, message: impl Into<String>, cause: impl Error, ) -> Self
Creates a new error that wraps an underlying cause.
Sourcepub fn with_details(self, details: Value) -> Self
pub fn with_details(self, details: Value) -> Self
Adds structured details to the error.
Sourcepub fn with_trace_id(self, trace_id: impl Into<String>) -> Self
pub fn with_trace_id(self, trace_id: impl Into<String>) -> Self
Adds a trace ID for distributed tracing.
Sourcepub fn with_retryable(self, retryable: bool) -> Self
pub fn with_retryable(self, retryable: bool) -> Self
Sets whether the error is retryable.
Sourcepub fn with_status(self, status: u16) -> Self
pub fn with_status(self, status: u16) -> Self
Overrides the HTTP status code.
Sourcepub fn with_retry_after(self, duration: Duration) -> Self
pub fn with_retry_after(self, duration: Duration) -> Self
Sets the retry-after duration for rate-limited responses.
Sourcepub fn with_cause_message(self, cause: impl Error) -> Self
pub fn with_cause_message(self, cause: impl Error) -> Self
Attaches a cause message from an underlying error.
Useful when mapping domain errors (e.g., thiserror) to HTTP errors while preserving the underlying error message for debugging.
§Example
use error_envelope::{Error, Code};
#[derive(Debug)]
struct DatabaseError;
impl std::fmt::Display for DatabaseError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "connection timeout")
}
}
impl std::error::Error for DatabaseError {}
let db_err = DatabaseError;
let err = Error::new(Code::Internal, 500, "Database failure")
.with_cause_message(db_err);Source§impl Error
Helper constructors for common error types.
impl Error
Helper constructors for common error types.
Sourcepub fn bad_request(message: impl Into<String>) -> Self
pub fn bad_request(message: impl Into<String>) -> Self
Creates a bad request error (400).
Sourcepub fn validation(message: impl Into<String>) -> Self
pub fn validation(message: impl Into<String>) -> Self
Creates a validation error (400).
Creates an unauthorized error (401).
Sourcepub fn method_not_allowed(message: impl Into<String>) -> Self
pub fn method_not_allowed(message: impl Into<String>) -> Self
Creates a method not allowed error (405).
Sourcepub fn request_timeout(message: impl Into<String>) -> Self
pub fn request_timeout(message: impl Into<String>) -> Self
Creates a request timeout error (408).
Sourcepub fn payload_too_large(message: impl Into<String>) -> Self
pub fn payload_too_large(message: impl Into<String>) -> Self
Creates a payload too large error (413).
Sourcepub fn unprocessable_entity(message: impl Into<String>) -> Self
pub fn unprocessable_entity(message: impl Into<String>) -> Self
Creates an unprocessable entity error (422).
Sourcepub fn rate_limited(message: impl Into<String>) -> Self
pub fn rate_limited(message: impl Into<String>) -> Self
Creates a rate limited error (429).
Creates an unavailable error (503).
Sourcepub fn downstream(service: impl Into<String>, cause: impl Error) -> Self
pub fn downstream(service: impl Into<String>, cause: impl Error) -> Self
Creates a downstream error (502).
Sourcepub fn downstream_timeout(service: impl Into<String>, cause: impl Error) -> Self
pub fn downstream_timeout(service: impl Into<String>, cause: impl Error) -> Self
Creates a downstream timeout error (504).