Skip to content

Commit e8156d8

Browse files
authored
[12.x] Fix setting request exception truncating doesn't work on HTTP layer when configured inside bootstrap/app.php (#57759)
* [12.x] Fix setting request exception truncating doesn't work on HTTP layer when configured inside `bootstrap/app.php` fix #57601 Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent f5514bc commit e8156d8

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

src/Illuminate/Http/Client/RequestException.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ class RequestException extends HttpClientException
1414
public $response;
1515

1616
/**
17-
* The truncation length for the exception message.
17+
* The current truncation length for the exception message.
18+
*
19+
* @var int|false
20+
*/
21+
public $truncateExceptionsAt;
22+
23+
/**
24+
* The global truncation length for the exception message.
1825
*
1926
* @var int|false
2027
*/
@@ -24,10 +31,13 @@ class RequestException extends HttpClientException
2431
* Create a new exception instance.
2532
*
2633
* @param \Illuminate\Http\Client\Response $response
34+
* @param int|false|null $truncateExceptionsAt
2735
*/
28-
public function __construct(Response $response)
36+
public function __construct(Response $response, $truncateExceptionsAt = null)
2937
{
30-
parent::__construct($this->prepareMessage($response), $response->status());
38+
parent::__construct("HTTP request returned status code {$response->status()}", $response->status());
39+
40+
$this->truncateExceptionsAt = $truncateExceptionsAt;
3141

3242
$this->response = $response;
3343
}
@@ -66,17 +76,18 @@ public static function dontTruncate()
6676
/**
6777
* Prepare the exception message.
6878
*
69-
* @param \Illuminate\Http\Client\Response $response
70-
* @return string
79+
* @return void
7180
*/
72-
protected function prepareMessage(Response $response)
81+
public function report(): void
7382
{
74-
$message = "HTTP request returned status code {$response->status()}";
83+
$truncateExceptionsAt = $this->truncateExceptionsAt ?? static::$truncateAt;
7584

76-
$summary = static::$truncateAt
77-
? Message::bodySummary($response->toPsrResponse(), static::$truncateAt)
78-
: Message::toString($response->toPsrResponse());
85+
$summary = $truncateExceptionsAt
86+
? Message::bodySummary($this->response->toPsrResponse(), $truncateExceptionsAt)
87+
: Message::toString($this->response->toPsrResponse());
7988

80-
return is_null($summary) ? $message : $message .= ":\n{$summary}\n";
89+
if (! is_null($summary)) {
90+
$this->message .= ":\n{$summary}\n";
91+
}
8192
}
8293
}

src/Illuminate/Http/Client/Response.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,7 @@ public function toPsrResponse()
304304
public function toException()
305305
{
306306
if ($this->failed()) {
307-
$originalTruncateAt = RequestException::$truncateAt;
308-
309-
try {
310-
if ($this->truncateExceptionsAt !== null) {
311-
$this->truncateExceptionsAt === false
312-
? RequestException::dontTruncate()
313-
: RequestException::truncateAt($this->truncateExceptionsAt);
314-
}
315-
316-
return new RequestException($this);
317-
} finally {
318-
RequestException::$truncateAt = $originalTruncateAt;
319-
}
307+
return new RequestException($this, $this->truncateExceptionsAt);
320308
}
321309
}
322310

tests/Http/HttpClientTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,9 +1301,10 @@ public function testRequestExceptionSummary()
13011301
'message' => 'The Request can not be completed',
13021302
],
13031303
];
1304+
13041305
$response = new Psr7Response(403, [], json_encode($error));
13051306

1306-
throw new RequestException(new Response($response));
1307+
throw tap(new RequestException(new Response($response)), fn ($exception) => $exception->report());
13071308
}
13081309

13091310
public function testRequestExceptionTruncatedSummary()
@@ -1319,7 +1320,7 @@ public function testRequestExceptionTruncatedSummary()
13191320
];
13201321
$response = new Psr7Response(403, [], json_encode($error));
13211322

1322-
throw new RequestException(new Response($response));
1323+
throw tap(new RequestException(new Response($response)), fn ($exception) => $exception->report());
13231324
}
13241325

13251326
public function testRequestExceptionWithoutTruncatedSummary()
@@ -1337,7 +1338,7 @@ public function testRequestExceptionWithoutTruncatedSummary()
13371338
];
13381339
$response = new Psr7Response(403, [], json_encode($error));
13391340

1340-
throw new RequestException(new Response($response));
1341+
throw tap(new RequestException(new Response($response)), fn ($exception) => $exception->report());
13411342
}
13421343

13431344
public function testRequestExceptionWithCustomTruncatedSummary()
@@ -1355,7 +1356,7 @@ public function testRequestExceptionWithCustomTruncatedSummary()
13551356
];
13561357
$response = new Psr7Response(403, [], json_encode($error));
13571358

1358-
throw new RequestException(new Response($response));
1359+
throw tap(new RequestException(new Response($response)), fn ($exception) => $exception->report());
13591360
}
13601361

13611362
public function testRequestLevelTruncationLevelOnRequestException()
@@ -1373,6 +1374,8 @@ public function testRequestLevelTruncationLevelOnRequestException()
13731374
$exception = $e;
13741375
}
13751376

1377+
$exception->report();
1378+
13761379
$this->assertEquals("HTTP request returned status code 403:\n[\"e (truncated...)\n", $exception->getMessage());
13771380

13781381
$this->assertEquals(60, RequestException::$truncateAt);
@@ -1394,6 +1397,8 @@ public function testNoTruncationOnRequestLevel()
13941397
$exception = $e;
13951398
}
13961399

1400+
$exception->report();
1401+
13971402
$this->assertEquals("HTTP request returned status code 403:\nHTTP/1.1 403 Forbidden\r\nContent-Type: application/json\r\n\r\n[\"error\"]\n", $exception->getMessage());
13981403

13991404
$this->assertEquals(60, RequestException::$truncateAt);
@@ -1414,6 +1419,8 @@ public function testRequestExceptionDoesNotTruncateButRequestDoes()
14141419
$exception = $e;
14151420
}
14161421

1422+
$exception->report();
1423+
14171424
$this->assertEquals("HTTP request returned status code 403:\n[\"e (truncated...)\n", $exception->getMessage());
14181425

14191426
$this->assertFalse(RequestException::$truncateAt);
@@ -1428,6 +1435,8 @@ public function testAsyncRequestExceptionsRespectRequestTruncation()
14281435

14291436
$exception = $this->factory->async()->throw()->truncateExceptionsAt(4)->get('http://foo.com/json')->wait();
14301437

1438+
$exception->report();
1439+
14311440
$this->assertInstanceOf(RequestException::class, $exception);
14321441
$this->assertEquals("HTTP request returned status code 403:\n[\"er (truncated...)\n", $exception->getMessage());
14331442
$this->assertFalse(RequestException::$truncateAt);

0 commit comments

Comments
 (0)