pub struct ErrorResponse {
pub status: u16,
pub code: AppCode,
pub message: String,
pub details: Option<String>,
pub retry: Option<RetryAdvice>,
pub www_authenticate: Option<String>,
}Expand description
Public, wire-level error payload for HTTP APIs.
This type is serialized to JSON (or another transport format) and forms part of the stable wire contract between services and clients.
Fields§
§status: u16HTTP status code (e.g. 404, 422, 500).
code: AppCodeStable machine-readable error code.
message: StringHuman-oriented, non-sensitive message.
details: Option<String>serde_json only.Optional textual details (if serde_json is not enabled).
retry: Option<RetryAdvice>Optional retry advice. If present, integrations set the Retry-After
header.
www_authenticate: Option<String>Optional authentication challenge. If present, integrations set the
WWW-Authenticate header.
Example value: Bearer realm="api", error="invalid_token".
Implementations§
Source§impl ErrorResponse
impl ErrorResponse
Sourcepub fn new(
status: u16,
code: AppCode,
message: impl Into<String>,
) -> AppResult<Self>
pub fn new( status: u16, code: AppCode, message: impl Into<String>, ) -> AppResult<Self>
Construct a new ErrorResponse with a status code, a stable
AppCode, and a public message.
§Errors
Returns AppError if status is not a valid HTTP status code.
Sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
Convert numeric status into StatusCode.
Invalid codes default to StatusCode::INTERNAL_SERVER_ERROR.
§Examples
use http::StatusCode;
use masterror::{AppCode, ErrorResponse};
let resp = ErrorResponse::new(404, AppCode::NotFound, "missing").expect("status");
assert_eq!(resp.status_code(), StatusCode::NOT_FOUND);Source§impl ErrorResponse
impl ErrorResponse
Sourcepub fn with_details_text(self, details: impl Into<String>) -> Self
Available on non-crate feature serde_json only.
pub fn with_details_text(self, details: impl Into<String>) -> Self
serde_json only.Attach plain-text details (available when serde_json is disabled).
Source§impl ErrorResponse
Legacy constructor retained for migration purposes.
impl ErrorResponse
Legacy constructor retained for migration purposes.
Deprecated: prefer ErrorResponse::new with an AppCode argument.
§Examples
#[allow(deprecated)]
let response = ErrorResponse::new_legacy(404, "Not found");
assert_eq!(response.status, 404);
assert_eq!(response.message, "Not found");Sourcepub fn new_legacy(status: u16, message: impl Into<String>) -> Self
👎Deprecated: Use new(status, code, message) instead
pub fn new_legacy(status: u16, message: impl Into<String>) -> Self
Construct an error response with only (status, message).
This defaults the code to AppCode::Internal. Kept temporarily to
ease migration from versions prior to 0.3.0.
§Examples
#[allow(deprecated)]
let response = ErrorResponse::new_legacy(500, "Internal error");
assert_eq!(response.status, 500);
assert_eq!(response.code, AppCode::Internal);
assert_eq!(response.message, "Internal error");Source§impl ErrorResponse
impl ErrorResponse
Sourcepub fn with_retry_after_secs(self, secs: u64) -> Self
pub fn with_retry_after_secs(self, secs: u64) -> Self
Attach retry advice (number of seconds).
See with_retry_after_duration for
using a Duration. When present, integrations set the Retry-After
header automatically.
§Examples
use masterror::{AppCode, ErrorResponse};
let resp = ErrorResponse::new(503, AppCode::Internal, "unavailable")
.expect("status")
.with_retry_after_secs(120);
assert_eq!(resp.retry.expect("retry").after_seconds, 120);Sourcepub fn with_retry_after_duration(self, dur: Duration) -> Self
pub fn with_retry_after_duration(self, dur: Duration) -> Self
Attach retry advice as a Duration.
Equivalent to with_retry_after_secs.
When present, integrations set the Retry-After header automatically.
§Examples
use core::time::Duration;
use masterror::{AppCode, ErrorResponse};
let resp = ErrorResponse::new(503, AppCode::Internal, "retry later")
.expect("status")
.with_retry_after_duration(Duration::from_secs(60));
assert_eq!(resp.retry.expect("retry").after_seconds, 60);Sourcepub fn with_www_authenticate(self, value: impl Into<String>) -> Self
pub fn with_www_authenticate(self, value: impl Into<String>) -> Self
Attach an authentication challenge string.
When present, integrations set the WWW-Authenticate header
automatically.
§Examples
use masterror::{AppCode, ErrorResponse};
let resp = ErrorResponse::new(401, AppCode::Unauthorized, "auth required")
.expect("status")
.with_www_authenticate("Bearer realm=\"api\"");
assert_eq!(
resp.www_authenticate.as_deref(),
Some("Bearer realm=\"api\"")
);Trait Implementations§
Source§impl Clone for ErrorResponse
impl Clone for ErrorResponse
Source§fn clone(&self) -> ErrorResponse
fn clone(&self) -> ErrorResponse
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ErrorResponse
impl Debug for ErrorResponse
Source§impl<'de> Deserialize<'de> for ErrorResponse
impl<'de> Deserialize<'de> for ErrorResponse
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for ErrorResponse
Format ErrorResponse for logging and debugging.
impl Display for ErrorResponse
Format ErrorResponse for logging and debugging.
Produces a concise format: "{status} {code:?}: {message}".
§Examples
use masterror::{AppCode, ErrorResponse};
let resp = ErrorResponse::new(404, AppCode::NotFound, "user not found").expect("status");
let formatted = resp.to_string();
assert!(formatted.contains("404"));
assert!(formatted.contains("NOT_FOUND"));
assert!(formatted.contains("user not found"));Source§impl From<&Error> for ErrorResponse
Convert borrowed AppError to ErrorResponse.
impl From<&Error> for ErrorResponse
Convert borrowed AppError to ErrorResponse.
Clones the error’s message and metadata, leaving the original error intact.
Respects MessageEditPolicy for message visibility.
§Examples
use masterror::{AppError, ErrorResponse};
let err = AppError::conflict("resource exists");
let resp: ErrorResponse = (&err).into();
assert_eq!(resp.message, "resource exists");
// Original error remains intact
assert_eq!(err.message.as_deref(), Some("resource exists"));Source§impl From<Error> for ErrorResponse
Convert owned AppError to ErrorResponse.
impl From<Error> for ErrorResponse
Convert owned AppError to ErrorResponse.
Consumes the error, transferring ownership of message and metadata.
Respects MessageEditPolicy for message visibility.
§Examples
use masterror::{AppError, AppErrorKind, ErrorResponse};
let err = AppError::validation("invalid email");
let resp: ErrorResponse = err.into();
assert_eq!(resp.status, AppErrorKind::Validation.http_status());
assert_eq!(resp.message, "invalid email");