Skip to content

Commit

Permalink
[11.x] Add ability to customize or disable `Http\Client\RequestExcept…
Browse files Browse the repository at this point in the history
…ion` message truncation (#53734)

* Add ability to disable or customize exception message truncation

* Add tests for truncation

* Spacing

* Fix spacing

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
stevebauman and taylorotwell authored Dec 6, 2024
1 parent 5ed48d3 commit 69d3d07
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Illuminate/Foundation/Configuration/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Arr;

class Exceptions
Expand Down Expand Up @@ -200,4 +201,27 @@ public function stopIgnoring(array|string $class)

return $this;
}

/**
* Set the truncation length for request exception messages.
*
* @param int $length
* @return $this
*/
public function truncateRequestExceptionsAt(int $length)
{
RequestException::truncateAt($length);

return $this;
}

/**
* Disable truncation of request exception messages.
*
* @return $this
*/
public function dontTruncateRequestExceptions()
{
RequestException::dontTruncate();
}
}
42 changes: 41 additions & 1 deletion src/Illuminate/Http/Client/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class RequestException extends HttpClientException
*/
public $response;

/**
* The truncation length for the exception message.
*
* @var int|false
*/
public static $truncateAt = 120;

/**
* Create a new exception instance.
*
Expand All @@ -26,6 +33,37 @@ public function __construct(Response $response)
$this->response = $response;
}

/**
* Enable truncation of request exception messages.
*
* @return void
*/
public static function truncate()
{
static::$truncateAt = 120;
}

/**
* Set the truncation length for request exception messages.
*
* @param int $length
* @return void
*/
public static function truncateAt(int $length)
{
static::$truncateAt = $length;
}

/**
* Disable truncation of request exception messages.
*
* @return void
*/
public static function dontTruncate()
{
static::$truncateAt = false;
}

/**
* Prepare the exception message.
*
Expand All @@ -36,7 +74,9 @@ protected function prepareMessage(Response $response)
{
$message = "HTTP request returned status code {$response->status()}";

$summary = Message::bodySummary($response->toPsrResponse());
$summary = static::$truncateAt
? Message::bodySummary($response->toPsrResponse(), static::$truncateAt)
: Message::toString($response->toPsrResponse());

return is_null($summary) ? $message : $message .= ":\n{$summary}\n";
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ protected function setUp(): void
parent::setUp();

$this->factory = new Factory;

RequestException::truncate();
}

protected function tearDown(): void
Expand Down Expand Up @@ -1257,6 +1259,42 @@ public function testRequestExceptionTruncatedSummary()
throw new RequestException(new Response($response));
}

public function testRequestExceptionWithoutTruncatedSummary()
{
RequestException::dontTruncate();

$this->expectException(RequestException::class);
$this->expectExceptionMessage('{"error":{"code":403,"message":"The Request can not be completed because quota limit was exceeded. Please, check our support team to increase your limit');

$error = [
'error' => [
'code' => 403,
'message' => 'The Request can not be completed because quota limit was exceeded. Please, check our support team to increase your limit',
],
];
$response = new Psr7Response(403, [], json_encode($error));

throw new RequestException(new Response($response));
}

public function testRequestExceptionWithCustomTruncatedSummary()
{
RequestException::truncateAt(60);

$this->expectException(RequestException::class);
$this->expectExceptionMessage('{"error":{"code":403,"message":"The Request can not be compl (truncated...)');

$error = [
'error' => [
'code' => 403,
'message' => 'The Request can not be completed because quota limit was exceeded. Please, check our support team to increase your limit',
],
];
$response = new Psr7Response(403, [], json_encode($error));

throw new RequestException(new Response($response));
}

public function testRequestExceptionEmptyBody()
{
$this->expectException(RequestException::class);
Expand Down

0 comments on commit 69d3d07

Please sign in to comment.