Skip to content

Commit

Permalink
Updating the various CakeExceptions to take arrays in their construct…
Browse files Browse the repository at this point in the history
…ors. This allows for the existing templates to continue working, as well as generalize the way in which errors are handled. This change also makes the messages coming out of exceptions more readable and removes string hackery.
  • Loading branch information
markstory committed Aug 30, 2010
1 parent 741f297 commit f1164c9
Show file tree
Hide file tree
Showing 22 changed files with 231 additions and 345 deletions.
24 changes: 11 additions & 13 deletions cake/dispatcher.php
Expand Up @@ -134,7 +134,9 @@ public function dispatch($url = null, $additionalParams = array()) {

if (!is_object($controller)) {
Router::setRequestInfo($request);
throw new MissingControllerException(Inflector::camelize($request->params['controller']) . 'Controller');
throw new MissingControllerException(array(
'controller' => Inflector::camelize($request->params['controller']) . 'Controller'
));
}
$privateAction = $request->params['action'][0] === '_';
$prefixes = Router::prefixes();
Expand All @@ -151,12 +153,10 @@ public function dispatch($url = null, $additionalParams = array()) {
Router::setRequestInfo($request);

if ($privateAction) {
$message = sprintf(
'%s::%s()',
Inflector::camelize($request->params['controller']) . "Controller",
$request->params['action']
);
throw new PrivateActionException($message);
throw new PrivateActionException(array(
'controller' => Inflector::camelize($request->params['controller']) . "Controller",
'action' => $request->params['action']
));
}

return $this->_invoke($controller, $request);
Expand Down Expand Up @@ -184,12 +184,10 @@ protected function _invoke(&$controller, $request) {
App::import('Controller', 'Scaffold', false);
return new Scaffold($controller, $request);
}
$message = sprintf(
'%s::%s()',
Inflector::camelize($request->params['controller']) . "Controller",
$request->params['action']
);
throw new MissingActionException($message);
throw new MissingActionException(array(
'controller' => Inflector::camelize($request->params['controller']) . "Controller",
'action' => $request->params['action']
));
}
$result =& call_user_func_array(array(&$controller, $request->params['action']), $request->params['pass']);
$response = $controller->getResponse();
Expand Down
10 changes: 8 additions & 2 deletions cake/libs/controller/component_collection.php
Expand Up @@ -54,10 +54,16 @@ public function load($component, $settings = array(), $enable = true) {
$componentClass = $name . 'Component';
if (!class_exists($componentClass)) {
if (!App::import('Component', $component)) {
throw new MissingComponentFileException(Inflector::underscore($component) . '.php');
throw new MissingComponentFileException(array(
'file' => Inflector::underscore($component) . '.php',
'class' => $componentClass
));
}
if (!class_exists($componentClass)) {
throw new MissingComponentFileException($component);
throw new MissingComponentFileException(array(
'file' => Inflector::underscore($component) . '.php',
'class' => $componentClass
));
}
}
$this->_loaded[$name] = new $componentClass($this, $settings);
Expand Down
8 changes: 5 additions & 3 deletions cake/libs/controller/scaffold.php
Expand Up @@ -414,11 +414,13 @@ protected function _scaffold(CakeRequest $request) {
break;
}
} else {
$message = sprintf('%s::%s()', $this->controller->name . "Controller", $request->action);
throw new MissingActionException($message);
throw new MissingActionException(array(
'controller' => $this->controller->name,
'action' => $request->action
));
}
} else {
throw new MissingDatabaseException($this->ScaffoldModel->useDbConfig);
throw new MissingDatabaseException(array('connection' => $this->ScaffoldModel->useDbConfig));
}
}

Expand Down
273 changes: 58 additions & 215 deletions cake/libs/error_handler.php
Expand Up @@ -40,6 +40,27 @@ class ErrorHandler {
*/
public $controller = null;

/**
* 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;

/**
* Class constructor.
*
Expand All @@ -54,27 +75,27 @@ function __construct(Exception $exception) {
if (method_exists($this->controller, 'apperror')) {
return $this->controller->appError($exception);
}
$method = Inflector::variable(str_replace('Exception', '', get_class($exception)));
$method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));

if (!in_array($method, get_class_methods($this))) {
$method = 'error';
if ($exception instanceof CakeException && !in_array($method, get_class_methods($this))) {
$method = '_cakeError';
}
if ($method !== 'error') {
if (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';
}

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';
}
}
$this->template = $template;
$this->method = $method;
$this->error = $exception;
}
Expand Down Expand Up @@ -133,6 +154,24 @@ public function error(Exception $error) {
$this->error404($error);
}

/**
* 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);
}

/**
* Convenience method to display a 404 page.
*
Expand Down Expand Up @@ -167,207 +206,11 @@ public function error500($params) {
));
$this->_outputMessage('error500');
}
/**
* Renders the Missing Controller web page.
*
* @param array $params Parameters for controller
*/
public function missingController($error) {
$controllerName = str_replace('Controller', '', $error->getMessage());
$this->controller->set(array(
'controller' => $error->getMessage(),
'controllerName' => $controllerName
));
$this->_outputMessage('missingController');
}

/**
* Renders the Missing Action web page.
*
* @param array $params Parameters for controller
*/
public function missingAction($error) {
$message = $error->getMessage();
list($controllerName, $action) = explode('::', $message);
$this->controller->set(array(
'controller' => $controllerName,
'action' => $action,
));
$this->_outputMessage('missingAction');
}

/**
* Renders the Private Action web page.
*
* @param array $params Parameters for controller
*/
public function privateAction($error) {
$message = $error->getMessage();
list($controllerName, $action) = explode('::', $message);
$this->controller->set(array(
'controller' => $controllerName,
'action' => $action
));
$this->_outputMessage('privateAction');
}

/**
* Renders the Missing Table web page.
*
* @param array $params Parameters for controller
*/
public function missingTable($error) {
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'model' => $error->getModel(),
'table' => $error->getTable(),
));
$this->_outputMessage('missingTable');
}

/**
* Renders the Missing Database web page.
*
* @param array $params Parameters for controller
*/
public function missingDatabase($exception) {
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'code' => '500',
'title' => __('Scaffold Missing Database Connection')
));
$this->_outputMessage('missingScaffolddb');
}

/**
* Renders the Missing View web page.
*
* @param array $params Parameters for controller
*/
public function missingView($error) {
$this->controller->set(array(
'file' => $error->getMessage(),
));
$this->_outputMessage('missingView');
}

/**
* Renders the Missing Layout web page.
*
* @param array $params Parameters for controller
*/
public function missingLayout($error) {
$this->controller->layout = 'default';
$this->controller->set(array(
'file' => $error->getMessage(),
));
$this->_outputMessage('missingLayout');
}

/**
* Renders the Database Connection web page.
*
* @param array $params Parameters for controller
*/
public function missingConnection($error) {
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'code' => '500',
'model' => $error->getMessage(),
));
$this->_outputMessage('missingConnection');
}

/**
* Renders the Missing Helper file web page.
*
* @param array $params Parameters for controller
*/
public function missingHelperFile($error) {
list($class, $ext) = explode('.', $error->getMessage());
$this->controller->set(array(
'className' => Inflector::camelize($class),
'file' => $error->getMessage()
));
$this->_outputMessage('missingHelperFile');
}

/**
* Renders the Missing Helper class web page.
*
* @param array $params Parameters for controller
*/
public function missingHelperClass($error) {
$class = $error->getMessage();
$file = Inflector::underscore(str_replace('Helper', '', $error->getMessage())) . '.php';
$this->controller->set(array(
'className' => $class,
'file' => $file,
));
$this->_outputMessage('missingHelperClass');
}

/**
* Renders the Missing Behavior file web page.
*
* @param array $params Parameters for controller
*/
public function missingBehaviorFile($error) {
list($class, $ext) = explode('.', $error->getMessage());
$this->controller->set(array(
'className' => Inflector::camelize($class),
'file' => $error->getMessage()
));
$this->_outputMessage('missingBehaviorFile');
}

/**
* Renders the Missing Behavior class web page.
*
* @param array $params Parameters for controller
*/
public function missingBehaviorClass($error) {
$class = $error->getMessage();
$file = Inflector::underscore(str_replace('Behavior', '', $error->getMessage())) . '.php';
$this->controller->set(array(
'className' => $class,
'file' => $file,
));
$this->_outputMessage('missingBehaviorClass');
}

/**
* Renders the Missing Component file web page.
*
* @param array $params Parameters for controller
*/
public function missingComponentFile($error) {
list($class, $ext) = explode('.', $error->getMessage());
$this->controller->set(array(
'className' => Inflector::camelize($class),
'file' => $error->getMessage()
));
$this->_outputMessage('missingComponentFile');
}

/**
* Renders the Missing Component class web page.
*
* @param array $params Parameters for controller
*/
public function missingComponentClass($error) {
$class = $error->getMessage();
$file = Inflector::underscore(str_replace('Component', '', $error->getMessage())) . '.php';
$this->controller->set(array(
'className' => $class,
'file' => $file,
));
$this->_outputMessage('missingComponentClass');
}

/**
* Output message
* Generate the response using the controller object.
*
* @param string $template The template to render.
*/
protected function _outputMessage($template) {
$this->controller->render($template);
Expand Down

0 comments on commit f1164c9

Please sign in to comment.