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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 16 deletions

View File

@ -33,7 +33,15 @@ class GettrBridge extends BridgeAbstract
$user,
min($this->getInput('limit'), 20)
);
$data = json_decode(getContents($api), false);
try {
$json = getContents($api);
} catch (HttpException $e) {
if ($e->getCode() === 400 && str_contains($e->response->getBody(), 'E_USER_NOTFOUND')) {
throw new \Exception('User not found: ' . $user);
}
throw $e;
}
$data = json_decode($json, false);
foreach ($data->result->aux->post as $post) {
$this->items[] = [

View File

@ -47,7 +47,8 @@ enable_debug_mode = false
enable_maintenance_mode = false
[http]
timeout = 60
; Operation timeout in seconds
timeout = 30
useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0"
; Max http response size in MB

View File

@ -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

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

View File

@ -16,6 +16,13 @@
</p>
<?php endif; ?>
<?php if ($e->getCode() === 400): ?>
<h2>400 Bad Request</h2>
<p>
This is usually caused by an incorrectly constructed http request.
</p>
<?php endif; ?>
<?php if ($e->getCode() === 404): ?>
<h2>404 Page Not Found</h2>
<p>
@ -40,6 +47,22 @@
</p>
<?php endif; ?>
<?php if ($e->getCode() === 0): ?>
<p>
See
<a href="https://curl.haxx.se/libcurl/c/libcurl-errors.html">
https://curl.haxx.se/libcurl/c/libcurl-errors.html
</a>
for description of the curl error code.
</p>
<?php else: ?>
<p>
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/<?= raw($e->getCode()) ?>">
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/<?= raw($e->getCode()) ?>
</a>
</p>
<?php endif; ?>
<?php else: ?>
<?php if ($e->getCode() === 10): ?>
<h2>The rss feed is completely empty</h2>