mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-18 22:31:42 +02:00
feat: embed response in http exception (#3847)
This commit is contained in:
@@ -101,19 +101,8 @@ function getContents(
|
||||
$response = $response->withBody($cachedResponse->getBody());
|
||||
break;
|
||||
default:
|
||||
$exceptionMessage = 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)) {
|
||||
throw new CloudFlareException($exceptionMessage, $response->getCode());
|
||||
}
|
||||
throw new HttpException(trim($exceptionMessage), $response->getCode());
|
||||
$e = HttpException::fromResponse($response, $url);
|
||||
throw $e;
|
||||
}
|
||||
if ($returnFull === true) {
|
||||
// todo: return the actual response object
|
||||
|
24
lib/http.php
24
lib/http.php
@@ -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
|
||||
|
Reference in New Issue
Block a user