Skip to content

Commit fb18f83

Browse files
committed
fix exception case
1 parent 8fa554d commit fb18f83

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

Collector/ProfilePlugin.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,17 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
7777
return $first($request);
7878
};
7979

80-
return $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst)->then(function (ResponseInterface $response) use ($profile, $request, $stack) {
80+
try {
81+
$promise = $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst);
82+
} catch (Exception $e) {
83+
$profile->setFailed(true);
84+
$profile->setResponse($this->formatter->formatException($e));
85+
$this->collectRequestInformation($request, $stack);
86+
87+
throw $e;
88+
}
89+
90+
return $promise->then(function (ResponseInterface $response) use ($profile, $request, $stack) {
8191
$profile->setResponse($this->formatter->formatResponse($response));
8292
$this->collectRequestInformation($request, $stack);
8393

@@ -96,7 +106,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
96106
* and the cache is hit without re-validation.
97107
*
98108
* @param RequestInterface $request
99-
* @param Stack|null $stack
109+
* @param Stack|null $stack
100110
*/
101111
private function collectRequestInformation(RequestInterface $request, Stack $stack = null)
102112
{

Tests/Unit/Collector/ProfilePluginTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Http\HttplugBundle\Collector\ProfilePlugin;
1212
use Http\HttplugBundle\Collector\Stack;
1313
use Http\Promise\FulfilledPromise;
14+
use Http\Promise\Promise;
15+
use Http\Promise\RejectedPromise;
1416
use Psr\Http\Message\RequestInterface;
1517
use Psr\Http\Message\ResponseInterface;
1618

@@ -36,6 +38,11 @@ class ProfilePluginTest extends \PHPUnit_Framework_TestCase
3638
*/
3739
private $response;
3840

41+
/**
42+
* @var Promise
43+
*/
44+
private $fulfilledPromise;
45+
3946
/**
4047
* @var Stack
4148
*/
@@ -46,6 +53,11 @@ class ProfilePluginTest extends \PHPUnit_Framework_TestCase
4653
*/
4754
private $exception;
4855

56+
/**
57+
* @var Promise
58+
*/
59+
private $rejectedPromise;
60+
4961
/**
5062
* @var Formatter
5163
*/
@@ -62,8 +74,10 @@ public function setUp()
6274
$this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock();
6375
$this->request = new Request('GET', '/');
6476
$this->response = new Response();
77+
$this->fulfilledPromise = new FulfilledPromise($this->response);
6578
$this->currentStack = new Stack('default', 'FormattedRequest');
6679
$this->exception = new TransferException();
80+
$this->rejectedPromise = new RejectedPromise($this->exception);
6781
$this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock();
6882

6983
$this->collector
@@ -74,9 +88,7 @@ public function setUp()
7488
$this->plugin
7589
->method('handleRequest')
7690
->willReturnCallback(function ($request, $next, $first) {
77-
$next($request);
78-
79-
return new FulfilledPromise($this->response);
91+
return $next($request);
8092
})
8193
;
8294

@@ -115,13 +127,15 @@ public function testCallDecoratedPlugin()
115127
;
116128

117129
$this->subject->handleRequest($this->request, function () {
130+
return $this->fulfilledPromise;
118131
}, function () {
119132
});
120133
}
121134

122135
public function testProfileIsInitialized()
123136
{
124137
$this->subject->handleRequest($this->request, function () {
138+
return $this->fulfilledPromise;
125139
}, function () {
126140
});
127141

@@ -133,6 +147,7 @@ public function testProfileIsInitialized()
133147
public function testCollectRequestInformations()
134148
{
135149
$this->subject->handleRequest($this->request, function () {
150+
return $this->fulfilledPromise;
136151
}, function () {
137152
});
138153

@@ -143,6 +158,7 @@ public function testCollectRequestInformations()
143158
public function testOnFulfilled()
144159
{
145160
$promise = $this->subject->handleRequest($this->request, function () {
161+
return $this->fulfilledPromise;
146162
}, function () {
147163
});
148164

@@ -156,7 +172,7 @@ public function testOnRejected()
156172
$this->setExpectedException(TransferException::class);
157173

158174
$promise = $this->subject->handleRequest($this->request, function () {
159-
throw new TransferException();
175+
return $this->rejectedPromise;
160176
}, function () {
161177
});
162178

0 commit comments

Comments
 (0)