Skip to content

Commit c8fbb77

Browse files
authored
[12.x] RequestException: attempt to summarize message before reporting (#57767)
* only summarize once * Update RequestException.php * Update RequestException.php * wip * wip * Update RequestException.php * Update HttpClientTest.php
1 parent 6f88214 commit c8fbb77

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/Illuminate/Http/Client/RequestException.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class RequestException extends HttpClientException
1616
/**
1717
* The current truncation length for the exception message.
1818
*
19-
* @var int|false
19+
* @var int|false|null
2020
*/
2121
public $truncateExceptionsAt;
2222

@@ -27,6 +27,13 @@ class RequestException extends HttpClientException
2727
*/
2828
public static $truncateAt = 120;
2929

30+
/**
31+
* Whether the response has been summarized in the message.
32+
*
33+
* @var bool
34+
*/
35+
public $hasBeenSummarized = false;
36+
3037
/**
3138
* Create a new exception instance.
3239
*
@@ -35,7 +42,7 @@ class RequestException extends HttpClientException
3542
*/
3643
public function __construct(Response $response, $truncateExceptionsAt = null)
3744
{
38-
parent::__construct("HTTP request returned status code {$response->status()}", $response->status());
45+
parent::__construct($this->prepareMessage($response), $response->status());
3946

4047
$this->truncateExceptionsAt = $truncateExceptionsAt;
4148

@@ -76,18 +83,35 @@ public static function dontTruncate()
7683
/**
7784
* Prepare the exception message.
7885
*
79-
* @return void
86+
* @param \Illuminate\Http\Client\Response $response
87+
* @return string
8088
*/
81-
public function report(): void
89+
protected function prepareMessage(Response $response)
8290
{
91+
$message = "HTTP request returned status code {$response->status()}";
92+
8393
$truncateExceptionsAt = $this->truncateExceptionsAt ?? static::$truncateAt;
8494

85-
$summary = $truncateExceptionsAt
86-
? Message::bodySummary($this->response->toPsrResponse(), $truncateExceptionsAt)
87-
: Message::toString($this->response->toPsrResponse());
95+
$summary = is_int($truncateExceptionsAt)
96+
? Message::bodySummary($response->toPsrResponse(), $truncateExceptionsAt)
97+
: Message::toString($response->toPsrResponse());
98+
99+
return is_null($summary) ? $message : $message . ":\n{$summary}\n";
100+
}
88101

89-
if (! is_null($summary)) {
90-
$this->message .= ":\n{$summary}\n";
102+
/**
103+
* Prepare the exception message.
104+
*
105+
* @return void
106+
*/
107+
public function report(): void
108+
{
109+
if ($this->hasBeenSummarized) {
110+
return;
91111
}
112+
113+
$this->message = $this->prepareMessage($this->response);
114+
115+
$this->hasBeenSummarized = true;
92116
}
93117
}

tests/Http/HttpClientTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,26 @@ public function testRequestExceptionEmptyBody()
14521452
throw new RequestException(new Response($response));
14531453
}
14541454

1455+
public function testReportingExceptionTwiceDoesNotIncludeSummaryTwice()
1456+
{
1457+
RequestException::dontTruncate();
1458+
1459+
$error = [
1460+
'error' => [
1461+
'code' => 403,
1462+
'message' => 'The Request can not be completed',
1463+
],
1464+
];
1465+
1466+
$response = new Psr7Response(403, [], json_encode($error));
1467+
1468+
$exception = new RequestException(new Response($response));
1469+
$exception->report();
1470+
$exception->report();
1471+
1472+
$this->assertEquals(1, substr_count($exception->getMessage(), '{"error":{"code":403,"message":"The Request can not be completed"}}'));
1473+
}
1474+
14551475
public function testOnErrorDoesntCallClosureOnInformational()
14561476
{
14571477
$status = 0;

0 commit comments

Comments
 (0)