diff --git a/CHANGELOG.md b/CHANGELOG.md index ece91f0e..2ea82242 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee - Configured clients are now tagged with `'httplug.client'` +### Changed + +- Fixed error handling. Now TypeErrors and other \Throwable are correctly handled by ProfileClient + ## 1.16.0 - 2019-06-05 ### Changed diff --git a/src/Collector/Formatter.php b/src/Collector/Formatter.php index 7732600f..e2eb9c12 100644 --- a/src/Collector/Formatter.php +++ b/src/Collector/Formatter.php @@ -44,11 +44,11 @@ public function __construct(MessageFormatter $formatter, CurlCommandFormatter $c /** * Formats an exception. * - * @param Exception $exception + * @param \Throwable $exception * * @return string */ - public function formatException(Exception $exception) + public function formatException(\Throwable $exception) { if ($exception instanceof HttpException) { return $this->formatter->formatResponse($exception->getResponse()); diff --git a/src/Collector/ProfileClient.php b/src/Collector/ProfileClient.php index 58d7b1ac..318120ea 100644 --- a/src/Collector/ProfileClient.php +++ b/src/Collector/ProfileClient.php @@ -176,11 +176,11 @@ private function collectResponseInformations(ResponseInterface $response, Stopwa } /** - * @param \Exception $exception + * @param \Throwable $exception * @param StopwatchEvent $event * @param Stack $stack */ - private function collectExceptionInformations(\Exception $exception, StopwatchEvent $event, Stack $stack) + private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack) { if ($exception instanceof HttpException) { $this->collectResponseInformations($exception->getResponse(), $event, $stack); diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index f5d39064..2cc7a607 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -14,6 +14,7 @@ use Http\Promise\FulfilledPromise; use Http\Promise\Promise; use Http\Promise\RejectedPromise; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -34,7 +35,7 @@ class ProfileClientTest extends TestCase private $activeStack; /** - * @var HttpClient + * @var HttpClient|MockObject */ private $client; @@ -44,7 +45,7 @@ class ProfileClientTest extends TestCase private $request; /** - * @var Formatter + * @var Formatter|MockObject */ private $formatter; @@ -110,11 +111,6 @@ public function setUp(): void ->with($this->response) ->willReturn('FormattedResponse') ; - $this->formatter - ->method('formatException') - ->with($this->exception) - ->willReturn('FormattedException') - ; $this->stopwatch ->method('start') @@ -145,6 +141,26 @@ public function testSendRequest(): void $this->assertEquals('https', $this->activeStack->getRequestScheme()); } + /** + * @expectedException \Error + * @expectedException "You set string to int prop" + */ + public function testSendRequestTypeError() + { + $this->client + ->expects($this->once()) + ->method('sendRequest') + ->willReturnCallback(function () { + throw new \Error('You set string to int prop'); + }); + $this->formatter + ->expects($this->once()) + ->method('formatException') + ->with($this->isInstanceOf(\Error::class)); + + $this->subject->sendRequest($this->request); + } + public function testSendAsyncRequest(): void { $this->client @@ -211,6 +227,12 @@ public function testOnRejected(): void ->willReturn($this->rejectedPromise) ; + $this->formatter + ->method('formatException') + ->with($this->exception) + ->willReturn('FormattedException') + ; + $this->subject->sendAsyncRequest($this->request); $this->assertEquals(42, $this->activeStack->getDuration());