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

View File

@ -1,7 +1,8 @@
<?php <?php
// todo: move this somewhere useful, possibly into a function final class Response
const RSSBRIDGE_HTTP_STATUS_CODES = [ {
public const STATUS_CODES = [
'100' => 'Continue', '100' => 'Continue',
'101' => 'Switching Protocols', '101' => 'Switching Protocols',
'200' => 'OK', '200' => 'OK',
@ -42,7 +43,8 @@ const RSSBRIDGE_HTTP_STATUS_CODES = [
'503' => 'Service Unavailable', '503' => 'Service Unavailable',
'504' => 'Gateway Timeout', '504' => 'Gateway Timeout',
'505' => 'HTTP Version Not Supported' '505' => 'HTTP Version Not Supported'
]; ];
}
/** /**
* Fetch data from an http url * Fetch data from an http url
@ -135,16 +137,30 @@ function getContents(
$response['content'] = $cache->loadData(); $response['content'] = $cache->loadData();
break; break;
default: 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( throw new HttpException(
sprintf( sprintf(
'%s resulted in `%s %s`', '%s resulted in `%s %s`',
$url, $url,
$result['code'], $result['code'],
RSSBRIDGE_HTTP_STATUS_CODES[$result['code']] ?? '' Response::STATUS_CODES[$result['code']] ?? '',
), ),
$result['code'] $result['code']
); );
} }
}
if ($returnFull === true) { if ($returnFull === true) {
return $response; return $response;
} }