Merge pull request #288 from thewilkybarkid/exception-previous

Add exception chaining
This commit is contained in:
Justin Hileman
2016-03-19 00:35:21 -07:00
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 string $msg
* @param array $token * @param array $token
* @param Exception $previous
*/ */
public function __construct($msg, array $token) public function __construct($msg, array $token, Exception $previous = null)
{ {
$this->token = $token; $this->token = $token;
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
parent::__construct($msg, 0, $previous);
} else {
parent::__construct($msg); parent::__construct($msg);
} }
}
/** /**
* @return array * @return array
@@ -18,11 +18,17 @@ class Mustache_Exception_UnknownFilterException extends UnexpectedValueException
/** /**
* @param string $filterName * @param string $filterName
* @param Exception $previous
*/ */
public function __construct($filterName) public function __construct($filterName, Exception $previous = null)
{ {
$this->filterName = $filterName; $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() public function getFilterName()
@@ -18,11 +18,17 @@ class Mustache_Exception_UnknownHelperException extends InvalidArgumentException
/** /**
* @param string $helperName * @param string $helperName
* @param Exception $previous
*/ */
public function __construct($helperName) public function __construct($helperName, Exception $previous = null)
{ {
$this->helperName = $helperName; $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() public function getHelperName()
@@ -18,11 +18,17 @@ class Mustache_Exception_UnknownTemplateException extends InvalidArgumentExcepti
/** /**
* @param string $templateName * @param string $templateName
* @param Exception $previous
*/ */
public function __construct($templateName) public function __construct($templateName, Exception $previous = null)
{ {
$this->templateName = $templateName; $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() public function getTemplateName()
@@ -24,4 +24,16 @@ class Mustache_Test_Exception_SyntaxExceptionTest extends PHPUnit_Framework_Test
$e = new Mustache_Exception_SyntaxException('ignore this', $token); $e = new Mustache_Exception_SyntaxException('ignore this', $token);
$this->assertEquals($token, $e->getToken()); $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'); $e = new Mustache_Exception_UnknownFilterException('eggs');
$this->assertEquals('eggs', $e->getFilterName()); $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'); $e = new Mustache_Exception_UnknownHelperException('gamma');
$this->assertEquals('gamma', $e->getHelperName()); $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'); $e = new Mustache_Exception_UnknownTemplateException('yoshi');
$this->assertEquals('yoshi', $e->getTemplateName()); $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());
}
} }