Skip to content

Commit

Permalink
Little cleanup in exceptions.
Browse files Browse the repository at this point in the history
- 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 1cf67b1
Show file tree
Hide file tree
Showing 22 changed files with 89 additions and 271 deletions.
7 changes: 4 additions & 3 deletions lib/Cake/Console/ShellDispatcher.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -201,20 +201,21 @@ public function dispatch() {
* *
* @param string $shell Optionally the name of a plugin * @param string $shell Optionally the name of a plugin
* @return mixed An object * @return mixed An object
* @throws MissingShellFileException when errors are encountered. * @throws MissingShellException when errors are encountered.
*/ */
protected function _getShell($shell) { protected function _getShell($shell) {
list($plugin, $shell) = pluginSplit($shell, true); list($plugin, $shell) = pluginSplit($shell, true);



$class = Inflector::camelize($shell) . 'Shell'; $class = Inflector::camelize($shell) . 'Shell';


App::uses('Shell', 'Console'); App::uses('Shell', 'Console');
App::uses('AppShell', 'Console'); App::uses('AppShell', 'Console');
App::uses($class, $plugin . 'Console/Command'); App::uses($class, $plugin . 'Console/Command');


if (!class_exists($class)) { if (!class_exists($class)) {
throw new MissingShellFileException(array('shell' => $shell)); throw new MissingShellException(array(
'class' => $class
));
} }
$Shell = new $class(); $Shell = new $class();
return $Shell; return $Shell;
Expand Down
6 changes: 4 additions & 2 deletions lib/Cake/Console/TaskCollection.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(Shell $Shell) {
* @param string $task Task name to load * @param string $task Task name to load
* @param array $settings Settings for the task. * @param array $settings Settings for the task.
* @return Task A task object, Either the existing loaded task or a new one. * @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()) { public function load($task, $settings = array()) {
list($plugin, $name) = pluginSplit($task, true); list($plugin, $name) = pluginSplit($task, true);
Expand All @@ -66,7 +66,9 @@ public function load($task, $settings = array()) {
App::uses($taskClass, $plugin . 'Console/Command/Task'); App::uses($taskClass, $plugin . 'Console/Command/Task');
if (!class_exists($taskClass)) { if (!class_exists($taskClass)) {
if (!class_exists($taskClass)) { if (!class_exists($taskClass)) {
throw new MissingTaskClassException($taskClass); throw new MissingTaskException(array(
'class' => $taskClass
));
} }
} }


Expand Down
5 changes: 2 additions & 3 deletions lib/Cake/Controller/ComponentCollection.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getController() {
* @param string $component Component name to load * @param string $component Component name to load
* @param array $settings Settings for the component. * @param array $settings Settings for the component.
* @return Component A component object, Either the existing loaded component or a new one. * @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()) { public function load($component, $settings = array()) {
if (is_array($settings) && isset($settings['className'])) { if (is_array($settings) && isset($settings['className'])) {
Expand All @@ -90,8 +90,7 @@ public function load($component, $settings = array()) {
$componentClass = $name . 'Component'; $componentClass = $name . 'Component';
App::uses($componentClass, $plugin . 'Controller/Component'); App::uses($componentClass, $plugin . 'Controller/Component');
if (!class_exists($componentClass)) { if (!class_exists($componentClass)) {
throw new MissingComponentClassException(array( throw new MissingComponentException(array(
'file' => Inflector::underscore($componentClass) . '.php',
'class' => $componentClass 'class' => $componentClass
)); ));
} }
Expand Down
78 changes: 18 additions & 60 deletions lib/Cake/Error/exceptions.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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 * @package Cake.Error
*/ */
class MissingComponentFileException extends CakeException { class MissingComponentException extends CakeException {
protected $_messageTemplate = 'Component file "%s" is missing.'; 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 * @package Cake.Error
*/ */
class MissingComponentClassException extends CakeException { class MissingBehaviorException extends CakeException {
protected $_messageTemplate = 'Component class "%s" is missing.'; 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. * Used when a view file cannot be found.
* *
Expand All @@ -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 * @package Cake.Error
*/ */
class MissingHelperFileException extends CakeException { class MissingHelperException extends CakeException {
protected $_messageTemplate = 'Helper file "%s" is missing.'; 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 * Runtime Exceptions for ConnectionManager
* *
Expand All @@ -328,21 +304,12 @@ class MissingConnectionException extends CakeException {
} }


/** /**
* Used when a Task file cannot be found. * Used when a Task 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.
* *
* @package Cake.Error * @package Cake.Error
*/ */
class MissingTaskClassException extends CakeException { class MissingTaskException extends CakeException {
protected $_messageTemplate = 'Task class "%s" is missing.'; protected $_messageTemplate = 'Task class %s could not be found.';
} }


/** /**
Expand All @@ -355,21 +322,12 @@ class MissingShellMethodException extends CakeException {
} }


/** /**
* Used when a shell class cannot be found. * Used when a shell 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.
* *
* @package Cake.Error * @package Cake.Error
*/ */
class MissingShellFileException extends CakeException { class MissingShellException extends CakeException {
protected $_messageTemplate = "Shell file %s could not be loaded."; protected $_messageTemplate = 'Shell class %s could not be found.';
} }


/** /**
Expand All @@ -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 * @package Cake.Error
*/ */
class MissingDatasourceFileException extends CakeException { class MissingDatasourceException extends CakeException {
protected $_messageTemplate = 'Datasource "%s" was not found.'; protected $_messageTemplate = 'Datasource class %s could not be found.';
} }


/** /**
Expand Down
5 changes: 2 additions & 3 deletions lib/Cake/Model/BehaviorCollection.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function attach($behavior, $config = array()) {
* @param string $behavior CamelCased name of the behavior to load * @param string $behavior CamelCased name of the behavior to load
* @param array $config Behavior configuration parameters * @param array $config Behavior configuration parameters
* @return boolean True on success, false on failure * @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()) { public function load($behavior, $config = array()) {
if (is_array($config) && isset($config['className'])) { if (is_array($config) && isset($config['className'])) {
Expand All @@ -115,8 +115,7 @@ public function load($behavior, $config = array()) {


App::uses($class, $plugin . 'Model/Behavior'); App::uses($class, $plugin . 'Model/Behavior');
if (!class_exists($class)) { if (!class_exists($class)) {
throw new MissingBehaviorClassException(array( throw new MissingBehaviorException(array(
'file' => Inflector::underscore($behavior) . '.php',
'class' => $class 'class' => $class
)); ));
} }
Expand Down
9 changes: 6 additions & 3 deletions lib/Cake/Model/ConnectionManager.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected static function _init() {
* @param string $name The name of the DataSource, as defined in app/Config/database.php * @param string $name The name of the DataSource, as defined in app/Config/database.php
* @return DataSource Instance * @return DataSource Instance
* @throws MissingDatasourceConfigException * @throws MissingDatasourceConfigException
* @throws MissingDatasourceFileException * @throws MissingDatasourceException
*/ */
public static function getDataSource($name) { public static function getDataSource($name) {
if (empty(self::$_init)) { if (empty(self::$_init)) {
Expand Down Expand Up @@ -141,7 +141,7 @@ public static function getSourceName($source) {
* or an array containing the filename (without extension) and class name of the object, * 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/. * 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 * @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) { public static function loadDataSource($connName) {
if (empty(self::$_init)) { if (empty(self::$_init)) {
Expand All @@ -168,7 +168,10 @@ public static function loadDataSource($connName) {


App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package); App::uses($conn['classname'], $plugin . 'Model/Datasource' . $package);
if (!class_exists($conn['classname'])) { 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 Contributor

Thank you @majna.

Just fixed in [573a349]

));
} }
return true; return true;
} }
Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Test/Case/Console/TaskCollectionTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public function testLoadWithEnableFalse() {
$this->assertFalse($this->Tasks->enabled('DbConfig'), 'DbConfigTask should be disabled'); $this->assertFalse($this->Tasks->enabled('DbConfig'), 'DbConfigTask should be disabled');
} }
/** /**
* test missinghelper exception * test missingtask exception
* *
* @expectedException MissingTaskClassException * @expectedException MissingTaskException
* @return void * @return void
*/ */
public function testLoadMissingTaskFile() { public function testLoadMissingTask() {
$result = $this->Tasks->load('ThisTaskShouldAlwaysBeMissing'); $result = $this->Tasks->load('ThisTaskShouldAlwaysBeMissing');
} }


Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Test/Case/Controller/ComponentCollectionTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public function testLoadWithEnableFalse() {
/** /**
* test missingcomponent exception * test missingcomponent exception
* *
* @expectedException MissingComponentClassException * @expectedException MissingComponentException
* @return void * @return void
*/ */
public function testLoadMissingComponentFile() { public function testLoadMissingComponent() {
$this->Components->load('ThisComponentShouldAlwaysBeMissing'); $this->Components->load('ThisComponentShouldAlwaysBeMissing');
} }


Expand Down
54 changes: 14 additions & 40 deletions lib/Cake/Test/Case/Error/ExceptionRendererTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -505,62 +505,36 @@ public static function testProvider() {
500 500
), ),
array( array(
new MissingDatasourceFileException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')), new MissingDatasourceException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')),
array( array(
'/<h2>Missing Datasource Class<\/h2>/', '/<h2>Missing Datasource<\/h2>/',
'/Datasource class <em>MyDatasource<\/em> was not found/' '/Datasource class <em>MyDatasource<\/em> could not be found/'
), ),
500 500
), ),
array( array(
new MissingHelperFileException(array('file' => 'MyCustomHelper.php', 'class' => 'MyCustomHelper')), new MissingHelperException(array('class' => 'MyCustomHelper')),
array( array(
'/<h2>Missing Helper File<\/h2>/', '/<h2>Missing Helper<\/h2>/',
'/Create the class below in file:/', '/Create the class <em>MyCustomHelper<\/em> below in file:/',
'/(\/|\\\)MyCustomHelper.php/' '/(\/|\\\)MyCustomHelper.php/'
), ),
500 500
), ),
array( array(
new MissingHelperClassException(array('file' => 'MyCustomHelper.php', 'class' => 'MyCustomHelper')), new MissingBehaviorException(array('class' => 'MyCustomBehavior')),
array( array(
'/<h2>Missing Helper Class<\/h2>/', '/<h2>Missing Behavior<\/h2>/',
'/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./', '/Create the class <em>MyCustomBehavior<\/em> below in file:/',
'/(\/|\\\)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./',
'/(\/|\\\)MyCustomBehavior.php/' '/(\/|\\\)MyCustomBehavior.php/'
), ),
500 500
), ),
array( array(
new MissingComponentFileException(array('file' => 'SideboxComponent.php', 'class' => 'SideboxComponent')), new MissingComponentException(array('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')),
array( array(
'/<h2>Missing Component Class<\/h2>/', '/<h2>Missing Component<\/h2>/',
'/Create the class <em>SideboxComponent<\/em> in file:/', '/Create the class <em>SideboxComponent<\/em> below in file:/',
'/(\/|\\\)SideboxComponent.php/' '/(\/|\\\)SideboxComponent.php/'
), ),
500 500
Expand Down Expand Up @@ -620,15 +594,15 @@ public function testCakeExceptionHandling($exception, $patterns, $code) {
* @return void * @return void
*/ */
public function testMissingRenderSafe() { public function testMissingRenderSafe() {
$exception = new MissingHelperFileException(array('class' => 'Fail')); $exception = new MissingHelperException(array('class' => 'Fail'));
$ExceptionRenderer = new ExceptionRenderer($exception); $ExceptionRenderer = new ExceptionRenderer($exception);


$ExceptionRenderer->controller = $this->getMock('Controller'); $ExceptionRenderer->controller = $this->getMock('Controller');
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom'); $ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest'); $ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
$ExceptionRenderer->controller->expects($this->at(2)) $ExceptionRenderer->controller->expects($this->at(2))
->method('render') ->method('render')
->with('missingHelperFile') ->with('missingHelper')
->will($this->throwException($exception)); ->will($this->throwException($exception));


$ExceptionRenderer->controller->expects($this->at(3)) $ExceptionRenderer->controller->expects($this->at(3))
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/Model/BehaviorCollectionTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ public function testDetachWithPluginNames() {
/** /**
* test that attaching a non existant Behavior triggers a cake error. * test that attaching a non existant Behavior triggers a cake error.
* *
* @expectedException MissingBehaviorClassException * @expectedException MissingBehaviorException
* @return void * @return void
*/ */
public function testInvalidBehaviorCausingCakeError() { public function testInvalidBehaviorCausingCakeError() {
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/Case/Model/ConnectionManagerTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function testLoadDataSource() {
* testLoadDataSourceException() method * testLoadDataSourceException() method
* *
* @return void * @return void
* @expectedException MissingDatasourceFileException * @expectedException MissingDatasourceException
*/ */
public function testLoadDataSourceException() { public function testLoadDataSourceException() {
$connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent'); $connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent');
Expand Down
Loading

1 comment on commit 1cf67b1

@markstory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.