pub struct Error { /* private fields */ }Expand description
An error type that can act as a specialized version of a
ResponseBuilder.
Implementations§
Source§impl Error
impl Error
Sourcepub fn new(source: Box<dyn Error + Send + Sync>) -> Self
pub fn new(source: Box<dyn Error + Send + Sync>) -> Self
Create a new Error from the provided source.
Sourcepub fn redact(self, f: impl FnOnce(&str) -> Option<String>) -> Self
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(())
}Sourcepub fn with_message(self, message: String) -> Self
pub fn with_message(self, message: String) -> Self
Sourcepub fn with_status(self, status: StatusCode) -> Self
pub fn with_status(self, status: StatusCode) -> Self
Sets the status code of that will be used when converted to a
Response.
Sourcepub fn use_canonical_reason(self) -> Self
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§impl Error
impl Error
Trait Implementations§
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> 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
Mutably borrows from an owned value. Read more