Error handling
By default, the framework handles most situations where errors occur, displaying an error page to the user when 4xx and 5xx HTTP exceptions are thrown, with a backtrace and code preview when the debug mode is enabled.
In this recipe, we'll see how you can customize this experience for your users and also some more advanced techniques to gain full control over error handling.
Getting ready
As we'll simply use this recipe to demonstrate a custom exception, let's create a controller that will serve as an example. So, create a file named BrokenController.php
in app/Controller/
, and introduce the following code in it:
<?php App::uses('AppController', 'Controller'); class BrokenController extends AppController { }
How to do it...
Perform the following steps:
Create a file named
CustomException.php
inapp/Error/
with the following content:<?php class CustomException extends HttpException { public function __construct($message = null, $code = 417, Exception $previous = null) { ...