Skip to content

Commit d59a2c8

Browse files
committed
Making subclassing of ErrorHandler much easier.
Test cases added.
1 parent 955bd33 commit d59a2c8

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

cake/libs/error.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ function __construct($method, $messages) {
110110
}
111111

112112
if ($method !== 'error') {
113-
if (Configure::read() == 0) {
114-
$method = 'error404';
113+
if (Configure::read('debug') == 0) {
114+
$parentMethods = get_class_methods(get_parent_class($this));
115+
if (in_array($method, $parentMethods)) {
116+
$method = 'error404';
117+
}
115118
if (isset($code) && $code == 500) {
116119
$method = 'error500';
117120
}

cake/tests/cases/libs/error.test.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,34 @@ class BlueberryController extends AppController {
207207
var $components = array('Auth');
208208
}
209209

210+
/**
211+
* MyCustomErrorHandler class
212+
*
213+
* @package cake
214+
* @subpackage cake.tests.cases.libs
215+
*/
216+
class MyCustomErrorHandler extends ErrorHandler {
217+
218+
/**
219+
* custom error message type.
220+
*
221+
* @return void
222+
**/
223+
function missingWidgetThing() {
224+
echo 'widget thing is missing';
225+
}
226+
227+
/**
228+
* stop method
229+
*
230+
* @access public
231+
* @return void
232+
*/
233+
function _stop() {
234+
return;
235+
}
236+
}
237+
210238
/**
211239
* TestErrorHandler class
212240
*
@@ -244,6 +272,35 @@ function skip() {
244272
$this->skipIf(PHP_SAPI === 'cli', '%s Cannot be run from console');
245273
}
246274

275+
/**
276+
* test that methods declared in an ErrorHandler subclass are not converted
277+
* into error404 when debug == 0
278+
*
279+
* @return void
280+
**/
281+
function testSubclassMethodsNotBeingConvertedToError() {
282+
$back = Configure::read('debug');
283+
Configure::write('debug', 2);
284+
ob_start();
285+
$ErrorHandler = new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!'));
286+
$result = ob_get_clean();
287+
$this->assertEqual($result, 'widget thing is missing');
288+
289+
Configure::write('debug', 0);
290+
ob_start();
291+
$ErrorHandler = new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!'));
292+
$result = ob_get_clean();
293+
$this->assertEqual($result, 'widget thing is missing', 'Method declared in subclass converted to error404. %s');
294+
295+
Configure::write('debug', 0);
296+
ob_start();
297+
$ErrorHandler = new MyCustomErrorHandler('missingController', array('message' => 'Page not found'));
298+
$result = ob_get_clean();
299+
$this->assertPattern('/Not Found/', $result, 'Method declared in error handler not converted to error404. %s');
300+
301+
Configure::write('debug', $back);
302+
}
303+
247304
/**
248305
* testError method
249306
*

0 commit comments

Comments
 (0)