Book Image

CakePHP 2 Application Cookbook

Book Image

CakePHP 2 Application Cookbook

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

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:

  1. Create a file named CustomException.php in app/Error/ with the following content:

    <?php
    class CustomException extends HttpException {
      public function __construct($message = null, $code = 417, Exception $previous = null) {
    ...