@@ -75,59 +75,67 @@ public function __construct($client, Collector $collector, Formatter $formatter,
7575 */
7676 public function sendAsyncRequest (RequestInterface $ request )
7777 {
78- $ stack = $ this ->collector ->getCurrentStack ();
78+ $ stack = $ this ->collector ->getActiveStack ();
79+
7980 $ this ->collectRequestInformations ($ request , $ stack );
8081 $ event = $ this ->stopwatch ->start ($ this ->getStopwatchEventName ($ request ));
8182
82- return $ this ->client ->sendAsyncRequest ($ request )->then (
83- function (ResponseInterface $ response ) use ($ event , $ stack ) {
84- $ event ->stop ();
85- $ this ->collectResponseInformations ($ response , $ event , $ stack );
83+ $ onFulfilled = function (ResponseInterface $ response ) use ($ event , $ stack ) {
84+ $ this ->collectResponseInformations ($ response , $ event , $ stack );
85+
86+ return $ response ;
87+ };
88+
89+ $ onRejected = function (\Exception $ exception ) use ($ event , $ stack ) {
90+ $ this ->collectExceptionInformations ($ exception , $ event , $ stack );
91+
92+ throw $ exception ;
93+ };
8694
87- return $ response ;
88- }, function (\Exception $ exception ) use ($ event , $ stack ) {
89- $ event ->stop ();
90- $ this ->collectExceptionInformations ($ exception , $ event , $ stack );
95+ $ this ->collector ->deactivateStack ($ stack );
9196
92- throw $ exception ;
93- }
94- );
97+ try {
98+ return $ this ->client ->sendAsyncRequest ($ request )->then ($ onFulfilled , $ onRejected );
99+ } finally {
100+ $ event ->stop ();
101+ $ this ->collector ->activateStack ($ stack );
102+ }
95103 }
96104
97105 /**
98106 * {@inheritdoc}
99107 */
100108 public function sendRequest (RequestInterface $ request )
101109 {
102- $ stack = $ this ->collector ->getCurrentStack ();
110+ $ stack = $ this ->collector ->getActiveStack ();
111+
103112 $ this ->collectRequestInformations ($ request , $ stack );
104113 $ event = $ this ->stopwatch ->start ($ this ->getStopwatchEventName ($ request ));
105114
106115 try {
107116 $ response = $ this ->client ->sendRequest ($ request );
108- $ event ->stop ();
109-
110117 $ this ->collectResponseInformations ($ response , $ event , $ stack );
111118
112119 return $ response ;
113120 } catch (\Exception $ e ) {
114- $ event ->stop ();
115121 $ this ->collectExceptionInformations ($ e , $ event , $ stack );
116122
117123 throw $ e ;
124+ } catch (\Throwable $ e ) {
125+ $ this ->collectExceptionInformations ($ e , $ event , $ stack );
126+
127+ throw $ e ;
128+ } finally {
129+ $ event ->stop ();
118130 }
119131 }
120132
121133 /**
122134 * @param RequestInterface $request
123- * @param Stack|null $stack
135+ * @param Stack $stack
124136 */
125- private function collectRequestInformations (RequestInterface $ request , Stack $ stack = null )
137+ private function collectRequestInformations (RequestInterface $ request , Stack $ stack )
126138 {
127- if (null === $ stack ) {
128- return ;
129- }
130-
131139 $ stack ->setRequestTarget ($ request ->getRequestTarget ());
132140 $ stack ->setRequestMethod ($ request ->getMethod ());
133141 $ stack ->setRequestScheme ($ request ->getUri ()->getScheme ());
@@ -139,14 +147,10 @@ private function collectRequestInformations(RequestInterface $request, Stack $st
139147 /**
140148 * @param ResponseInterface $response
141149 * @param StopwatchEvent $event
142- * @param Stack|null $stack
150+ * @param Stack $stack
143151 */
144- private function collectResponseInformations (ResponseInterface $ response , StopwatchEvent $ event , Stack $ stack = null )
152+ private function collectResponseInformations (ResponseInterface $ response , StopwatchEvent $ event , Stack $ stack )
145153 {
146- if (null === $ stack ) {
147- return ;
148- }
149-
150154 $ stack ->setDuration ($ event ->getDuration ());
151155 $ stack ->setResponseCode ($ response ->getStatusCode ());
152156 $ stack ->setClientResponse ($ this ->formatter ->formatResponse ($ response ));
@@ -155,18 +159,14 @@ private function collectResponseInformations(ResponseInterface $response, Stopwa
155159 /**
156160 * @param \Exception $exception
157161 * @param StopwatchEvent $event
158- * @param Stack|null $stack
162+ * @param Stack $stack
159163 */
160- private function collectExceptionInformations (\Exception $ exception , StopwatchEvent $ event , Stack $ stack = null )
164+ private function collectExceptionInformations (\Exception $ exception , StopwatchEvent $ event , Stack $ stack )
161165 {
162166 if ($ exception instanceof HttpException) {
163167 $ this ->collectResponseInformations ($ exception ->getResponse (), $ event , $ stack );
164168 }
165169
166- if (null === $ stack ) {
167- return ;
168- }
169-
170170 $ stack ->setDuration ($ event ->getDuration ());
171171 $ stack ->setClientException ($ this ->formatter ->formatException ($ exception ));
172172 }
0 commit comments