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.
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.
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.
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.
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 more