Struct Error

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

An error type that can act as a specialized version of a ResponseBuilder.

Implementations§

Source§

impl Error

Source

pub fn new(source: Box<dyn Error + Send + Sync>) -> Self

Create a new Error from the provided source.

Source

pub fn from_io(source: Error) -> Self

Create a new Error from the provided io::Error. The status code of the error returned will correspond to source.kind().

Source

pub fn as_json(self) -> Self

Returns a new Error that will be serialized to JSON when converted to a Response.

Source

pub fn redact(self, f: impl FnOnce(&str) -> Option<String>) -> Self

Returns a new Error that will eagerly map the message that will be included in the body of the Response that will be generated from self by calling the provided closure. If the closure returns None, the message will be left unchanged.

§Example
use via::middleware::error_boundary;
use via::{Next, Request};

type Error = Box<dyn std::error::Error + Send + Sync>;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let mut app = via::app(());

    // Add an `ErrorBoundary` middleware to the route tree that maps
    // errors that occur in subsequent middleware by calling the `redact`
    // function.
    app.include(error_boundary::map(|_, error| {
        error.redact(|message| {
            if message.contains("password") {
                // If password is even mentioned in the error, return an
                // opaque message instead. You'll probably want something
                // more sophisticated than this in production.
                Some("An error occurred...".to_owned())
            } else {
                // Otherwise, use the existing error message.
                None
            }
        })
    }));

    Ok(())
}
Source

pub fn with_message(self, message: String) -> Self

Returns a new Error that will use the provided message instead of calling the Display implementation of the error source when converted to a Response.

Source

pub fn with_status(self, status: StatusCode) -> Self

Sets the status code of that will be used when converted to a Response.

Source

pub fn use_canonical_reason(self) -> Self

Returns a new Error that will use the canonical reason phrase of the status code as the message included in the Response body that is generated when converted to a Response.

§Example
use via::middleware::error_boundary;
use via::{Next, Request};

type Error = Box<dyn std::error::Error + Send + Sync>;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Error> {
    let mut app = via::app(());

    // Add an `ErrorBoundary` middleware to the route tree that maps
    // errors that occur in subsequent middleware by calling the
    // `use_canonical_reason` function.
    app.include(error_boundary::map(|_, error| {
        // Prevent error messages that occur in downstream middleware from
        // leaking into the response body by using the reason phrase of
        // the status code associated with the error.
        error.use_canonical_reason()
    }));

    Ok(())
}
Source

pub fn iter(&self) -> impl Iterator<Item = &dyn StdError>

Returns an iterator over the sources of this error.

Source

pub fn source(&self) -> &(dyn StdError + 'static)

Returns a reference to the error source.

Source§

impl Error

Source

pub fn bad_request(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 400 Bad Request status.

Source

pub fn unauthorized(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 401 Unauthorized status.

Source

pub fn payment_required(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 402 Payment Required status.

Source

pub fn forbidden(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 403 Forbidden status.

Source

pub fn not_found(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 404 Not Found status.

Source

pub fn method_not_allowed(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 405 Method Not Allowed status.

Source

pub fn not_acceptable(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 406 Not Acceptable status.

Source

pub fn proxy_authentication_required( source: Box<dyn Error + Send + Sync>, ) -> Self

Returns a new Error from the provided source that will generate a Response with a 407 Proxy Authentication Required status.

Source

pub fn request_timeout(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 408 Request Timeout status.

Source

pub fn conflict(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 409 Conflict status.

Source

pub fn gone(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 410 Gone status.

Source

pub fn length_required(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 411 Length Required status.

Source

pub fn precondition_failed(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 412 Precondition Failed status.

Source

pub fn payload_too_large(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 413 Payload Too Large status.

Source

pub fn uri_too_long(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 414 URI Too Long status.

Source

pub fn unsupported_media_type(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 415 Unsupported Media Type status.

Source

pub fn range_not_satisfiable(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 416 Range Not Satisfiable status.

Source

pub fn expectation_failed(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 417 Expectation Failed status.

Source

pub fn im_a_teapot(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 418 I'm a teapot status.

Source

pub fn misdirected_request(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 421 Misdirected Request status.

Source

pub fn unprocessable_entity(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 422 Unprocessable Entity status.

Source

pub fn locked(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 423 Locked status.

Source

pub fn failed_dependency(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 424 Failed Dependency status.

Source

pub fn upgrade_required(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 426 Upgrade Required status.

Source

pub fn precondition_required(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 428 Precondition Required status.

Source

pub fn too_many_requests(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 429 Too Many Requests status.

Source

pub fn request_header_fields_too_large( source: Box<dyn Error + Send + Sync>, ) -> Self

Returns a new Error from the provided source that will generate a Response with a 431 Request Header Fields Too Large status.

Returns a new Error from the provided source that will generate a Response with a 451 Unavailable For Legal Reasons status.

Source

pub fn internal_server_error(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 500 Internal Server Error status.

Source

pub fn not_implemented(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 501 Not Implemented status.

Source

pub fn bad_gateway(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 502 Bad Gateway status.

Source

pub fn service_unavailable(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 503 Service Unavailable status.

Source

pub fn gateway_timeout(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 504 Gateway Timeout status.

Source

pub fn http_version_not_supported(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 505 HTTP Version Not Supported status.

Source

pub fn variant_also_negotiates(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 506 Variant Also Negotiates status.

Source

pub fn insufficient_storage(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 507 Insufficient Storage status.

Source

pub fn loop_detected(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 508 Loop Detected status.

Source

pub fn not_extended(source: Box<dyn Error + Send + Sync>) -> Self

Returns a new Error from the provided source that will generate a Response with a 510 Not Extended status.

Source

pub fn network_authentication_required( source: Box<dyn Error + Send + Sync>, ) -> Self

Returns a new Error from the provided source that will generate a Response with a 511 Network Authentication Required status.

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

Source§

fn from(error: Error) -> Response

Converts to this type from the input type.
Source§

impl<T> From<T> for Error
where T: StdError + Send + Sync + 'static,

Source§

fn from(source: T) -> Self

Converts to this type from the input type.
Source§

impl Serialize for Error

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Error

§

impl !RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl !UnwindSafe 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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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