Skip to content

Commit

Permalink
#37 - add custom rendering for exceptions (#65)
Browse files Browse the repository at this point in the history
* add custom rendering for exceptions

* add symfony response status codes

* move error codes into array
  • Loading branch information
PrabuckiDominik authored Aug 6, 2024
1 parent 5a3f5eb commit 4cad7db
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
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>

0 comments on commit 4cad7db

Please sign in to comment.