From e26ca5d2270228a96b4170ced6b593fe981c5046 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Wed, 12 Nov 2025 17:46:41 -0500 Subject: [PATCH 1/7] only summarize once --- src/Illuminate/Http/Client/RequestException.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Illuminate/Http/Client/RequestException.php b/src/Illuminate/Http/Client/RequestException.php index 6f32fc9f64a0..f3b1ba87653e 100644 --- a/src/Illuminate/Http/Client/RequestException.php +++ b/src/Illuminate/Http/Client/RequestException.php @@ -27,6 +27,13 @@ class RequestException extends HttpClientException */ public static $truncateAt = 120; + /** + * Whether the response has been summarized in the message. + * + * @var bool + */ + protected $hasBeenSummarized = false; + /** * Create a new exception instance. * @@ -80,6 +87,10 @@ public static function dontTruncate() */ public function report(): void { + if ($this->hasBeenSummarized) { + return; + } + $truncateExceptionsAt = $this->truncateExceptionsAt ?? static::$truncateAt; $summary = $truncateExceptionsAt @@ -89,5 +100,7 @@ public function report(): void if (! is_null($summary)) { $this->message .= ":\n{$summary}\n"; } + + $this->hasBeenSummarized = true; } } From 7a4f79bfdcebbfd0567b91aa65b7d972aa97a9d2 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Wed, 12 Nov 2025 17:56:56 -0500 Subject: [PATCH 2/7] Update RequestException.php --- src/Illuminate/Http/Client/RequestException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Client/RequestException.php b/src/Illuminate/Http/Client/RequestException.php index f3b1ba87653e..41865c13a30e 100644 --- a/src/Illuminate/Http/Client/RequestException.php +++ b/src/Illuminate/Http/Client/RequestException.php @@ -16,7 +16,7 @@ class RequestException extends HttpClientException /** * The current truncation length for the exception message. * - * @var int|false + * @var int|false|null */ public $truncateExceptionsAt; From ac192041c853c2b260720b5b59ec42527fed2463 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Wed, 12 Nov 2025 18:15:25 -0500 Subject: [PATCH 3/7] Update RequestException.php --- src/Illuminate/Http/Client/RequestException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Client/RequestException.php b/src/Illuminate/Http/Client/RequestException.php index 41865c13a30e..2b3c33e551da 100644 --- a/src/Illuminate/Http/Client/RequestException.php +++ b/src/Illuminate/Http/Client/RequestException.php @@ -32,7 +32,7 @@ class RequestException extends HttpClientException * * @var bool */ - protected $hasBeenSummarized = false; + public $hasBeenSummarized = false; /** * Create a new exception instance. From 00d2278cc30351ff31f19363aa66d20642d0e49f Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 12 Nov 2025 18:36:34 -0500 Subject: [PATCH 4/7] wip --- .../Http/Client/RequestException.php | 29 ++++++++++++------- tests/Http/HttpClientTest.php | 20 +++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Http/Client/RequestException.php b/src/Illuminate/Http/Client/RequestException.php index 2b3c33e551da..34e76680e24e 100644 --- a/src/Illuminate/Http/Client/RequestException.php +++ b/src/Illuminate/Http/Client/RequestException.php @@ -42,7 +42,7 @@ class RequestException extends HttpClientException */ public function __construct(Response $response, $truncateExceptionsAt = null) { - parent::__construct("HTTP request returned status code {$response->status()}", $response->status()); + parent::__construct($this->prepareMessage($response), $response->status()); $this->truncateExceptionsAt = $truncateExceptionsAt; @@ -80,6 +80,23 @@ public static function dontTruncate() static::$truncateAt = false; } + /** + * Prepare the exception message. + * + * @param \Illuminate\Http\Client\Response $response + * @return string + */ + protected function prepareMessage(Response $response) + { + $message = "HTTP request returned status code {$response->status()}"; + + $summary = static::$truncateAt + ? Message::bodySummary($response->toPsrResponse(), static::$truncateAt) + : Message::toString($response->toPsrResponse()); + + return is_null($summary) ? $message : $message . ":\n{$summary}\n"; + } + /** * Prepare the exception message. * @@ -91,15 +108,7 @@ public function report(): void return; } - $truncateExceptionsAt = $this->truncateExceptionsAt ?? static::$truncateAt; - - $summary = $truncateExceptionsAt - ? Message::bodySummary($this->response->toPsrResponse(), $truncateExceptionsAt) - : Message::toString($this->response->toPsrResponse()); - - if (! is_null($summary)) { - $this->message .= ":\n{$summary}\n"; - } + $this->message = $this->prepareMessage($this->response); $this->hasBeenSummarized = true; } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 39c059dc9bd1..b9fd7edf8163 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -1452,6 +1452,26 @@ public function testRequestExceptionEmptyBody() throw new RequestException(new Response($response)); } + public function testReportingExceptionTwiceDoesNotIncludeSummaryTwice() + { + RequestException::dontTruncate(); + + $error = [ + 'error' => [ + 'code' => 403, + 'message' => 'The Request can not be completed', + ], + ]; + + $response = new Psr7Response(403, [], json_encode($error)); + + $exception = new RequestException(new Response($response)); + $exception->report(); + $exception->report(); + + $this->assertStringContainsString('{"error":{"code":403,"message":"The Request can not be completed"}}', $exception->getMessage()); + } + public function testOnErrorDoesntCallClosureOnInformational() { $status = 0; From 63f38da7ab76ab53517495f336a468aad118737e Mon Sep 17 00:00:00 2001 From: Luke Kuzmish Date: Wed, 12 Nov 2025 18:38:56 -0500 Subject: [PATCH 5/7] wip --- src/Illuminate/Http/Client/RequestException.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Client/RequestException.php b/src/Illuminate/Http/Client/RequestException.php index 34e76680e24e..d871e091adbe 100644 --- a/src/Illuminate/Http/Client/RequestException.php +++ b/src/Illuminate/Http/Client/RequestException.php @@ -90,8 +90,10 @@ protected function prepareMessage(Response $response) { $message = "HTTP request returned status code {$response->status()}"; - $summary = static::$truncateAt - ? Message::bodySummary($response->toPsrResponse(), static::$truncateAt) + $truncateExceptionsAt = $this->truncateExceptionsAt ?? static::$truncateAt; + + $summary = is_int($truncateExceptionsAt) + ? Message::bodySummary($response->toPsrResponse(), $truncateExceptionsAt) : Message::toString($response->toPsrResponse()); return is_null($summary) ? $message : $message . ":\n{$summary}\n"; From e404b4f824360985662d120731c53847d81935bd Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Thu, 13 Nov 2025 06:38:35 -0500 Subject: [PATCH 6/7] Update RequestException.php From f51324bce7e4a76b558407884ece35d6e11c6646 Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Thu, 13 Nov 2025 06:53:32 -0500 Subject: [PATCH 7/7] Update HttpClientTest.php --- tests/Http/HttpClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index b9fd7edf8163..e23f0ee9fca4 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -1469,7 +1469,7 @@ public function testReportingExceptionTwiceDoesNotIncludeSummaryTwice() $exception->report(); $exception->report(); - $this->assertStringContainsString('{"error":{"code":403,"message":"The Request can not be completed"}}', $exception->getMessage()); + $this->assertEquals(1, substr_count($exception->getMessage(), '{"error":{"code":403,"message":"The Request can not be completed"}}')); } public function testOnErrorDoesntCallClosureOnInformational()