Adding Custom Exception Views to Laravel

There are a number of views already created in Blade for when exceptions occur but as you might expect, you can alter these to be just how you require.

Editing the files directly is possible, but there is a better way which only takes a minute.

Let’s start by creating a blank project

> laravel new exceptions

Wait for that to finish and then run the in-built server so that you can see your shiny new website.

> php artisan serve

Great! Now let’s generate an exception. We can do this easily by placing the site into maintenance mode so go ahead and do that now.

> php artisan down

Go back to your home page URL and see the exception being shown in a view.

Now time to change it. Open up the file:

vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views/503.blade.php

We aren’t going to change this - just select all the text and copy it into the clipboard.

Next, create a file in resources/views/errors named: 503.blade.php. Note, you may have to create the errors folder.

Paste the text in that you copied and alter the message:

Before:

1
2
@extends('errors::minimal')
@section('title', <strong>('Service Unavailable')) @section('code', '503') @section('message',</strong> ($exception->getMessage() ?: 'Service Unavailable'))

After:

1
2
@extends('errors::minimal')
@section('title', <strong>('Service Unavailable')) @section('code', '503') @section('message',</strong> ($exception->getMessage() ?: 'Sorry…site is in maintenance mode. Back soon.'))

Save this and revisit the homepage of your Laravel website. Tada!


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com