Inertia - Status code on exception #4563
-
Hey, the inertia doc mention this: use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Inertia\Inertia;
->withExceptions(function (Exceptions $exceptions) {
$exceptions->respond(function (Response $response, Throwable $exception, Request $request) {
if (! app()->environment(['local', 'testing']) && in_array($response->getStatusCode(), [500, 503, 404, 403])) {
return Inertia::render('Error', ['status' => $response->getStatusCode()])
->toResponse($request)
->setStatusCode($response->getStatusCode());
} elseif ($response->getStatusCode() === 419) {
return back()->with([
'message' => 'The page expired, please try again.',
]);
}
return $response;
});
}) What would be the recommended way to return, for example, a 404 status code? |
Beta Was this translation helpful? Give feedback.
Answered by
Julien-R44
May 7, 2024
Replies: 1 comment
-
If you're using the inertia starter-kit then it's handled automatically for you in the Exception Handler. See there : https://github.com/adonisjs/inertia-starter-kit/blob/main/app/exceptions/handler.ts#L24 If you throw an exception with a 404 status code, your error page will be rendered with Inertia. For example, this is the case with Lucid's export default class UsersController {
async index({ inertia, response }: HttpContext) {
const user = await User.findOrFail(1212121);
// ...
}
} If the user is not found, then :
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
RomainLanz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're using the inertia starter-kit then it's handled automatically for you in the Exception Handler. See there : https://github.com/adonisjs/inertia-starter-kit/blob/main/app/exceptions/handler.ts#L24
If you throw an exception with a 404 status code, your error page will be rendered with Inertia. For example, this is the case with Lucid's
findxxOrFail
methods:If the user is not found, then :
404
as seen in the linked code.