Skip to main content

Error

Struct Error 

Source
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

Source

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.

Source

pub fn bad_request(message: impl Into<Cow<'static, str>>) -> Self

400 Bad Request with code BAD_REQUEST.

Source

pub fn unauthorized(message: impl Into<Cow<'static, str>>) -> Self

401 Unauthorized with code UNAUTHORIZED.

Source

pub fn forbidden(message: impl Into<Cow<'static, str>>) -> Self

403 Forbidden with code FORBIDDEN.

Source

pub fn not_found(message: impl Into<Cow<'static, str>>) -> Self

404 Not Found with code NOT_FOUND.

Source

pub fn method_not_allowed(message: impl Into<Cow<'static, str>>) -> Self

405 Method Not Allowed with code METHOD_NOT_ALLOWED.

Source

pub fn timeout(message: impl Into<Cow<'static, str>>) -> Self

408 Request Timeout with code REQUEST_TIMEOUT.

Source

pub fn conflict(message: impl Into<Cow<'static, str>>) -> Self

409 Conflict with code CONFLICT.

Source

pub fn payload_too_large(message: impl Into<Cow<'static, str>>) -> Self

413 Payload Too Large with code PAYLOAD_TOO_LARGE.

Source

pub fn unsupported_media_type(message: impl Into<Cow<'static, str>>) -> Self

415 Unsupported Media Type with code UNSUPPORTED_MEDIA_TYPE.

Source

pub fn unprocessable(message: impl Into<Cow<'static, str>>) -> Self

422 Unprocessable Entity with code UNPROCESSABLE_ENTITY.

Source

pub fn too_many_requests(message: impl Into<Cow<'static, str>>) -> Self

429 Too Many Requests with code TOO_MANY_REQUESTS.

Source

pub fn service_unavailable(message: impl Into<Cow<'static, str>>) -> Self

503 Service Unavailable with code SERVICE_UNAVAILABLE.

Source

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

Source

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.

Source

pub fn with_code(self, code: impl Into<Cow<'static, str>>) -> Self

Replaces the machine-readable error code.

Source

pub fn with_message(self, message: impl Into<Cow<'static, str>>) -> Self

Replaces the public message.

Source

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

Source

pub fn with_source(self, source: impl Into<BoxError>) -> Self

Attaches a diagnostic source error. It is logged, never sent to clients.

Source

pub fn with_header(self, name: HeaderName, value: HeaderValue) -> Self

Adds a response header (for example Retry-After or WWW-Authenticate).

Source

pub fn status(&self) -> StatusCode

The HTTP status code.

Source

pub fn code(&self) -> &str

The machine-readable error code.

Source

pub fn message(&self) -> &str

The public, client-visible message.

Source

pub fn details(&self) -> Option<&Value>

Client-visible structured details, if any.

Source

pub fn headers(&self) -> &[(HeaderName, HeaderValue)]

Extra response headers attached to this error.

Source

pub fn source_ref(&self) -> Option<&(dyn Error + Send + Sync + 'static)>

The diagnostic source, if any.

Source

pub fn to_json_body(&self) -> Vec<u8> ⓘ

Serializes the client-visible JSON body.

Trait Implementations§

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 From<ConfigError> for Error

Source§

fn from(err: ConfigError) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl From<ServerError> for Error

Source§

fn from(err: ServerError) -> Self

Converts to this type from the input type.
Source§

impl From<StatusCode> for Error

Source§

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.

Source§

impl IntoResponse for Error

Source§

fn into_response(self) -> Response

Create a response.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Error

§

impl !UnwindSafe for Error

§

impl Forget for Error

§

impl Freeze for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnsafeUnpin 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more