Skip to content

Commit

Permalink
Making AggregateException a child of RejectionException
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed May 13, 2015
1 parent 1642695 commit 3725254
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
15 changes: 5 additions & 10 deletions src/AggregateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
/**
* Exception thrown when too many errors occur in the some() or any() methods.
*/
class AggregateException extends \Exception
class AggregateException extends RejectionException
{
private $reasons;

public function __construct($msg, array $reasons)
{
parent::__construct($msg);
$this->reasons = $reasons;
}

public function getReasons()
{
return $this->reasons;
parent::__construct(
$reasons,
sprintf('%s; %d rejected promises', $msg, count($reasons))
);
}
}
12 changes: 8 additions & 4 deletions src/RejectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ class RejectionException extends \RuntimeException
private $reason;

/**
* @param mixed $reason Rejection reason.
* @param mixed $reason Rejection reason.
* @param string $description Optional description
*/
public function __construct($reason)
public function __construct($reason, $description = null)
{
$this->reason = $reason;

$message = 'The promise was rejected';
if (is_string($reason)

if ($description) {
$message .= ' with reason: ' . $description;
} elseif (is_string($reason)
|| (is_object($reason) && method_exists($reason, '__toString'))
) {
$message .= ' with reason: ' . $this->reason;
} elseif (is_array($reason) || $reason instanceof \JsonSerializable) {
} elseif ($reason instanceof \JsonSerializable) {
$message .= ' with reason: '
. json_encode($this->reason, JSON_PRETTY_PRINT);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/AggregateExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

class AggregateExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testHasReasons()
public function testHasReason()
{
$e = new AggregateException('foo', ['baz', 'bar']);
$this->assertContains('foo', $e->getMessage());
$this->assertEquals(['baz', 'bar'], $e->getReasons());
$this->assertEquals(['baz', 'bar'], $e->getReason());
}
}
18 changes: 13 additions & 5 deletions tests/RejectionExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use GuzzleHttp\Promise\RejectionException;

class Thing
class Thing1
{
public function __construct($message)
{
Expand All @@ -16,24 +16,32 @@ public function __toString()
}
}

class Thing2 implements \JsonSerializable
{
public function jsonSerialize()
{
return '{}';
}
}

/**
* @covers GuzzleHttp\Promise\RejectionException
*/
class RejectionExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testCanGetReasonFromException()
{
$thing = new Thing('foo');
$thing = new Thing1('foo');
$e = new RejectionException($thing);

$this->assertSame($thing, $e->getReason());
$this->assertEquals('The promise was rejected with reason: foo', $e->getMessage());
}

public function testCanGetReasonMessageFromArrayOrJson()
public function testCanGetReasonMessageFromJson()
{
$reason = ['foo' => 'bar'];
$reason = new Thing2();
$e = new RejectionException($reason);
$this->assertContains("{\n \"foo\": \"bar\"\n}", $e->getMessage());
$this->assertContains("{}", $e->getMessage());
}
}
2 changes: 1 addition & 1 deletion tests/functionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function testSomeRejectsWhenTooManyRejections()
});
P\queue()->run();
$this->assertInstanceOf('GuzzleHttp\Promise\AggregateException', $called);
$this->assertContains('bad', $called->getReasons());
$this->assertContains('bad', $called->getReason());
}

public function testCanWaitUntilSomeCountIsSatisfied()
Expand Down

0 comments on commit 3725254

Please sign in to comment.