pub struct Error { /* private fields */ }Expand description
The framework error type.
Error is a single pointer wide, so Result<T, Error> stays small on the
success path. It implements IntoResponse, so handlers can return
Result<T> directly.
See the module documentation for the response format and the exposure policy.
Implementations§
Source§impl Error
impl Error
Sourcepub fn new(
status: StatusCode,
code: impl Into<Cow<'static, str>>,
message: impl Into<Cow<'static, str>>,
) -> Self
pub fn new( status: StatusCode, code: impl Into<Cow<'static, str>>, message: impl Into<Cow<'static, str>>, ) -> Self
Creates an error with an explicit status, machine-readable code and public message.
Sourcepub fn bad_request(message: impl Into<Cow<'static, str>>) -> Self
pub fn bad_request(message: impl Into<Cow<'static, str>>) -> Self
400 Bad Request with code BAD_REQUEST.
401 Unauthorized with code UNAUTHORIZED.
Sourcepub fn forbidden(message: impl Into<Cow<'static, str>>) -> Self
pub fn forbidden(message: impl Into<Cow<'static, str>>) -> Self
403 Forbidden with code FORBIDDEN.
Sourcepub fn not_found(message: impl Into<Cow<'static, str>>) -> Self
pub fn not_found(message: impl Into<Cow<'static, str>>) -> Self
404 Not Found with code NOT_FOUND.
Sourcepub fn method_not_allowed(message: impl Into<Cow<'static, str>>) -> Self
pub fn method_not_allowed(message: impl Into<Cow<'static, str>>) -> Self
405 Method Not Allowed with code METHOD_NOT_ALLOWED.
Sourcepub fn timeout(message: impl Into<Cow<'static, str>>) -> Self
pub fn timeout(message: impl Into<Cow<'static, str>>) -> Self
408 Request Timeout with code REQUEST_TIMEOUT.
Sourcepub fn conflict(message: impl Into<Cow<'static, str>>) -> Self
pub fn conflict(message: impl Into<Cow<'static, str>>) -> Self
409 Conflict with code CONFLICT.
Sourcepub fn payload_too_large(message: impl Into<Cow<'static, str>>) -> Self
pub fn payload_too_large(message: impl Into<Cow<'static, str>>) -> Self
413 Payload Too Large with code PAYLOAD_TOO_LARGE.
Sourcepub fn unsupported_media_type(message: impl Into<Cow<'static, str>>) -> Self
pub fn unsupported_media_type(message: impl Into<Cow<'static, str>>) -> Self
415 Unsupported Media Type with code UNSUPPORTED_MEDIA_TYPE.
Sourcepub fn unprocessable(message: impl Into<Cow<'static, str>>) -> Self
pub fn unprocessable(message: impl Into<Cow<'static, str>>) -> Self
422 Unprocessable Entity with code UNPROCESSABLE_ENTITY.
Sourcepub fn too_many_requests(message: impl Into<Cow<'static, str>>) -> Self
pub fn too_many_requests(message: impl Into<Cow<'static, str>>) -> Self
429 Too Many Requests with code TOO_MANY_REQUESTS.
503 Service Unavailable with code SERVICE_UNAVAILABLE.
Sourcepub fn validation(details: impl Serialize) -> Self
pub fn validation(details: impl Serialize) -> Self
422 Unprocessable Entity with code VALIDATION_FAILED and the given
structured details (typically a map of field names to messages).
Sourcepub fn internal(source: impl Into<BoxError>) -> Self
pub fn internal(source: impl Into<BoxError>) -> Self
500 Internal Server Error with a generic public message.
The source is logged when the response is produced but is never sent
to the client.
Sourcepub fn with_code(self, code: impl Into<Cow<'static, str>>) -> Self
pub fn with_code(self, code: impl Into<Cow<'static, str>>) -> Self
Replaces the machine-readable error code.
Sourcepub fn with_message(self, message: impl Into<Cow<'static, str>>) -> Self
pub fn with_message(self, message: impl Into<Cow<'static, str>>) -> Self
Replaces the public message.
Sourcepub fn with_details(self, details: impl Serialize) -> Self
pub fn with_details(self, details: impl Serialize) -> Self
Attaches structured, client-visible details.
If details fails to serialize, the error is kept and the details are
omitted (the failure is logged).
Sourcepub fn with_source(self, source: impl Into<BoxError>) -> Self
pub fn with_source(self, source: impl Into<BoxError>) -> Self
Attaches a diagnostic source error. It is logged, never sent to clients.
Sourcepub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self
pub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self
Adds a response header (for example Retry-After or WWW-Authenticate).
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
The HTTP status code.
Sourcepub fn headers(&self) -> &[(HeaderName, HeaderValue)]
pub fn headers(&self) -> &[(HeaderName, HeaderValue)]
Extra response headers attached to this error.
Sourcepub fn source_ref(&self) -> Option<&(dyn Error + Send + Sync + 'static)>
pub fn source_ref(&self) -> Option<&(dyn Error + Send + Sync + 'static)>
The diagnostic source, if any.
Sourcepub fn to_json_body(&self) -> Vec<u8> ⓘ
pub fn to_json_body(&self) -> Vec<u8> ⓘ
Serializes the client-visible JSON body.
Trait Implementations§
Source§impl Error for Error
impl Error for Error
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()
Source§impl From<ConfigError> for Error
impl From<ConfigError> for Error
Source§fn from(err: ConfigError) -> Self
fn from(err: ConfigError) -> Self
Source§impl From<ServerError> for Error
impl From<ServerError> for Error
Source§fn from(err: ServerError) -> Self
fn from(err: ServerError) -> Self
Source§impl From<StatusCode> for Error
impl From<StatusCode> for Error
Source§fn from(status: StatusCode) -> Self
fn from(status: StatusCode) -> Self
Creates an error from a bare status code, using the canonical reason phrase as the message and an upper-snake-case code.