2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2008-08-22 01:52:37 +00:00
|
|
|
* Error handler
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-08-22 01:52:37 +00:00
|
|
|
* Provides Error Capturing for Framework errors.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-09-03 14:30:35 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
|
|
|
* @since CakePHP(tm) v 0.10.5.1732
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-11-08 02:54:07 +00:00
|
|
|
* Error Handler.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-09-02 20:39:09 +00:00
|
|
|
* Captures and handles all unhandled exceptions. Displays helpful framework errors when debug > 1.
|
2010-09-04 05:34:45 +00:00
|
|
|
* When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
|
|
|
|
* and it is a type that ErrorHandler does not know about it will be treated as a 500 error.
|
2010-09-02 20:39:09 +00:00
|
|
|
*
|
|
|
|
* ### Implementing application specific exception handling
|
|
|
|
*
|
|
|
|
* You can implement application specific exception handling in one of a few ways:
|
|
|
|
*
|
|
|
|
* - Create a AppController::appError();
|
|
|
|
* - Create an AppError class.
|
|
|
|
*
|
|
|
|
* #### Using AppController::appError();
|
|
|
|
*
|
|
|
|
* This controller method is called instead of the default exception handling. It receives the
|
|
|
|
* thrown exception as its only argument. You should implement your error handling in that method.
|
|
|
|
*
|
|
|
|
* #### Using an AppError class
|
|
|
|
*
|
|
|
|
* This approach gives more flexibility and power in how you handle exceptions. You can create
|
|
|
|
* `app/libs/app_error.php` and create a class called `AppError`. The core ErrorHandler class
|
|
|
|
* will attempt to construct this class and let it handle the exception. This provides a more
|
|
|
|
* flexible way to handle exceptions in your application.
|
|
|
|
*
|
|
|
|
* Finally, in your `app/config/bootstrap.php` you can configure use `set_exception_handler()`
|
|
|
|
* to take total control over application exception handling.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-08-28 05:39:02 +00:00
|
|
|
class ErrorHandler {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Controller instance.
|
|
|
|
*
|
2009-03-19 21:10:13 +00:00
|
|
|
* @var Controller
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $controller = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-08-30 01:37:25 +00:00
|
|
|
/**
|
|
|
|
* template to render for CakeException
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $template = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The method corresponding to the Exception this object is for.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $method = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The exception being handled.
|
|
|
|
*
|
|
|
|
* @var Exception
|
|
|
|
*/
|
|
|
|
public $error = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-09-02 00:29:55 +00:00
|
|
|
* Creates the controller to perform rendering on the error response.
|
|
|
|
* If the error is a CakeException it will be converted to either a 404 or a 500
|
|
|
|
* type error depending on the code used to construct the error.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $method Method producing the error
|
|
|
|
* @param array $messages Error messages
|
|
|
|
*/
|
2010-08-28 05:39:02 +00:00
|
|
|
function __construct(Exception $exception) {
|
|
|
|
App::import('Core', 'Sanitize');
|
2008-11-08 02:54:07 +00:00
|
|
|
|
2010-08-28 23:53:21 +00:00
|
|
|
$this->controller = $this->_getController($exception);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (method_exists($this->controller, 'apperror')) {
|
2010-08-28 05:39:02 +00:00
|
|
|
return $this->controller->appError($exception);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-30 01:37:25 +00:00
|
|
|
$method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-08-30 01:37:25 +00:00
|
|
|
if ($exception instanceof CakeException && !in_array($method, get_class_methods($this))) {
|
|
|
|
$method = '_cakeError';
|
2010-09-02 00:29:55 +00:00
|
|
|
} elseif (!method_exists($this, $method)) {
|
|
|
|
$method = 'error500';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-30 01:37:25 +00:00
|
|
|
|
|
|
|
if ($method !== 'error' && Configure::read('debug') == 0) {
|
|
|
|
$code = $exception->getCode();
|
|
|
|
$parentClass = get_parent_class($this);
|
|
|
|
if ($parentClass != 'ErrorHandler') {
|
|
|
|
$method = 'error404';
|
|
|
|
}
|
|
|
|
$parentMethods = (array)get_class_methods($parentClass);
|
|
|
|
if (in_array($method, $parentMethods)) {
|
|
|
|
$method = 'error404';
|
|
|
|
}
|
|
|
|
if ($code == 500) {
|
|
|
|
$method = 'error500';
|
2008-06-21 15:35:17 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-08-30 01:37:25 +00:00
|
|
|
$this->template = $template;
|
2010-08-28 23:53:21 +00:00
|
|
|
$this->method = $method;
|
|
|
|
$this->error = $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the controller instance to handle the exception.
|
|
|
|
* Override this method in subclasses to customize the controller used.
|
|
|
|
* This method returns the built in `CakeErrorController` normally, or if an error is repeated
|
|
|
|
* a bare controller will be used.
|
|
|
|
*
|
|
|
|
* @param Exception $exception The exception to get a controller for.
|
|
|
|
* @return Controller
|
|
|
|
*/
|
|
|
|
protected function _getController($exception) {
|
|
|
|
static $__previousError = null;
|
|
|
|
App::import('Controller', 'CakeError');
|
|
|
|
|
|
|
|
if ($__previousError != $exception) {
|
|
|
|
$__previousError = $exception;
|
|
|
|
$controller = new CakeErrorController();
|
|
|
|
} else {
|
|
|
|
$controller = new Controller();
|
|
|
|
$controller->viewPath = 'errors';
|
|
|
|
}
|
2010-08-29 03:32:14 +00:00
|
|
|
return $controller;
|
2010-08-28 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set as the default exception handler by the CakePHP bootstrap process.
|
2010-09-03 19:03:33 +00:00
|
|
|
*
|
|
|
|
* This will either use an AppError class if your application has one,
|
|
|
|
* or use the default ErrorHandler.
|
2010-08-28 05:39:02 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @see http://php.net/manual/en/function.set-exception-handler.php
|
|
|
|
*/
|
|
|
|
public static function handleException(Exception $exception) {
|
2010-09-03 19:03:33 +00:00
|
|
|
if (file_exists(APP . 'app_error.php') || class_exists('AppError')) {
|
|
|
|
if (!class_exists('AppError')) {
|
|
|
|
require(APP . 'app_error.php');
|
|
|
|
}
|
|
|
|
$AppError = new AppError($exception);
|
|
|
|
return $AppError->render();
|
|
|
|
}
|
2010-08-28 05:39:02 +00:00
|
|
|
$error = new ErrorHandler($exception);
|
2010-08-28 23:53:21 +00:00
|
|
|
$error->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the response for the exception.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function render() {
|
|
|
|
call_user_func_array(array($this, $this->method), array($this->error));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Displays an error page (e.g. 404 Not found).
|
|
|
|
*
|
|
|
|
* @param array $params Parameters for controller
|
|
|
|
*/
|
2010-08-29 03:32:14 +00:00
|
|
|
public function error(Exception $error) {
|
|
|
|
$this->error404($error);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-08-30 01:37:25 +00:00
|
|
|
/**
|
|
|
|
* Generic handler for the internal framework errors CakePHP can generate.
|
|
|
|
*
|
|
|
|
* @param CakeExeption $error
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _cakeError(CakeException $error) {
|
|
|
|
$url = Router::normalize($this->controller->request->here);
|
|
|
|
$code = $error->getCode();
|
|
|
|
$this->controller->response->statusCode($code);
|
|
|
|
$this->controller->set(array(
|
|
|
|
'code' => $code,
|
|
|
|
'url' => h($url),
|
|
|
|
));
|
|
|
|
$this->controller->set($error->getAttributes());
|
|
|
|
$this->_outputMessage($this->template);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Convenience method to display a 404 page.
|
|
|
|
*
|
|
|
|
* @param array $params Parameters for controller
|
|
|
|
*/
|
2010-08-29 03:32:14 +00:00
|
|
|
public function error404($error) {
|
2010-08-29 17:49:10 +00:00
|
|
|
$message = $error->getMessage();
|
2010-08-30 03:39:28 +00:00
|
|
|
if (Configure::read('debug') == 0 && $error instanceof CakeException) {
|
2010-08-29 17:49:10 +00:00
|
|
|
$message = __('Not Found');
|
|
|
|
}
|
2010-08-29 03:32:14 +00:00
|
|
|
$url = Router::normalize($this->controller->request->here);
|
2010-08-28 05:39:02 +00:00
|
|
|
$this->controller->response->statusCode(404);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->controller->set(array(
|
2010-08-29 03:32:14 +00:00
|
|
|
'code' => 404,
|
2010-08-29 17:49:10 +00:00
|
|
|
'name' => $message,
|
2010-08-29 03:32:14 +00:00
|
|
|
'url' => h($url),
|
2008-05-30 11:40:08 +00:00
|
|
|
));
|
2008-11-11 00:14:38 +00:00
|
|
|
$this->_outputMessage('error404');
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-12-13 20:35:51 +00:00
|
|
|
/**
|
|
|
|
* Convenience method to display a 500 page.
|
|
|
|
*
|
|
|
|
* @param array $params Parameters for controller
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function error500($params) {
|
2010-08-29 17:49:10 +00:00
|
|
|
$url = Router::normalize($this->controller->request->here);
|
|
|
|
$this->controller->response->statusCode(500);
|
2009-12-13 20:35:51 +00:00
|
|
|
$this->controller->set(array(
|
2010-04-15 16:00:25 +00:00
|
|
|
'name' => __('An Internal Error Has Occurred'),
|
2009-12-13 20:35:51 +00:00
|
|
|
'message' => h($url),
|
|
|
|
));
|
|
|
|
$this->_outputMessage('error500');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-08-30 01:37:25 +00:00
|
|
|
* Generate the response using the controller object.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-08-30 01:37:25 +00:00
|
|
|
* @param string $template The template to render.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:21:28 +00:00
|
|
|
protected function _outputMessage($template) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->controller->render($template);
|
|
|
|
$this->controller->afterFilter();
|
2010-08-28 05:39:02 +00:00
|
|
|
$this->controller->response->send();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|