Two different things go wrong in an Inertia application: a handler decides the answer is an error, or a prop fails after the handler has already returned. In this chapter we'll render an error as a page, see what the crate does with a failing prop, and look at the two ways to keep one failure from taking the page down.
This chapter assumes Chapter 6, since the failures here happen in prop callbacks. It does not cover validation errors; those are Chapter 4.
An error is a page with a status
The report prop trusted its upstream service. The service was down. The handler had returned by then, satisfied, with a callback still to run. The callback ran during rendering, failed, and the customer received three words: Internal Server Error.
Sometimes the right response to a request is an error the user can read. Thankfully, an error page is an ordinary page returned with a status code. Take a look at Example 9-1.
Example 9-1. A 404 that is still an Inertia page
pub async fn missing(inertia: Inertia) -> Response {
(StatusCode::NOT_FOUND, inertia.render("Error", props! { status => 404 })).into_response()
}
There is one new piece here. A tuple of a status and a render is a response, so the client receives a 404 whose body is a page object for the Error component, shared props included.
When a prop fails
A prop callback may return a Result. Example 9-2 has one that always fails.
Example 9-2. A prop that fails
async fn load_report() -> Result<u32, std::io::Error> {
Err(std::io::Error::other("The report service is down"))
}
pub async fn report(inertia: Inertia) -> inertia::Response {
inertia.render("Reports/Show", props! { "report" => inertia::try_lazy(load_report) })
}
try_lazy takes a callback that returns a Result. When it returns an error, the whole response fails: the layer logs the error and replies 500 with a plain-text body. No page is rendered, so no error component is shown.
You can also rescue a prop. Add .rescue(), and its failure is reported in a rescuedProps field on the page while the rest of the page goes out as normal (see Example 9-3).
Example 9-3. A rescued, deferred prop
props! { "title" => "Q4", "report" => inertia::try_lazy(load_report).deferred().rescue() }
A partial reload asking for report gets a 200 with this page:
{"component":"Reports/Show","props":{"errors":{}},"url":"/report-rescued","version":"1",
"sharedProps":["errors","app_name","auth"],"rescuedProps":["report"]}
The second way is to replace the 500. InertiaLayer::handle_with runs the layer's logic with a callback for a failed prop, and that callback may return a page render, such as your own error page. The crate's test suite exercises this; this chapter's listings do not. If you're unsure which to reach for, I'd rescue the props a page can live without and leave the rest to fail loudly.
What this chapter leaves open
So a rescued prop keeps the page alive, and the response is a 200. Your monitoring counts status codes. What tells you the report service has been down since morning? Does anything on the client read rescuedProps, and what does the user see where the report should be?
Testing
Example 9-4 fails if a prop error starts producing a page, or stops producing a 500.
Example 9-4. A failing prop
let reply = send(&app(), visit("GET", "/report").body(Body::empty()).unwrap()).await;
assert_eq!(reply.status, StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(reply.body, "Internal Server Error");
TL;DR
An error page is a render returned with a status code. A prop callback that returns an error fails the whole response with a logged 500, unless the prop is rescued, in which case the page is sent and the prop's name appears in rescuedProps. handle_with lets you answer a failed prop with a response of your own.