Search⌘ K
AI Features

Displaying an Error Page

Explore how to build a custom error page in PHP by modifying exception handlers. Learn to log errors properly, set correct HTTP status codes, and control error display based on environment settings. This lesson equips you to handle errors gracefully while retaining useful debugging information.

Creating an error script

Let’s improve the exception handler by showing the error page. First, create a new page script, pages/error.php. Paste the following code in it:

PHP
<?php
$title = 'Error';
include(__DIR__ . '/../_header.php');
?>
<h1>An error occurred</h1>
<?php
include(__DIR__ . '/../_footer.php');

We don’t need to add a URL for it to the $urlMap because the error page isn’t a normal page. We will only show it when we catch an exception. To do this, we have to modify the call to set_exception_handler() in bootstrap.php:

PHP
<?php
set_exception_handler(
function (Throwable $exception) {
include(__DIR__ . '/pages/error.php');
}
);
session_start();

Go to http://APPLINK/oops, and see the error page that it now shows:

For regular users, this is good, but for developers, this isn’t great. For us, it is important to know what exactly went wrong.

Note: You don’t have to go anywhere and open any URL as we have set things up for you on our platform. We have used http://APPLINK just as a placeholder. The URL to view the application is different on our platform. ...