pub struct RouteError<S = (), const EXPOSE_INTERNAL_ERROR: bool = false>
where S: Serialize + for<'a> Deserialize<'a> + Debug,
{ /* private fields */ }
Expand description

This Rust module provides a standard error type for routes. It encapsulates information about errors that occur while handling requests. It includes a status code, error details, any extra data, and a public error message.

It includes the means to output these as Json for the user.

The output will be in the form:

    {
        "error": "My public error message"
    }

Most of the time you will want to simply return one of:

  • RouteError::new_unauthorised()
  • RouteError::new_not_found()
  • RouteError::new_bad_request()
  • RouteError::new_internal_server()

Depending on which is the most appropriate.

Implementations§

source§

impl RouteError<()>

source§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: Serialize + for<'a> Deserialize<'a> + Debug,

source

pub fn set_status_code(self, status_code: StatusCode) -> Self

Set a new status code for the error response.

source

pub fn set_error(self, error: AnyhowError) -> Self

Set an internal error.

This is used for tracking the source of the error internally.

source

pub fn set_error_data<NewS>(self, extra_data: NewS) -> RouteError<NewS>
where NewS: Serialize + for<'a> Deserialize<'a> + Debug,

Sets additional error data to be added to the output. Data here must be serialisable into Json.

Example Code
use ::axum_route_error::RouteError;
use ::serde::Deserialize;
use ::serde::Serialize;

#[derive(Deserialize, Serialize, Debug)]
pub struct UserErrorInformation {
    pub guid: String
}

let guid = "abc123".to_string();
let err = RouteError::new_not_found()
    .set_error_data(UserErrorInformation {
        guid,
    });

This will return a response with the JSON format:

{
  "error": "The resource was not found",
  "username": "<the-username>"
}
source

pub fn set_public_error_message(self, public_error_message: &str) -> Self

Set the error message to display within the error.

If this is not set, then an appropriate message is provided based on the status code.

source

pub fn public_error_message<'a>(&'a self) -> &'a str

Returns the error message that will be shown to the end user.

source

pub fn status_code(&self) -> StatusCode

Returns the status code for the response.

Trait Implementations§

source§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> Debug for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: Serialize + for<'a> Deserialize<'a> + Debug,

source§

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

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

impl<S, const EXPOSE_INTERNAL_ERROR: bool> Default for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: Serialize + for<'a> Deserialize<'a> + Debug,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> Display for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: Serialize + for<'a> Deserialize<'a> + Debug,

source§

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

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

impl<S, const EXPOSE_INTERNAL_ERROR: bool, FE> From<FE> for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: Serialize + for<'a> Deserialize<'a> + Debug, FE: Into<Error>,

This essentially means if you can turn it into an Anyhow, then you can turn it into a RouteError.

source§

fn from(error: FE) -> Self

Converts to this type from the input type.
source§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> IntoResponse for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: Serialize + for<'a> Deserialize<'a> + Debug,

source§

fn into_response(self) -> Response

Create a response.

Auto Trait Implementations§

§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> RefUnwindSafe for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: RefUnwindSafe,

§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> Send for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: Send,

§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> Sync for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: Sync,

§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> Unpin for RouteError<S, EXPOSE_INTERNAL_ERROR>

§

impl<S, const EXPOSE_INTERNAL_ERROR: bool> UnwindSafe for RouteError<S, EXPOSE_INTERNAL_ERROR>
where S: UnwindSafe,

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

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

§

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

§

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