Skip to content
Draft
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
33 changes: 11 additions & 22 deletions src/Illuminate/Http/Client/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ class RequestException extends HttpClientException
public $response;

/**
* The current truncation length for the exception message.
*
* @var int|false
*/
public $truncateExceptionsAt;

/**
* The global truncation length for the exception message.
* The truncation length for the exception message.
*
* @var int|false
*/
Expand All @@ -31,13 +24,10 @@ class RequestException extends HttpClientException
* Create a new exception instance.
*
* @param \Illuminate\Http\Client\Response $response
* @param int|false|null $truncateExceptionsAt
*/
public function __construct(Response $response, $truncateExceptionsAt = null)
public function __construct(Response $response)
{
parent::__construct("HTTP request returned status code {$response->status()}", $response->status());

$this->truncateExceptionsAt = $truncateExceptionsAt;
parent::__construct($this->prepareMessage($response), $response->status());

$this->response = $response;
}
Expand Down Expand Up @@ -76,18 +66,17 @@ 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)
{
$truncateExceptionsAt = $this->truncateExceptionsAt ?? static::$truncateAt;
$message = "HTTP request returned status code {$response->status()}";

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

if (! is_null($summary)) {
$this->message .= ":\n{$summary}\n";
}
return is_null($summary) ? $message : $message .= ":\n{$summary}\n";
}
}
14 changes: 13 additions & 1 deletion src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,19 @@ public function toPsrResponse()
public function toException()
{
if ($this->failed()) {
return new RequestException($this, $this->truncateExceptionsAt);
$originalTruncateAt = RequestException::$truncateAt;

try {
if ($this->truncateExceptionsAt !== null) {
$this->truncateExceptionsAt === false
? RequestException::dontTruncate()
: RequestException::truncateAt($this->truncateExceptionsAt);
}

return new RequestException($this);
} finally {
RequestException::$truncateAt = $originalTruncateAt;
}
}
}

Expand Down
17 changes: 4 additions & 13 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1301,10 +1301,9 @@ public function testRequestExceptionSummary()
'message' => 'The Request can not be completed',
],
];

$response = new Psr7Response(403, [], json_encode($error));

throw tap(new RequestException(new Response($response)), fn ($exception) => $exception->report());
throw new RequestException(new Response($response));
}

public function testRequestExceptionTruncatedSummary()
Expand All @@ -1320,7 +1319,7 @@ public function testRequestExceptionTruncatedSummary()
];
$response = new Psr7Response(403, [], json_encode($error));

throw tap(new RequestException(new Response($response)), fn ($exception) => $exception->report());
throw new RequestException(new Response($response));
}

public function testRequestExceptionWithoutTruncatedSummary()
Expand All @@ -1338,7 +1337,7 @@ public function testRequestExceptionWithoutTruncatedSummary()
];
$response = new Psr7Response(403, [], json_encode($error));

throw tap(new RequestException(new Response($response)), fn ($exception) => $exception->report());
throw new RequestException(new Response($response));
}

public function testRequestExceptionWithCustomTruncatedSummary()
Expand All @@ -1356,7 +1355,7 @@ public function testRequestExceptionWithCustomTruncatedSummary()
];
$response = new Psr7Response(403, [], json_encode($error));

throw tap(new RequestException(new Response($response)), fn ($exception) => $exception->report());
throw new RequestException(new Response($response));
}

public function testRequestLevelTruncationLevelOnRequestException()
Expand All @@ -1374,8 +1373,6 @@ public function testRequestLevelTruncationLevelOnRequestException()
$exception = $e;
}

$exception->report();

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

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

$exception->report();

$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());

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

$exception->report();

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

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

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

$exception->report();

$this->assertInstanceOf(RequestException::class, $exception);
$this->assertEquals("HTTP request returned status code 403:\n[\"er (truncated...)\n", $exception->getMessage());
$this->assertFalse(RequestException::$truncateAt);
Expand Down
Loading