Skip to content
Permalink
Browse files

Little cleanup in exceptions.

- Removed duplicated or non-used exceptions.
- Making the error messages more descriptive and stardard.
  • Loading branch information...
renan committed Oct 15, 2011
1 parent 3ed712e commit 1cf67b1e5511a85b4f15ada6b359ec8f4316be3f
@@ -201,20 +201,21 @@ public function dispatch() {
*
* @param string $shell Optionally the name of a plugin
* @return mixed An object
* @throws MissingShellFileException when errors are encountered.
* @throws MissingShellException when errors are encountered.
*/
protected function _getShell($shell) {
list($plugin, $shell) = pluginSplit($shell, true);
$class = Inflector::camelize($shell) . 'Shell';
App::uses('Shell', 'Console');
App::uses('AppShell', 'Console');
App::uses($class, $plugin . 'Console/Command');
if (!class_exists($class)) {
throw new MissingShellFileException(array('shell' => $shell));
throw new MissingShellException(array(
'class' => $class
));
}
$Shell = new $class();
return $Shell;
@@ -54,7 +54,7 @@ public function __construct(Shell $Shell) {
* @param string $task Task name to load
* @param array $settings Settings for the task.
* @return Task A task object, Either the existing loaded task or a new one.
* @throws MissingTaskFileException, MissingTaskClassException when the task could not be found
* @throws MissingTaskException when the task could not be found
*/
public function load($task, $settings = array()) {
list($plugin, $name) = pluginSplit($task, true);
@@ -66,7 +66,9 @@ public function load($task, $settings = array()) {
App::uses($taskClass, $plugin . 'Console/Command/Task');
if (!class_exists($taskClass)) {
if (!class_exists($taskClass)) {
throw new MissingTaskClassException($taskClass);
throw new MissingTaskException(array(
'class' => $taskClass
));
}
}
@@ -73,7 +73,7 @@ public function getController() {
* @param string $component Component name to load
* @param array $settings Settings for the component.
* @return Component A component object, Either the existing loaded component or a new one.
* @throws MissingComponentFileException, MissingComponentClassException when the component could not be found
* @throws MissingComponentException when the component could not be found
*/
public function load($component, $settings = array()) {
if (is_array($settings) && isset($settings['className'])) {
@@ -90,8 +90,7 @@ public function load($component, $settings = array()) {
$componentClass = $name . 'Component';
App::uses($componentClass, $plugin . 'Controller/Component');
if (!class_exists($componentClass)) {
throw new MissingComponentClassException(array(
'file' => Inflector::underscore($componentClass) . '.php',
throw new MissingComponentException(array(
'class' => $componentClass
));
}
@@ -241,37 +241,23 @@ public function __construct($message, $code = 404, Exception $previous = null) {
}
/**
* Used when a Component file cannot be found.
* Used when a component cannot be found.
*
* @package Cake.Error
*/
class MissingComponentFileException extends CakeException {
protected $_messageTemplate = 'Component file "%s" is missing.';
class MissingComponentException extends CakeException {
protected $_messageTemplate = 'Component class %s could not be found.';
}
/**
* Used when a Component class cannot be found.
* Used when a behavior cannot be found.
*
* @package Cake.Error
*/
class MissingComponentClassException extends CakeException {
protected $_messageTemplate = 'Component class "%s" is missing.';
class MissingBehaviorException extends CakeException {
protected $_messageTemplate = 'Behavior class %s could not be found.';
}
/**
* Used when a Behavior file cannot be found.
*
* @package Cake.Error
*/
class MissingBehaviorFileException extends CakeException { }
/**
* Used when a Behavior class cannot be found.
*
* @package Cake.Error
*/
class MissingBehaviorClassException extends CakeException { }
/**
* Used when a view file cannot be found.
*
@@ -291,24 +277,14 @@ class MissingLayoutException extends CakeException {
}
/**
* Used when a helper file cannot be found.
* Used when a helper cannot be found.
*
* @package Cake.Error
*/
class MissingHelperFileException extends CakeException {
protected $_messageTemplate = 'Helper file "%s" is missing.';
class MissingHelperException extends CakeException {
protected $_messageTemplate = 'Helper class %s could not be found.';
}
/**
* Used when a helper class cannot be found.
*
* @package Cake.Error
*/
class MissingHelperClassException extends CakeException {
protected $_messageTemplate = 'Helper class "%s" is missing.';
}
/**
* Runtime Exceptions for ConnectionManager
*
@@ -328,21 +304,12 @@ class MissingConnectionException extends CakeException {
}
/**
* Used when a Task file cannot be found.
*
* @package Cake.Error
*/
class MissingTaskFileException extends CakeException {
protected $_messageTemplate = 'Task file "%s" is missing.';
}
/**
* Used when a Task class cannot be found.
* Used when a Task cannot be found.
*
* @package Cake.Error
*/
class MissingTaskClassException extends CakeException {
protected $_messageTemplate = 'Task class "%s" is missing.';
class MissingTaskException extends CakeException {
protected $_messageTemplate = 'Task class %s could not be found.';
}
/**
@@ -355,21 +322,12 @@ class MissingShellMethodException extends CakeException {
}
/**
* Used when a shell class cannot be found.
*
* @package Cake.Error
*/
class MissingShellClassException extends CakeException {
protected $_messageTemplate = "Shell class %s could not be loaded.";
}
/**
* Used when a shell file cannot be found.
* Used when a shell cannot be found.
*
* @package Cake.Error
*/
class MissingShellFileException extends CakeException {
protected $_messageTemplate = "Shell file %s could not be loaded.";
class MissingShellException extends CakeException {
protected $_messageTemplate = 'Shell class %s could not be found.';
}
/**
@@ -382,12 +340,12 @@ class MissingDatasourceConfigException extends CakeException {
}
/**
* Exception class to be thrown when a datasource is not found
* Used when a datasource cannot be found.
*
* @package Cake.Error
*/
class MissingDatasourceFileException extends CakeException {
protected $_messageTemplate = 'Datasource "%s" was not found.';
class MissingDatasourceException extends CakeException {
protected $_messageTemplate = 'Datasource class %s could not be found.';
}
/**
@@ -99,7 +99,7 @@ public function attach($behavior, $config = array()) {
* @param string $behavior CamelCased name of the behavior to load
* @param array $config Behavior configuration parameters
* @return boolean True on success, false on failure
* @throws MissingBehaviorClassException when a behavior could not be found.
* @throws MissingBehaviorException when a behavior could not be found.
*/
public function load($behavior, $config = array()) {
if (is_array($config) && isset($config['className'])) {
@@ -115,8 +115,7 @@ public function load($behavior, $config = array()) {
App::uses($class, $plugin . 'Model/Behavior');
if (!class_exists($class)) {
throw new MissingBehaviorClassException(array(
'file' => Inflector::underscore($behavior) . '.php',
throw new MissingBehaviorException(array(
'class' => $class
));
}
@@ -75,7 +75,7 @@ protected static function _init() {
* @param string $name The name of the DataSource, as defined in app/Config/database.php
* @return DataSource Instance
* @throws MissingDatasourceConfigException
* @throws MissingDatasourceFileException
* @throws MissingDatasourceException
*/
public static function getDataSource($name) {
if (empty(self::$_init)) {
@@ -141,7 +141,7 @@ public static function getSourceName($source) {
* or an array containing the filename (without extension) and class name of the object,
* to be found in app/Model/Datasource/ or lib/Cake/Model/Datasource/.
* @return boolean True on success, null on failure or false if the class is already loaded
* @throws MissingDatasourceFileException
* @throws MissingDatasourceException
*/
public static function loadDataSource($connName) {
if (empty(self::$_init)) {
@@ -168,7 +168,10 @@ public static function loadDataSource($connName) {
App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package);
if (!class_exists($conn['classname'])) {
throw new MissingDatasourceFileException(array('class' => $conn['classname'], 'plugin' => $plugin));
throw new MissingDatasourceException(array(
'class' => $conn['classname'],
'plugin' => $plugin

This comment has been minimized.

Copy link
@majna

majna Oct 16, 2011

Contributor

Actually, $plugin information is never used in ExceptionRenderer or error view
https://github.com/cakephp/cakephp/blob/2.0/lib/Cake/View/Errors/missing_datasource.ctp

it could be removed or added for consistency to all exceptions with plugin scope (components, helpers..).

This comment has been minimized.

Copy link
@renan

renan Oct 16, 2011

Author Member

Thank you @majna.

Just fixed in [573a349]

));
}
return true;
}
@@ -70,12 +70,12 @@ public function testLoadWithEnableFalse() {
$this->assertFalse($this->Tasks->enabled('DbConfig'), 'DbConfigTask should be disabled');
}
/**
* test missinghelper exception
* test missingtask exception
*
* @expectedException MissingTaskClassException
* @expectedException MissingTaskException
* @return void
*/
public function testLoadMissingTaskFile() {
public function testLoadMissingTask() {
$result = $this->Tasks->load('ThisTaskShouldAlwaysBeMissing');
}
@@ -111,10 +111,10 @@ public function testLoadWithEnableFalse() {
/**
* test missingcomponent exception
*
* @expectedException MissingComponentClassException
* @expectedException MissingComponentException
* @return void
*/
public function testLoadMissingComponentFile() {
public function testLoadMissingComponent() {
$this->Components->load('ThisComponentShouldAlwaysBeMissing');
}
@@ -505,62 +505,36 @@ public static function testProvider() {
500
),
array(
new MissingDatasourceFileException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')),
new MissingDatasourceException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')),
array(
'/<h2>Missing Datasource Class<\/h2>/',
'/Datasource class <em>MyDatasource<\/em> was not found/'
'/<h2>Missing Datasource<\/h2>/',
'/Datasource class <em>MyDatasource<\/em> could not be found/'
),
500
),
array(
new MissingHelperFileException(array('file' => 'MyCustomHelper.php', 'class' => 'MyCustomHelper')),
new MissingHelperException(array('class' => 'MyCustomHelper')),
array(
'/<h2>Missing Helper File<\/h2>/',
'/Create the class below in file:/',
'/<h2>Missing Helper<\/h2>/',
'/Create the class <em>MyCustomHelper<\/em> below in file:/',
'/(\/|\\\)MyCustomHelper.php/'
),
500
),
array(
new MissingHelperClassException(array('file' => 'MyCustomHelper.php', 'class' => 'MyCustomHelper')),
new MissingBehaviorException(array('class' => 'MyCustomBehavior')),
array(
'/<h2>Missing Helper Class<\/h2>/',
'/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./',
'/(\/|\\\)MyCustomHelper.php/',
),
500
),
array(
new MissingBehaviorFileException(array('file' => 'MyCustomBehavior.php', 'class' => 'MyCustomBehavior')),
array(
'/<h2>Missing Behavior File<\/h2>/',
'/Create the class below in file:/',
'/(\/|\\\)MyCustomBehavior.php/',
),
500
),
array(
new MissingBehaviorClassException(array('file' => 'MyCustomBehavior.php', 'class' => 'MyCustomBehavior')),
array(
'/The behavior class <em>MyCustomBehavior<\/em> can not be found or does not exist./',
'/<h2>Missing Behavior<\/h2>/',
'/Create the class <em>MyCustomBehavior<\/em> below in file:/',
'/(\/|\\\)MyCustomBehavior.php/'
),
500
),
array(
new MissingComponentFileException(array('file' => 'SideboxComponent.php', 'class' => 'SideboxComponent')),
array(
'/<h2>Missing Component File<\/h2>/',
'/Create the class <em>SideboxComponent<\/em> in file:/',
'/(\/|\\\)SideboxComponent.php/'
),
500
),
array(
new MissingComponentClassException(array('file' => 'SideboxComponent.php', 'class' => 'SideboxComponent')),
new MissingComponentException(array('class' => 'SideboxComponent')),
array(
'/<h2>Missing Component Class<\/h2>/',
'/Create the class <em>SideboxComponent<\/em> in file:/',
'/<h2>Missing Component<\/h2>/',
'/Create the class <em>SideboxComponent<\/em> below in file:/',
'/(\/|\\\)SideboxComponent.php/'
),
500
@@ -620,15 +594,15 @@ public function testCakeExceptionHandling($exception, $patterns, $code) {
* @return void
*/
public function testMissingRenderSafe() {
$exception = new MissingHelperFileException(array('class' => 'Fail'));
$exception = new MissingHelperException(array('class' => 'Fail'));
$ExceptionRenderer = new ExceptionRenderer($exception);
$ExceptionRenderer->controller = $this->getMock('Controller');
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
$ExceptionRenderer->controller->expects($this->at(2))
->method('render')
->with('missingHelperFile')
->with('missingHelper')
->will($this->throwException($exception));
$ExceptionRenderer->controller->expects($this->at(3))
@@ -538,7 +538,7 @@ public function testDetachWithPluginNames() {
/**
* test that attaching a non existant Behavior triggers a cake error.
*
* @expectedException MissingBehaviorClassException
* @expectedException MissingBehaviorException
* @return void
*/
public function testInvalidBehaviorCausingCakeError() {
@@ -224,7 +224,7 @@ public function testLoadDataSource() {
* testLoadDataSourceException() method
*
* @return void
* @expectedException MissingDatasourceFileException
* @expectedException MissingDatasourceException
*/
public function testLoadDataSourceException() {
$connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent');
Oops, something went wrong.

1 comment on commit 1cf67b1

@markstory

This comment has been minimized.

Copy link
Member

commented on 1cf67b1 Oct 16, 2011

This is a nice cleanup. Thanks for doing it :)

Please sign in to comment.
You can’t perform that action at this time.