exception handling in cakephp


Posted on 2015-01-19 08:49:17


Exceptions is very important thing in application. It is for indication of logic errors or misuse. CakePHP extend CakeException for all of the exceptions, and there are class/task specific exceptions that extend this base class. CakePHP has a number of exception classes that can be use for HTTP errors. Some built in exceptions in cake php are given below
  1. exception BadRequestException Used for doing 400 Bad Request error.
  2. exception ForbiddenException Used for doing a 403 Forbidden error.
  3. exception NotFoundException Used for doing a 404 Not found error.
  4. exception MethodNotAllowedException Used for doing a 405 Method Not Allowed error.
  5. exception InternalErrorException Used for doing a 500 Internal Server Error.
  6. exception NotImplementedException Used for doing a 501 Not Implemented Errors.
In this article we will discuss that how can we manage and design various exceptions and errors in cakephp 1. First of all we need to update /app/Config/core.php file in cake installation. In core.php configuration of exception should be available, we need to update ‘renderer’ => ‘AppExceptionRenderer’.
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'AppExceptionRenderer',
'log' => true
));
2. We need to add AppExceptionRenderer. To achieve this create /app/Lib/Error/AppExceptionRenderer.php and paste following code.
 App::uses('ExceptionRenderer', 'Error');

class AppExceptionRenderer extends ExceptionRenderer {

public function notFound($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Not Found');
$this->controller->render('/Errors/error404');
$this->controller->response->send();
}
public function badRequest($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', Bad Request');
$this->controller->render('/Errors/error400');
$this->controller->response->send();
}
public function forbidden($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', Forbidden Access');
$this->controller->render('/Errors/error403');
$this->controller->response->send();
}
public function methodNotAllowed($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', Not Allowed');
$this->controller->render('/Errors/error405');
$this->controller->response->send();
}
public function internalError($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Internal Server Error');
$this->controller->render('/Errors/error500');
$this->controller->response->send();
}
public function notImplemented($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Method not implemented');
$this->controller->render('/Errors/error501');
$this->controller->response->send();
}

public function missingController($error) {
$this->notFound($error);
}
public function missingAction($error) {
$this->notFound($error);
}
public function missingView($error) {
$this->notFound($error);
}
public function missingLayout($error) {
$this->internalError($error);
}
public function missingHelper($error) {
$this->internalError($error);
}
public function missingBehavior($error) {
$this->internalError($error);
}
public function missingComponent($error) {
$this->internalError($error);
}
public function missingTask($error) {
$this->internalError($error);
}
public function missingShell($error) {
$this->internalError($error);
}
public function missingShellMethod($error) {
$this->internalError($error);
}
public function missingDatabase($error) {
$this->internalError($error);
}
public function missingConnection($error) {
$this->internalError($error);
}
public function missingTable($error) {
$this->internalError($error);
}
public function privateAction($error) {
$this->internalError($error);
}

}
3. For customized view we need view files for customization. Here are some files, it is basically error code in fienames.
/app/View/Errors/error400.ctp
/app/View/Errors/error403.ctp
/app/View/Errors/error404.ctp
/app/View/Errors/error405.ctp
/app/View/Errors/error500.ctp
/app/View/Errors/error501.ctp