Struct axum_route_error::RouteError
source · pub struct RouteError<S = (), const EXPOSE_INTERNAL_ERROR: bool = false>{ /* 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<()>
impl RouteError<()>
pub fn new_not_found() -> RouteError<()>
pub fn new_bad_request() -> RouteError<()>
pub fn new_internal_server() -> RouteError<()>
pub fn new_conflict() -> RouteError<()>
pub fn new_forbidden() -> RouteError<()>
pub fn new_from_status(status_code: StatusCode) -> RouteError<()>
source§impl<S, const EXPOSE_INTERNAL_ERROR: bool> RouteError<S, EXPOSE_INTERNAL_ERROR>
impl<S, const EXPOSE_INTERNAL_ERROR: bool> RouteError<S, EXPOSE_INTERNAL_ERROR>
sourcepub fn set_status_code(self, status_code: StatusCode) -> Self
pub fn set_status_code(self, status_code: StatusCode) -> Self
Set a new status code for the error response.
sourcepub fn set_error(self, error: AnyhowError) -> Self
pub fn set_error(self, error: AnyhowError) -> Self
Set an internal error.
This is used for tracking the source of the error internally.
sourcepub fn set_error_data<NewS>(self, extra_data: NewS) -> RouteError<NewS>
pub fn set_error_data<NewS>(self, extra_data: NewS) -> RouteError<NewS>
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>"
}
sourcepub fn set_public_error_message(self, public_error_message: &str) -> Self
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.
sourcepub fn public_error_message<'a>(&'a self) -> &'a str
pub fn public_error_message<'a>(&'a self) -> &'a str
Returns the error message that will be shown to the end user.
sourcepub fn status_code(&self) -> StatusCode
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>
impl<S, const EXPOSE_INTERNAL_ERROR: bool> Debug for RouteError<S, EXPOSE_INTERNAL_ERROR>
source§impl<S, const EXPOSE_INTERNAL_ERROR: bool> Default for RouteError<S, EXPOSE_INTERNAL_ERROR>
impl<S, const EXPOSE_INTERNAL_ERROR: bool> Default for RouteError<S, EXPOSE_INTERNAL_ERROR>
source§impl<S, const EXPOSE_INTERNAL_ERROR: bool> Display for RouteError<S, EXPOSE_INTERNAL_ERROR>
impl<S, const EXPOSE_INTERNAL_ERROR: bool> Display for RouteError<S, EXPOSE_INTERNAL_ERROR>
source§impl<S, const EXPOSE_INTERNAL_ERROR: bool, FE> From<FE> for RouteError<S, EXPOSE_INTERNAL_ERROR>
impl<S, const EXPOSE_INTERNAL_ERROR: bool, FE> From<FE> for RouteError<S, EXPOSE_INTERNAL_ERROR>
This essentially means if you can turn it into an Anyhow, then you can turn it into a RouteError.