Skip to content

Commit 00aa604

Browse files
committed
revert send changes, move to Response@toException
1 parent 7f5f58c commit 00aa604

File tree

2 files changed

+93
-64
lines changed

2 files changed

+93
-64
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -905,74 +905,59 @@ public function send(string $method, string $url, array $options = [])
905905
}
906906

907907
$shouldRetry = null;
908-
$originalTruncateAt = null;
909908

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);
956913

957-
$this->factory?->recordRequestResponsePair($request, null);
914+
$this->dispatchResponseReceivedEvent($response);
958915

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;
960921

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+
}
966924

967-
$shouldRetry = null;
925+
if ($this->throwCallback &&
926+
($this->throwIfCallback === null ||
927+
call_user_func($this->throwIfCallback, $response))) {
928+
$response->throw($this->throwCallback);
929+
}
968930

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;
974953
}
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+
});
976961
}
977962

978963
/**
@@ -1445,7 +1430,12 @@ public function mergeOptions(...$options)
14451430
*/
14461431
protected function newResponse($response)
14471432
{
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+
});
14491439
}
14501440

14511441
/**
@@ -1588,7 +1578,7 @@ public function truncateAt(int $length)
15881578
}
15891579

15901580
/**
1591-
* Do not truncate a RequestException.
1581+
* Do not truncate a RequestException if thrown.
15921582
*
15931583
* @return $this
15941584
*/

src/Illuminate/Http/Client/Response.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class Response implements ArrayAccess, Stringable
4747
*/
4848
public $transferStats;
4949

50+
/**
51+
* @var int<1, max>|false|null
52+
*/
53+
protected $truncateAt = null;
54+
5055
/**
5156
* Create a new response instance.
5257
*
@@ -289,6 +294,31 @@ public function toPsrResponse()
289294
return $this->response;
290295
}
291296

297+
/**
298+
* When a RequestException is thrown, truncate to this length.
299+
*
300+
* @param int<1, max> $length
301+
* @return $this
302+
*/
303+
public function truncateAt(int $length)
304+
{
305+
$this->truncateAt = $length;
306+
307+
return $this;
308+
}
309+
310+
/**
311+
* Do not truncate a RequestException if thrown.
312+
*
313+
* @return $this
314+
*/
315+
public function dontTruncate()
316+
{
317+
$this->truncateAt = false;
318+
319+
return $this;
320+
}
321+
292322
/**
293323
* Create an exception if a server or client error occurred.
294324
*
@@ -297,7 +327,16 @@ public function toPsrResponse()
297327
public function toException()
298328
{
299329
if ($this->failed()) {
300-
return new RequestException($this);
330+
$originalTruncateAt = RequestException::$truncateAt;
331+
try {
332+
if ($this->truncateAt !== null) {
333+
$this->truncateAt === false ? RequestException::dontTruncate() : RequestException::truncateAt($this->truncateAt);
334+
}
335+
336+
return new RequestException($this);
337+
} finally {
338+
RequestException::$truncateAt = $originalTruncateAt;
339+
}
301340
}
302341
}
303342

0 commit comments

Comments
 (0)