Skip to main content

Error

Struct Error 

Source
pub struct Error { /* private fields */ }
Expand description

A handler/framework error carrying the HTTP status to respond with.

An Error bundles everything needed to render a failed request: the StatusCode, a message (sent as the response body), an optional source error, and headers to attach to the rendered response. Return one from any handler or extractor and the pipeline turns it into a Response for you via the IntoResponse impl.

Use the constructors (Error::bad_request, Error::not_found, Error::internal, or Error::new for an arbitrary status) and chain with_source / with_response_header as needed.

use churust_core::Error;
use http::StatusCode;

let err = Error::not_found("user 42 does not exist");
assert_eq!(err.status(), StatusCode::NOT_FOUND);
assert_eq!(err.message(), "user 42 does not exist");

Implementations§

Source§

impl Error

Source

pub fn new(status: StatusCode, message: impl Into<String>) -> Self

Create an error with an explicit status and message.

Prefer the named constructors (bad_request, not_found, internal) for the common cases; use new when you need any other status (e.g. 401 or 409).

use churust_core::Error;
use http::StatusCode;

let err = Error::new(StatusCode::CONFLICT, "already exists");
assert_eq!(err.status(), StatusCode::CONFLICT);
Source

pub fn bad_request(message: impl Into<String>) -> Self

Create a 400 Bad Request error — use for malformed input such as an unparseable path parameter or invalid query string.

use churust_core::Error;
use http::StatusCode;

assert_eq!(Error::bad_request("nope").status(), StatusCode::BAD_REQUEST);
Source

pub fn not_found(message: impl Into<String>) -> Self

Create a 404 Not Found error — use when a requested resource does not exist.

use churust_core::Error;
use http::StatusCode;

assert_eq!(Error::not_found("gone").status(), StatusCode::NOT_FOUND);
Source

pub fn internal(message: impl Into<String>) -> Self

Create a 500 Internal Server Error — use for unexpected server-side failures (e.g. a missing application-state dependency).

use churust_core::Error;
use http::StatusCode;

assert_eq!(Error::internal("boom").status(), StatusCode::INTERNAL_SERVER_ERROR);
Source

pub fn with_source(self, source: impl Error + Send + Sync + 'static) -> Self

Attach an underlying source error for diagnostics (exposed via std::error::Error::source). The source does not change the rendered response; it is for logging and error chaining. Returns self so it can be chained.

use churust_core::Error;
use std::error::Error as _;

let io = std::io::Error::new(std::io::ErrorKind::Other, "disk gone");
let err = Error::internal("write failed").with_source(io);
assert!(err.source().is_some());
Source

pub fn status(&self) -> StatusCode

The HTTP status this error renders to.

use churust_core::Error;
use http::StatusCode;

assert_eq!(Error::bad_request("x").status(), StatusCode::BAD_REQUEST);
Source

pub fn message(&self) -> &str

The human-readable message, used as the response body when rendered.

use churust_core::Error;

assert_eq!(Error::not_found("missing").message(), "missing");
Source

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

Attach a header to the response this error renders into (e.g. WWW-Authenticate on a 401). May be called repeatedly to add several headers; returns self for chaining.

use churust_core::Error;
use http::{StatusCode, header::WWW_AUTHENTICATE, HeaderValue};

let err = Error::new(StatusCode::UNAUTHORIZED, "login required")
    .with_response_header(WWW_AUTHENTICATE, HeaderValue::from_static("Bearer"));
assert_eq!(err.response_headers().len(), 1);
Source

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

The headers to apply when rendering this error to a Response. Returns an empty slice unless with_response_header was called.

use churust_core::Error;

assert!(Error::bad_request("x").response_headers().is_empty());

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 IntoResponse for Error

Source§

fn into_response(self) -> Response

Consume self and produce the Response to send.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Error

§

impl !UnwindSafe 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> 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> 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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