Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/Illuminate/Http/Client/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

/**
* Whether the response has been summarized in the message.
*
* @var bool
*/
public $hasBeenSummarized = false;

/**
* Create a new exception instance.
*
Expand All @@ -35,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;

Expand Down Expand Up @@ -76,18 +83,35 @@ public static function dontTruncate()
/**
* Prepare the exception message.
*
* @return void
* @param \Illuminate\Http\Client\Response $response
* @return string
*/
public function report(): void
protected function prepareMessage(Response $response)
{
$message = "HTTP request returned status code {$response->status()}";

$truncateExceptionsAt = $this->truncateExceptionsAt ?? static::$truncateAt;

$summary = $truncateExceptionsAt
? Message::bodySummary($this->response->toPsrResponse(), $truncateExceptionsAt)
: Message::toString($this->response->toPsrResponse());
$summary = is_int($truncateExceptionsAt)
? Message::bodySummary($response->toPsrResponse(), $truncateExceptionsAt)
: Message::toString($response->toPsrResponse());

return is_null($summary) ? $message : $message . ":\n{$summary}\n";
}

if (! is_null($summary)) {
$this->message .= ":\n{$summary}\n";
/**
* Prepare the exception message.
*
* @return void
*/
public function report(): void
{
if ($this->hasBeenSummarized) {
return;
}

$this->message = $this->prepareMessage($this->response);

$this->hasBeenSummarized = true;
}
}
20 changes: 20 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->assertEquals(1, substr_count($exception->getMessage(), '{"error":{"code":403,"message":"The Request can not be completed"}}'));
}

public function testOnErrorDoesntCallClosureOnInformational()
{
$status = 0;
Expand Down
Loading