Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#37 - add custom rendering for exceptions #65

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Symfony\Component\HttpFoundation\Response;

const HTTP_SESSION_EXPIRED = 419;
const HANDLED_ERROR_CODES = [
Response::HTTP_FORBIDDEN,
Response::HTTP_INTERNAL_SERVER_ERROR,
Response::HTTP_SERVICE_UNAVAILABLE,
Response::HTTP_NOT_FOUND,
];

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
Expand All @@ -16,4 +27,17 @@
->withMiddleware(function (Middleware $middleware): void {
})
->withExceptions(function (Exceptions $exceptions): void {
$exceptions->respond(function (Response $response, Throwable $exception, Request $request): Response {
if (!app()->environment(["local", "testing"]) && in_array($response->getStatusCode(), HANDLED_ERROR_CODES, true)) {
return Inertia::render("Errors/Error", ["status" => $response->getStatusCode()])
->toResponse($request)
->setStatusCode($response->getStatusCode());
} elseif ($response->getStatusCode() === HTTP_SESSION_EXPIRED) {
return back()->with([
"message" => "The page expired, please try again.",
]);
}

return $response;
});
})->create();
43 changes: 43 additions & 0 deletions resources/js/Pages/Errors/Error.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script setup lang="ts">
import GuestLayout from '@/Layouts/GuestLayout.vue'
import { Head } from '@inertiajs/vue3'
import { computed } from 'vue'
const props = defineProps<{
status: number
}>()
const title = computed(() => {
return {
503: '503: Service Unaviable',
500: '500: Server Error',
404: '404: Page Not Found',
403: '403: Forbidden',
}[props.status]
})
const description = computed(() => {
return {
503: 'Sorry, we are doing some maintenance. Please check back soon.',
500: 'Whoops, something went wrong on our servers.',
404: 'Sorry, the page you are looking for could not be found.',
403: 'Sorry, you are forbidden from accessing this page.',
}[props.status]
})
</script>

<template>
<Head title="Error" />
<div class="flex flex-col justify-between h-full items-center">
<h2 class="text-2xl text-center"> {{ title }}</h2>
<p class="text-center my-auto">{{ description }}</p>
<a class="bg-blue-800 rounded-md block text-center text-white w-5/6 py-3 hover:bg-blue-700" href="/">Back</a>
</div>
</template>

<script lang="ts">
export default {
layout: GuestLayout,
}
</script>
Loading