Add exception chaining

This commit is contained in:
Chris Wilkinson
2016-02-23 07:49:22 +00:00
parent 865de4114a
commit c858bb1f1a
8 changed files with 82 additions and 13 deletions
+6 -1
View File
@@ -19,12 +19,17 @@ class Mustache_Exception_SyntaxException extends LogicException implements Musta
/**
* @param string $msg
* @param array $token
* @param Exception $previous
*/
public function __construct($msg, array $token)
public function __construct($msg, array $token, Exception $previous = null)
{
$this->token = $token;
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($msg, 0, $previous);
} else {
parent::__construct($msg);
}
}
/**
* @return array
@@ -18,11 +18,17 @@ class Mustache_Exception_UnknownFilterException extends UnexpectedValueException
/**
* @param string $filterName
* @param Exception $previous
*/
public function __construct($filterName)
public function __construct($filterName, Exception $previous = null)
{
$this->filterName = $filterName;
parent::__construct(sprintf('Unknown filter: %s', $filterName));
$message = sprintf('Unknown filter: %s', $filterName);
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($message, 0, $previous);
} else {
parent::__construct($message);
}
}
public function getFilterName()
@@ -18,11 +18,17 @@ class Mustache_Exception_UnknownHelperException extends InvalidArgumentException
/**
* @param string $helperName
* @param Exception $previous
*/
public function __construct($helperName)
public function __construct($helperName, Exception $previous = null)
{
$this->helperName = $helperName;
parent::__construct(sprintf('Unknown helper: %s', $helperName));
$message = sprintf('Unknown helper: %s', $helperName);
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($message, 0, $previous);
} else {
parent::__construct($message);
}
}
public function getHelperName()
@@ -18,11 +18,17 @@ class Mustache_Exception_UnknownTemplateException extends InvalidArgumentExcepti
/**
* @param string $templateName
* @param Exception $previous
*/
public function __construct($templateName)
public function __construct($templateName, Exception $previous = null)
{
$this->templateName = $templateName;
parent::__construct(sprintf('Unknown template: %s', $templateName));
$message = sprintf('Unknown template: %s', $templateName);
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($message, 0, $previous);
} else {
parent::__construct($message);
}
}
public function getTemplateName()
@@ -24,4 +24,16 @@ class Mustache_Test_Exception_SyntaxExceptionTest extends PHPUnit_Framework_Test
$e = new Mustache_Exception_SyntaxException('ignore this', $token);
$this->assertEquals($token, $e->getToken());
}
public function testPrevious()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Exception chaining requires at least PHP 5.3');
}
$previous = new Exception();
$e = new Mustache_Exception_SyntaxException('foo', array(), $previous);
$this->assertSame($previous, $e->getPrevious());
}
}
@@ -29,4 +29,16 @@ class Mustache_Test_Exception_UnknownFilterExceptionTest extends PHPUnit_Framewo
$e = new Mustache_Exception_UnknownFilterException('eggs');
$this->assertEquals('eggs', $e->getFilterName());
}
public function testPrevious()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Exception chaining requires at least PHP 5.3');
}
$previous = new Exception();
$e = new Mustache_Exception_UnknownFilterException('foo', $previous);
$this->assertSame($previous, $e->getPrevious());
}
}
@@ -29,4 +29,15 @@ class Mustache_Test_Exception_UnknownHelperExceptionTest extends PHPUnit_Framewo
$e = new Mustache_Exception_UnknownHelperException('gamma');
$this->assertEquals('gamma', $e->getHelperName());
}
public function testPrevious()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Exception chaining requires at least PHP 5.3');
}
$previous = new Exception();
$e = new Mustache_Exception_UnknownHelperException('foo', $previous);
$this->assertSame($previous, $e->getPrevious());
}
}
@@ -29,4 +29,15 @@ class Mustache_Test_Exception_UnknownTemplateExceptionTest extends PHPUnit_Frame
$e = new Mustache_Exception_UnknownTemplateException('yoshi');
$this->assertEquals('yoshi', $e->getTemplateName());
}
public function testPrevious()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Exception chaining requires at least PHP 5.3');
}
$previous = new Exception();
$e = new Mustache_Exception_UnknownTemplateException('foo', $previous);
$this->assertSame($previous, $e->getPrevious());
}
}