Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
App::uses('AppException', 'Lib');
App::uses('ErrorHandler', 'Error');
class AppErrorHandler extends ErrorHandler {
public static function handleException(Exception $exception) {
if ($exception instanceof AppException) {
$element = 'default';
$message = $exception->getMessage();
$params = array('class' => 'error');
CakeSession::write('Message.flash', compact('message', 'element', 'params'));
if ($exception->hasRoute()) {
$controller = $this->_getController();
return $controller->redirect($exception->getRoute());
}
}
return parent::handleException($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) {
App::uses('CakeErrorController', 'Controller');
if (!$request = Router::getRequest(true)) {
$request = new CakeRequest();
}
$response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
try {
if (class_exists('AppController')) {
$controller = new CakeErrorController($request, $response);
}
} catch (Exception $e) {
}
if (empty($controller)) {
$controller = new Controller($request, $response);
$controller->viewPath = 'Errors';
}
return $controller;
}
}
?>
<?php
// app /Lib/Exception/AppException.php
class AppException extends CakeException {
public function <strong>construct($message, $code = 0) {
parent::</strong>construct($message, $code);
return $this;
}<p></p>
public function setRoute($route = null) {
$this->_attributes['route'] = $route;
return $this;
}
}
?>
<?php
// app /Lib/Exception/AppException.php
class AppException extends CakeException {
public function setRoute($route = null) {
$this->_attributes['route'] = $route;
}
public function getRoute() {
return $this->_attributes['route'];
}
public function hasRoute() {
return isset($this->_attributes['route']);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment