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>>,
) -> Error
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.
Sourcepub fn bad_request(message: impl Into<Cow<'static, str>>) -> Error
pub fn bad_request(message: impl Into<Cow<'static, str>>) -> Error
400 Bad Request with code BAD_REQUEST.
401 Unauthorized with code UNAUTHORIZED.
Sourcepub fn forbidden(message: impl Into<Cow<'static, str>>) -> Error
pub fn forbidden(message: impl Into<Cow<'static, str>>) -> Error
403 Forbidden with code FORBIDDEN.
Sourcepub fn not_found(message: impl Into<Cow<'static, str>>) -> Error
pub fn not_found(message: impl Into<Cow<'static, str>>) -> Error
404 Not Found with code NOT_FOUND.
Sourcepub fn method_not_allowed(message: impl Into<Cow<'static, str>>) -> Error
pub fn method_not_allowed(message: impl Into<Cow<'static, str>>) -> Error
405 Method Not Allowed with code METHOD_NOT_ALLOWED.
Sourcepub fn timeout(message: impl Into<Cow<'static, str>>) -> Error
pub fn timeout(message: impl Into<Cow<'static, str>>) -> Error
408 Request Timeout with code REQUEST_TIMEOUT.
Sourcepub fn conflict(message: impl Into<Cow<'static, str>>) -> Error
pub fn conflict(message: impl Into<Cow<'static, str>>) -> Error
409 Conflict with code CONFLICT.
Sourcepub fn payload_too_large(message: impl Into<Cow<'static, str>>) -> Error
pub fn payload_too_large(message: impl Into<Cow<'static, str>>) -> Error
413 Payload Too Large with code PAYLOAD_TOO_LARGE.
Sourcepub fn unsupported_media_type(message: impl Into<Cow<'static, str>>) -> Error
pub fn unsupported_media_type(message: impl Into<Cow<'static, str>>) -> Error
415 Unsupported Media Type with code UNSUPPORTED_MEDIA_TYPE.
Sourcepub fn unprocessable(message: impl Into<Cow<'static, str>>) -> Error
pub fn unprocessable(message: impl Into<Cow<'static, str>>) -> Error
422 Unprocessable Entity with code UNPROCESSABLE_ENTITY.
Sourcepub fn too_many_requests(message: impl Into<Cow<'static, str>>) -> Error
pub fn too_many_requests(message: impl Into<Cow<'static, str>>) -> Error
429 Too Many Requests with code TOO_MANY_REQUESTS.
503 Service Unavailable with code SERVICE_UNAVAILABLE.
Sourcepub fn validation(details: impl Serialize) -> Error
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).
Sourcepub fn internal(source: impl Into<Box<dyn Error + Send + Sync>>) -> Error
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.
Sourcepub fn with_code(self, code: impl Into<Cow<'static, str>>) -> Error
pub fn with_code(self, code: impl Into<Cow<'static, str>>) -> Error
Replaces the machine-readable error code.
Sourcepub fn with_message(self, message: impl Into<Cow<'static, str>>) -> Error
pub fn with_message(self, message: impl Into<Cow<'static, str>>) -> Error
Replaces the public message.
Sourcepub fn with_details(self, details: impl Serialize) -> Error
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).
Sourcepub fn with_source(
self,
source: impl Into<Box<dyn Error + Send + Sync>>,
) -> Error
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.
Sourcepub fn with_header(self, name: HeaderName, value: HeaderValue) -> Error
pub fn with_header(self, name: HeaderName, value: HeaderValue) -> Error
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) -> Error
fn from(err: ConfigError) -> Error
Source§impl From<ServerError> for Error
impl From<ServerError> for Error
Source§fn from(err: ServerError) -> Error
fn from(err: ServerError) -> Error
Source§impl From<StatusCode> for Error
impl From<StatusCode> for Error
Source§fn from(status: StatusCode) -> Error
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
impl IntoResponse for Error
Source§fn into_response(self) -> Response<Body>
fn into_response(self) -> Response<Body>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn compression(self) -> Compression<Self>where
Self: Sized,
fn compression(self) -> Compression<Self>where
Self: Sized,
compression-br or compression-deflate or compression-gzip or compression-zstd only.Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
request-id only.Source§fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
request-id only.x-request-id as the header name. Read moreSource§fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
request-id only.Source§fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
request-id only.x-request-id as the header name. Read more