feat: in debug mode, include part of http response in exception message (#3090)

This commit is contained in:
Dag 2022-10-29 08:42:50 +02:00 committed by GitHub
parent 85b87b9597
commit 1f576312ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 68 additions and 52 deletions

View File

@ -1,7 +1,8 @@
<?php
// todo: move this somewhere useful, possibly into a function
const RSSBRIDGE_HTTP_STATUS_CODES = [
final class Response
{
public const STATUS_CODES = [
'100' => 'Continue',
'101' => 'Switching Protocols',
'200' => 'OK',
@ -43,6 +44,7 @@ const RSSBRIDGE_HTTP_STATUS_CODES = [
'504' => 'Gateway Timeout',
'505' => 'HTTP Version Not Supported'
];
}
/**
* Fetch data from an http url
@ -135,16 +137,30 @@ function getContents(
$response['content'] = $cache->loadData();
break;
default:
if (Debug::isEnabled()) {
// Include a part of the response body in the exception message
throw new HttpException(
sprintf(
'%s resulted in `%s %s: %s`',
$url,
$result['code'],
Response::STATUS_CODES[$result['code']] ?? '',
mb_substr($result['body'], 0, 500),
),
$result['code']
);
} else {
throw new HttpException(
sprintf(
'%s resulted in `%s %s`',
$url,
$result['code'],
RSSBRIDGE_HTTP_STATUS_CODES[$result['code']] ?? ''
Response::STATUS_CODES[$result['code']] ?? '',
),
$result['code']
);
}
}
if ($returnFull === true) {
return $response;
}