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>>, ) -> Error

Creates an error with an explicit status, machine-readable code and public message.

Source

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

400 Bad Request with code BAD_REQUEST.

Source

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

401 Unauthorized with code UNAUTHORIZED.

Source

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

403 Forbidden with code FORBIDDEN.

Source

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

404 Not Found with code NOT_FOUND.

Source

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

405 Method Not Allowed with code METHOD_NOT_ALLOWED.

Source

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

408 Request Timeout with code REQUEST_TIMEOUT.

Source

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

409 Conflict with code CONFLICT.

Source

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

413 Payload Too Large with code PAYLOAD_TOO_LARGE.

Source

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

415 Unsupported Media Type with code UNSUPPORTED_MEDIA_TYPE.

Source

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

422 Unprocessable Entity with code UNPROCESSABLE_ENTITY.

Source

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

429 Too Many Requests with code TOO_MANY_REQUESTS.

Source

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

503 Service Unavailable with code SERVICE_UNAVAILABLE.

Source

pub fn validation(details: impl Serialize) -> Error

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<Box<dyn Error + Send + Sync>>) -> Error

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

Replaces the machine-readable error code.

Source

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

Replaces the public message.

Source

pub fn with_details(self, details: impl Serialize) -> Error

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<Box<dyn Error + Send + Sync>>, ) -> Error

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

Source

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

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<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

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

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(err: Error) -> Error

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(err: Error) -> Error

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(err: Error) -> Error

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(error: Error) -> Error

Converts to this type from the input type.
Source§

impl From<ServerError> for Error

Source§

fn from(err: ServerError) -> Error

Converts to this type from the input type.
Source§

impl From<StatusCode> for Error

Source§

fn from(status: StatusCode) -> Error

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<Body>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ServiceExt for T

Source§

fn compression(self) -> Compression<Self>
where Self: Sized,

Available on crate features compression-br or compression-deflate or compression-gzip or compression-zstd only.
Compresses response bodies. Read more
Source§

fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>
where Self: Sized,

Available on crate feature trace only.
High level tracing that classifies responses using HTTP status codes. Read more
Source§

fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>
where Self: Sized,

Available on crate feature trace only.
High level tracing that classifies responses using gRPC headers. Read more
Source§

fn set_request_id<M>( self, header_name: HeaderName, make_request_id: M, ) -> SetRequestId<Self, M>
where Self: Sized, M: MakeRequestId,

Available on crate feature request-id only.
Add request id header and extension. Read more
Source§

fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>
where Self: Sized, M: MakeRequestId,

Available on crate feature request-id only.
Add request id header and extension, using x-request-id as the header name. Read more
Source§

fn propagate_request_id( self, header_name: HeaderName, ) -> PropagateRequestId<Self>
where Self: Sized,

Available on crate feature request-id only.
Propgate request ids from requests to responses. Read more
Source§

fn propagate_x_request_id(self) -> PropagateRequestId<Self>
where Self: Sized,

Available on crate feature request-id only.
Propgate request ids from requests to responses, using x-request-id as the header name. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ValidateIp for T
where T: ToString,

Source§

fn validate_ipv4(&self) -> bool

Validates whether the given string is an IP V4
Source§

fn validate_ipv6(&self) -> bool

Validates whether the given string is an IP V6
Source§

fn validate_ip(&self) -> bool

Validates whether the given string is an IP
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