pub struct Errors { /* private fields */ }Expand description
How the application reports and renders errors. You configure it with
ApplicationBuilder::with_errors,
and service providers configure it with
Registrar::errors.
Later registrations take precedence. The application’s registrations run after every provider’s, so they replace a package’s status or renderer for a type.
By default, the error handler logs server errors. Errors render as JSON for requests that
expect JSON, and as an HTML page otherwise, with full details when APP_DEBUG is on.
use laravel::errors::Errors;
use laravel::http::{IntoResponse, StatusCode};
#[derive(Debug)]
struct PaymentFailed;
impl std::fmt::Display for PaymentFailed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("The payment failed")
}
}
impl std::error::Error for PaymentFailed {}
fn configure(errors: &mut Errors) {
errors
.status::<PaymentFailed>(StatusCode::PAYMENT_REQUIRED)
.render(|_: &PaymentFailed, _request, status| {
Some((status, "Please update your card"))
});
}Implementations§
Source§impl Errors
impl Errors
Sourcepub fn status<E>(&mut self, status: impl IntoStatusCode) -> &mut Errors
pub fn status<E>(&mut self, status: impl IntoStatusCode) -> &mut Errors
Respond to errors of a type with a status, as a number or a StatusCode, when ?
converts them. A status given when the error is created, such as with Error::new, takes
precedence. Errors with a client status, such as a 402, are expected outcomes. Laravel
doesn’t report them, and they never get the debug page.
fn configure(errors: &mut Errors) {
errors.status::<PaymentFailed>(402);
}§Panics
Panics when the number isn’t a valid status code.
Sourcepub fn render<E, R>(
&mut self,
render: impl Fn(&E, &Request, StatusCode) -> Option<R> + Send + Sync + 'static,
) -> &mut Errors
pub fn render<E, R>( &mut self, render: impl Fn(&E, &Request, StatusCode) -> Option<R> + Send + Sync + 'static, ) -> &mut Errors
Render errors of a type with the given function, instead of the default rendering. The
function receives the error, the request, and the error’s status. It returns a response, or
None to fall back to the next renderer for the type and then to the default rendering.
The error handler sends the response as it is, so give the response the status, and
render JSON when the request expects it. If your application uses
Inertia, render a page with request.inertia()?.render(..).
fn configure(errors: &mut Errors) {
errors.status::<CardDeclined>(402).render(|error: &CardDeclined, request, status| {
if request.expects_json() {
let body = Json(serde_json::json!({ "message": error.to_string() }));
return Some((status, body).into_response());
}
Some((status, "Please use another card").into_response())
});
}The error handler tries the renderers registered later first, so the application’s renderers replace a package’s.
Sourcepub fn report<E>(
&mut self,
report: impl Fn(&E) + Send + Sync + 'static,
) -> &mut Errors
pub fn report<E>( &mut self, report: impl Fn(&E) + Send + Sync + 'static, ) -> &mut Errors
Report errors of a type with the given function, such as to an error tracker, in addition to
logging them. The function receives only failures, which are errors with a 5xx status.
Errors with a client status, such as a 404, are expected outcomes, so Laravel doesn’t
report them.
A reporter that panics doesn’t stop the request. The error handler logs the panic and still sends the error response.
Sourcepub fn report_all(
&mut self,
report: impl Fn(&Error) + Send + Sync + 'static,
) -> &mut Errors
pub fn report_all( &mut self, report: impl Fn(&Error) + Send + Sync + 'static, ) -> &mut Errors
Report every failure with the given function, whatever the error’s type. As with
report, a reporter that panics doesn’t stop the request.
Laravel also logs failures at the error level, with the request’s context. A tracing
layer added with with_logging, such
as Sentry’s, therefore receives them without a reporter.
Sourcepub fn dont_report<E>(&mut self) -> &mut Errors
pub fn dont_report<E>(&mut self) -> &mut Errors
Never report errors of a type. The error handler neither logs them nor passes them to the reporters, but they still render as usual.
Sourcepub fn dont_flash<I, S>(&mut self, fields: I) -> &mut Errors
pub fn dont_flash<I, S>(&mut self, fields: I) -> &mut Errors
Never flash these input fields, named by dot-notation path, as old input. Laravel never
flashes password, password_confirmation, or current_password either. The list applies
when an invalid browser form redirects back, and to
Redirect::with_input.
fn configure(errors: &mut Errors) {
errors.dont_flash(["ssn", "card.cvc"]);
}Sourcepub fn respond(
&mut self,
respond: impl Fn(Response, &Error, &Request) -> Response + Send + Sync + 'static,
) -> &mut Errors
pub fn respond( &mut self, respond: impl Fn(Response, &Error, &Request) -> Response + Send + Sync + 'static, ) -> &mut Errors
Adjust every rendered error response, such as to render error pages with Inertia. The function receives the response, the error, and the request, and returns the response to send. The functions run in the order you register them, and each receives the previous function’s response.
fn configure(errors: &mut Errors) {
errors.respond(|response, _error, _request| {
response.with_header("cache-control", "no-store")
});
}Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Errors
impl !UnwindSafe for Errors
impl Forget for Errors
impl Freeze for Errorswhere
*const u8 is !null: Freeze,
PhantomData<(fn(&Error) -> Option<&(dyn Error + 'static)>, StatusCode)>: Freeze,
PhantomData<Arc<dyn Fn(&Error, &Request, StatusCode) -> Option<Response<Body>> + Sync + Send>>: Freeze,
PhantomData<Arc<dyn Fn(&Error) + Sync + Send>>: Freeze,
PhantomData<fn(&Error) -> bool>: Freeze,
PhantomData<Arc<dyn Fn(Response, &Error, &Request) -> Response + Sync + Send>>: Freeze,
PhantomData<String>: Freeze,
impl Move for Errors
impl Send for Errors
impl Sync for Errors
impl Unpin for Errors
impl UnsafeUnpin for Errorswhere
*const u8 is !null: UnsafeUnpin,
PhantomData<(fn(&Error) -> Option<&(dyn Error + 'static)>, StatusCode)>: UnsafeUnpin,
PhantomData<Arc<dyn Fn(&Error, &Request, StatusCode) -> Option<Response<Body>> + Sync + Send>>: UnsafeUnpin,
PhantomData<Arc<dyn Fn(&Error) + Sync + Send>>: UnsafeUnpin,
PhantomData<fn(&Error) -> bool>: UnsafeUnpin,
PhantomData<Arc<dyn Fn(Response, &Error, &Request) -> Response + Sync + Send>>: UnsafeUnpin,
PhantomData<String>: UnsafeUnpin,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more