Skip to content

Commit

Permalink
Exiting with 1 when Exception::getCode() returns non-integer values.
Browse files Browse the repository at this point in the history
From php.net/exception.getcode
> Returns the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException).
  • Loading branch information
renan committed Jun 26, 2014
1 parent e8ee25f commit 1a89a3c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Cake/Console/ConsoleErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function handleException(Exception $exception) {
$exception->getMessage(),
$exception->getTraceAsString()
));
return $this->_stop($exception->getCode() ? $exception->getCode() : 1);
$code = $exception->getCode();
$code = ($code && is_integer($code)) ? $code : 1;
return $this->_stop($code);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions lib/Cake/Test/Case/Console/ConsoleErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,31 @@ public function testError500Exception() {
$this->Error->handleException($exception);
}

/**
* test a exception with non-integer code
*
* @return void
*/
public function testNonIntegerExceptionCode() {
if (PHP_VERSION_ID < 50300) {
$this->markTestSkipped('ReflectionProperty::setAccessible() is available since 5.3');
}

$exception = new Exception('Non-integer exception code');

$class = new ReflectionClass('Exception');
$property = $class->getProperty('code');
$property->setAccessible(true);
$property->setValue($exception, '42S22');

ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
->with($this->stringContains('Non-integer exception code'));

$this->Error->expects($this->once())
->method('_stop')
->with(1);

$this->Error->handleException($exception);
}

}

0 comments on commit 1a89a3c

Please sign in to comment.