@@ -211,55 +211,43 @@ private function wrapHandler(callable $handler): callable
211
211
$ this ->lastRequest ['request ' ] = $ request ;
212
212
213
213
// Send the request using the wrapped handler.
214
- $ response = Core::proxy (
215
- $ handler ($ request ),
216
- function ($ response ) use ($ connection , $ transport , $ request , $ options ) {
217
-
218
- $ this ->lastRequest ['response ' ] = $ response ;
219
-
220
- if (isset ($ response ['error ' ]) === true ) {
221
- if ($ response ['error ' ] instanceof ConnectException || $ response ['error ' ] instanceof RingException) {
222
- $ this ->log ->warning ("Curl exception encountered. " );
223
-
224
- $ exception = $ this ->getCurlRetryException ($ request , $ response );
225
-
226
- $ this ->logRequestFail (
227
- $ request ['http_method ' ],
228
- $ response ['effective_url ' ],
229
- $ request ['body ' ],
230
- $ request ['headers ' ],
231
- (int ) $ response ['status ' ],
232
- $ response ['body ' ],
233
- (float ) $ response ['transfer_stats ' ]['total_time ' ],
234
- $ exception
235
- );
236
-
237
- $ node = $ connection ->getHost ();
238
- $ this ->log ->warning ("Marking node $ node dead. " );
239
- $ connection ->markDead ();
240
-
241
- // If the transport has not been set, we are inside a Ping or Sniff,
242
- // so we don't want to retrigger retries anyway.
243
- //
244
- // TODO this could be handled better, but we are limited because connectionpools do not
245
- // have access to Transport. Architecturally, all of this needs to be refactored
246
- if (isset ($ transport ) === true ) {
247
- $ transport ->connectionPool ->scheduleCheck ();
248
-
249
- $ neverRetry = isset ($ request ['client ' ]['never_retry ' ]) ? $ request ['client ' ]['never_retry ' ] : false ;
250
- $ shouldRetry = $ transport ->shouldRetry ($ request );
251
- $ shouldRetryText = ($ shouldRetry ) ? 'true ' : 'false ' ;
252
-
253
- $ this ->log ->warning ("Retries left? $ shouldRetryText " );
254
- if ($ shouldRetry && !$ neverRetry ) {
255
- return $ transport ->performRequest (
256
- $ request ['http_method ' ],
257
- $ request ['uri ' ],
258
- [],
259
- $ request ['body ' ],
260
- $ options
261
- );
262
- }
214
+ $ response = Core::proxy ($ handler ($ request ), function ($ response ) use ($ connection , $ transport , $ request , $ options ) {
215
+
216
+ $ this ->lastRequest ['response ' ] = $ response ;
217
+
218
+ if (isset ($ response ['error ' ]) === true ) {
219
+ if ($ response ['error ' ] instanceof ConnectException || $ response ['error ' ] instanceof RingException) {
220
+ $ this ->log ->warning ("Curl exception encountered. " );
221
+
222
+ $ exception = $ this ->getCurlRetryException ($ request , $ response );
223
+
224
+ $ this ->logRequestFail ($ request , $ response , $ exception );
225
+
226
+ $ node = $ connection ->getHost ();
227
+ $ this ->log ->warning ("Marking node $ node dead. " );
228
+ $ connection ->markDead ();
229
+
230
+ // If the transport has not been set, we are inside a Ping or Sniff,
231
+ // so we don't want to retrigger retries anyway.
232
+ //
233
+ // TODO this could be handled better, but we are limited because connectionpools do not
234
+ // have access to Transport. Architecturally, all of this needs to be refactored
235
+ if (isset ($ transport ) === true ) {
236
+ $ transport ->connectionPool ->scheduleCheck ();
237
+
238
+ $ neverRetry = isset ($ request ['client ' ]['never_retry ' ]) ? $ request ['client ' ]['never_retry ' ] : false ;
239
+ $ shouldRetry = $ transport ->shouldRetry ($ request );
240
+ $ shouldRetryText = ($ shouldRetry ) ? 'true ' : 'false ' ;
241
+
242
+ $ this ->log ->warning ("Retries left? $ shouldRetryText " );
243
+ if ($ shouldRetry && !$ neverRetry ) {
244
+ return $ transport ->performRequest (
245
+ $ request ['http_method ' ],
246
+ $ request ['uri ' ],
247
+ [],
248
+ $ request ['body ' ],
249
+ $ options
250
+ );
263
251
}
264
252
265
253
$ this ->log ->warning ("Out of retries, throwing exception from $ node " );
@@ -281,7 +269,13 @@ function ($response) use ($connection, $transport, $request, $options) {
281
269
throw $ exception ;
282
270
}
283
271
} else {
284
- $ connection ->markAlive ();
272
+ // Something went seriously wrong, bail
273
+ $ exception = new TransportException ($ response ['error ' ]->getMessage ());
274
+ $ this ->logRequestFail ($ request , $ response , $ exception );
275
+ throw $ exception ;
276
+ }
277
+ } else {
278
+ $ connection ->markAlive ();
285
279
286
280
if (isset ($ response ['body ' ]) === true ) {
287
281
$ response ['body ' ] = stream_get_contents ($ response ['body ' ]);
@@ -316,7 +310,10 @@ function ($response) use ($connection, $transport, $request, $options) {
316
310
317
311
return isset ($ request ['client ' ]['verbose ' ]) && $ request ['client ' ]['verbose ' ] === true ? $ response : $ response ['body ' ];
318
312
}
319
- );
313
+ $ this ->logRequestSuccess ($ request , $ response );
314
+
315
+ return isset ($ request ['client ' ]['verbose ' ]) && $ request ['client ' ]['verbose ' ] === true ? $ response : $ response ['body ' ];
316
+ });
320
317
321
318
return $ response ;
322
319
};
@@ -349,87 +346,76 @@ function (&$value, &$key) {
349
346
/**
350
347
* Log a successful request
351
348
*
352
- * @param string $method
353
- * @param string $fullURI
354
- * @param string $body
355
- * @param array $headers
356
- * @param int $statusCode
357
- * @param array $response
358
- * @param float $duration
359
- *
349
+ * @param array $request
350
+ * @param array $response
360
351
* @return void
361
352
*/
362
- public function logRequestSuccess (string $ method , string $ fullURI , ? string $ body , array $ headers , ? int $ statusCode , array $ response, float $ duration ): void
353
+ public function logRequestSuccess ($ request , $ response)
363
354
{
364
- $ this ->log ->debug ('Request Body ' , array ($ body ));
355
+ $ this ->log ->debug ('Request Body ' , array ($ request [ ' body ' ] ));
365
356
$ this ->log ->info (
366
357
'Request Success: ' ,
367
358
array (
368
- 'method ' => $ method ,
369
- 'uri ' => $ fullURI ,
370
- 'headers ' => $ headers ,
371
- 'HTTP code ' => $ statusCode ,
372
- 'duration ' => $ duration ,
359
+ 'method ' => $ request [ ' http_method ' ] ,
360
+ 'uri ' => $ response [ ' effective_url ' ] ,
361
+ 'headers ' => $ request [ ' headers ' ] ,
362
+ 'HTTP code ' => $ response [ ' status ' ] ,
363
+ 'duration ' => $ response [ ' transfer_stats ' ][ ' total_time ' ] ,
373
364
)
374
365
);
375
- $ this ->log ->debug ('Response ' , array ($ response ));
366
+ $ this ->log ->debug ('Response ' , array ($ response[ ' body ' ] ));
376
367
377
368
// Build the curl command for Trace.
378
- $ curlCommand = $ this ->buildCurlCommand ($ method , $ fullURI , $ body );
369
+ $ curlCommand = $ this ->buildCurlCommand ($ request [ ' http_method ' ] , $ response [ ' effective_url ' ] , $ request [ ' body ' ] );
379
370
$ this ->trace ->info ($ curlCommand );
380
371
$ this ->trace ->debug (
381
372
'Response: ' ,
382
373
array (
383
- 'response ' => $ response ,
384
- 'method ' => $ method ,
385
- 'uri ' => $ fullURI ,
386
- 'HTTP code ' => $ statusCode ,
387
- 'duration ' => $ duration ,
374
+ 'response ' => $ response[ ' body ' ] ,
375
+ 'method ' => $ request [ ' http_method ' ] ,
376
+ 'uri ' => $ response [ ' effective_url ' ] ,
377
+ 'HTTP code ' => $ response [ ' status ' ] ,
378
+ 'duration ' => $ response [ ' transfer_stats ' ][ ' total_time ' ] ,
388
379
)
389
380
);
390
381
}
391
382
392
383
/**
393
- * Log a a failed request
384
+ * Log a failed request
394
385
*
395
- * @param string $method
396
- * @param string $fullURI
397
- * @param string $body
398
- * @param array $headers
399
- * @param null|int $statusCode
400
- * @param null|string $response
401
- * @param float $duration
402
- * @param \Exception $exception
386
+ * @param array $request
387
+ * @param array $response
388
+ * @param \Exception $exception
403
389
*
404
390
* @return void
405
391
*/
406
- public function logRequestFail (string $ method , string $ fullURI , ? string $ body , array $ headers , ? int $ statusCode , ? string $ response , float $ duration , \Exception $ exception ): void
392
+ public function logRequestFail ($ request , $ response , \Exception $ exception )
407
393
{
408
- $ this ->log ->debug ('Request Body ' , array ($ body ));
394
+ $ this ->log ->debug ('Request Body ' , array ($ request [ ' body ' ] ));
409
395
$ this ->log ->warning (
410
396
'Request Failure: ' ,
411
397
array (
412
- 'method ' => $ method ,
413
- 'uri ' => $ fullURI ,
414
- 'headers ' => $ headers ,
415
- 'HTTP code ' => $ statusCode ,
416
- 'duration ' => $ duration ,
398
+ 'method ' => $ request [ ' http_method ' ] ,
399
+ 'uri ' => $ response [ ' effective_url ' ] ,
400
+ 'headers ' => $ request [ ' headers ' ] ,
401
+ 'HTTP code ' => $ response [ ' status ' ] ,
402
+ 'duration ' => $ response [ ' transfer_stats ' ][ ' total_time ' ] ,
417
403
'error ' => $ exception ->getMessage (),
418
404
)
419
405
);
420
- $ this ->log ->warning ('Response ' , array ($ response ));
406
+ $ this ->log ->warning ('Response ' , array ($ response[ ' body ' ] ));
421
407
422
408
// Build the curl command for Trace.
423
- $ curlCommand = $ this ->buildCurlCommand ($ method , $ fullURI , $ body );
409
+ $ curlCommand = $ this ->buildCurlCommand ($ request [ ' http_method ' ] , $ response [ ' effective_url ' ] , $ request [ ' body ' ] );
424
410
$ this ->trace ->info ($ curlCommand );
425
411
$ this ->trace ->debug (
426
412
'Response: ' ,
427
413
array (
428
414
'response ' => $ response ,
429
- 'method ' => $ method ,
430
- 'uri ' => $ fullURI ,
431
- 'HTTP code ' => $ statusCode ,
432
- 'duration ' => $ duration ,
415
+ 'method ' => $ request [ ' http_method ' ] ,
416
+ 'uri ' => $ response [ ' effective_url ' ] ,
417
+ 'HTTP code ' => $ response [ ' status ' ] ,
418
+ 'duration ' => $ response [ ' transfer_stats ' ][ ' total_time ' ] ,
433
419
)
434
420
);
435
421
}
@@ -595,16 +581,7 @@ private function process4xxError(array $request, array $response, array $ignore)
595
581
$ exception = new BadRequest400Exception ($ responseBody , $ statusCode );
596
582
}
597
583
598
- $ this ->logRequestFail (
599
- $ request ['http_method ' ],
600
- $ response ['effective_url ' ],
601
- $ request ['body ' ],
602
- $ request ['headers ' ],
603
- (int ) $ response ['status ' ],
604
- $ response ['body ' ],
605
- (float ) $ response ['transfer_stats ' ]['total_time ' ],
606
- $ exception
607
- );
584
+ $ this ->logRequestFail ($ request , $ response , $ exception );
608
585
609
586
throw $ exception ;
610
587
}
@@ -637,16 +614,7 @@ private function process5xxError(array $request, array $response, array $ignore)
637
614
$ exception = new ServerErrorResponseException ($ responseBody , $ statusCode );
638
615
}
639
616
640
- $ this ->logRequestFail (
641
- $ request ['http_method ' ],
642
- $ response ['effective_url ' ],
643
- $ request ['body ' ],
644
- $ request ['headers ' ],
645
- (int ) $ response ['status ' ],
646
- $ response ['body ' ],
647
- (float ) $ response ['transfer_stats ' ]['total_time ' ],
648
- $ exception
649
- );
617
+ $ this ->logRequestFail ($ request , $ response , $ exception );
650
618
651
619
throw $ exception ;
652
620
}
0 commit comments