@@ -905,74 +905,59 @@ public function send(string $method, string $url, array $options = [])
905
905
}
906
906
907
907
$ shouldRetry = null ;
908
- $ originalTruncateAt = null ;
909
908
910
- try {
911
- if ($ this ->truncateAt !== null ) {
912
- $ originalTruncateAt = LaravelRequestException::$ truncateAt ;
913
- $ this ->truncateAt === false ? LaravelRequestException::dontTruncate () : LaravelRequestException::truncateAt ($ this ->truncateAt );
914
- }
915
- return retry ($ this ->tries ?? 1 , function ($ attempt ) use ($ method , $ url , $ options , &$ shouldRetry ) {
916
- try {
917
- return tap ($ this ->newResponse ($ this ->sendRequest ($ method , $ url , $ options )),
918
- function ($ response ) use ($ attempt , &$ shouldRetry ) {
919
- $ this ->populateResponse ($ response );
920
-
921
- $ this ->dispatchResponseReceivedEvent ($ response );
922
-
923
- if (!$ response ->successful ()) {
924
- try {
925
- $ shouldRetry = $ this ->retryWhenCallback ? call_user_func ($ this ->retryWhenCallback ,
926
- $ response ->toException (), $ this ,
927
- $ this ->request ->toPsrRequest ()->getMethod ()) : true ;
928
- } catch (Exception $ exception ) {
929
- $ shouldRetry = false ;
930
-
931
- throw $ exception ;
932
- }
933
-
934
- if ($ this ->throwCallback &&
935
- ($ this ->throwIfCallback === null ||
936
- call_user_func ($ this ->throwIfCallback , $ response ))) {
937
- $ response ->throw ($ this ->throwCallback );
938
- }
939
-
940
- $ potentialTries = is_array ($ this ->tries )
941
- ? count ($ this ->tries ) + 1
942
- : $ this ->tries ;
943
-
944
- if ($ attempt < $ potentialTries && $ shouldRetry ) {
945
- $ response ->throw ();
946
- }
947
-
948
- if ($ potentialTries > 1 && $ this ->retryThrow ) {
949
- $ response ->throw ();
950
- }
951
- }
952
- });
953
- } catch (ConnectException $ e ) {
954
- $ exception = new ConnectionException ($ e ->getMessage (), 0 , $ e );
955
- $ request = new Request ($ e ->getRequest ());
909
+ return retry ($ this ->tries ?? 1 , function ($ attempt ) use ($ method , $ url , $ options , &$ shouldRetry ) {
910
+ try {
911
+ return tap ($ this ->newResponse ($ this ->sendRequest ($ method , $ url , $ options )), function ($ response ) use ($ attempt , &$ shouldRetry ) {
912
+ $ this ->populateResponse ($ response );
956
913
957
- $ this ->factory ?->recordRequestResponsePair( $ request , null );
914
+ $ this ->dispatchResponseReceivedEvent ( $ response );
958
915
959
- $ this ->dispatchConnectionFailedEvent ($ request , $ exception );
916
+ if (! $ response ->successful ()) {
917
+ try {
918
+ $ shouldRetry = $ this ->retryWhenCallback ? call_user_func ($ this ->retryWhenCallback , $ response ->toException (), $ this , $ this ->request ->toPsrRequest ()->getMethod ()) : true ;
919
+ } catch (Exception $ exception ) {
920
+ $ shouldRetry = false ;
960
921
961
- throw $ exception ;
962
- }
963
- }, $ this ->retryDelay ?? 100 , function ($ exception ) use (&$ shouldRetry ) {
964
- $ result = $ shouldRetry ?? ($ this ->retryWhenCallback ? call_user_func ($ this ->retryWhenCallback ,
965
- $ exception , $ this , $ this ->request ?->toPsrRequest()->getMethod ()) : true );
922
+ throw $ exception ;
923
+ }
966
924
967
- $ shouldRetry = null ;
925
+ if ($ this ->throwCallback &&
926
+ ($ this ->throwIfCallback === null ||
927
+ call_user_func ($ this ->throwIfCallback , $ response ))) {
928
+ $ response ->throw ($ this ->throwCallback );
929
+ }
968
930
969
- return $ result ;
970
- });
971
- } finally {
972
- if ($ originalTruncateAt !== null ) {
973
- $ originalTruncateAt === false ? LaravelRequestException::dontTruncate () : LaravelRequestException::truncateAt ($ originalTruncateAt );
931
+ $ potentialTries = is_array ($ this ->tries )
932
+ ? count ($ this ->tries ) + 1
933
+ : $ this ->tries ;
934
+
935
+ if ($ attempt < $ potentialTries && $ shouldRetry ) {
936
+ $ response ->throw ();
937
+ }
938
+
939
+ if ($ potentialTries > 1 && $ this ->retryThrow ) {
940
+ $ response ->throw ();
941
+ }
942
+ }
943
+ });
944
+ } catch (ConnectException $ e ) {
945
+ $ exception = new ConnectionException ($ e ->getMessage (), 0 , $ e );
946
+ $ request = new Request ($ e ->getRequest ());
947
+
948
+ $ this ->factory ?->recordRequestResponsePair($ request , null );
949
+
950
+ $ this ->dispatchConnectionFailedEvent ($ request , $ exception );
951
+
952
+ throw $ exception ;
974
953
}
975
- }
954
+ }, $ this ->retryDelay ?? 100 , function ($ exception ) use (&$ shouldRetry ) {
955
+ $ result = $ shouldRetry ?? ($ this ->retryWhenCallback ? call_user_func ($ this ->retryWhenCallback , $ exception , $ this , $ this ->request ?->toPsrRequest()->getMethod ()) : true );
956
+
957
+ $ shouldRetry = null ;
958
+
959
+ return $ result ;
960
+ });
976
961
}
977
962
978
963
/**
@@ -1445,7 +1430,12 @@ public function mergeOptions(...$options)
1445
1430
*/
1446
1431
protected function newResponse ($ response )
1447
1432
{
1448
- return new Response ($ response );
1433
+ return tap (new Response ($ response ), function (Response $ laravelResponse ) {
1434
+ if ($ this ->truncateAt === null ) {
1435
+ return ;
1436
+ }
1437
+ $ this ->truncateAt === false ? $ laravelResponse ->dontTruncate () : $ laravelResponse ->truncateAt ($ this ->truncateAt );
1438
+ });
1449
1439
}
1450
1440
1451
1441
/**
@@ -1588,7 +1578,7 @@ public function truncateAt(int $length)
1588
1578
}
1589
1579
1590
1580
/**
1591
- * Do not truncate a RequestException.
1581
+ * Do not truncate a RequestException if thrown .
1592
1582
*
1593
1583
* @return $this
1594
1584
*/
0 commit comments