1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-08-10 18:44:04 +02:00

feat: embed response in http exception (#3847)

This commit is contained in:
Dag
2023-12-20 03:16:25 +01:00
committed by GitHub
parent 0c6ffbf5a4
commit 98a94855dc
5 changed files with 59 additions and 16 deletions

View File

@@ -2,7 +2,29 @@
class HttpException extends \Exception
{
// todo: should include the failing http response (if present)
public ?Response $response;
public function __construct(string $message = '', int $statusCode = 0, ?Response $response = null)
{
parent::__construct($message, $statusCode);
$this->response = $response ?? new Response('', 0);
}
public static function fromResponse(Response $response, string $url): HttpException
{
$message = sprintf(
'%s resulted in %s %s %s',
$url,
$response->getCode(),
$response->getStatusLine(),
// If debug, include a part of the response body in the exception message
Debug::isEnabled() ? mb_substr($response->getBody(), 0, 500) : '',
);
if (CloudFlareException::isCloudFlareResponse($response)) {
return new CloudFlareException($message, $response->getCode(), $response);
}
return new HttpException(trim($message), $response->getCode(), $response);
}
}
final class CloudFlareException extends HttpException