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,48 +1,50 @@
<?php <?php
// todo: move this somewhere useful, possibly into a function final class Response
const RSSBRIDGE_HTTP_STATUS_CODES = [ {
'100' => 'Continue', public const STATUS_CODES = [
'101' => 'Switching Protocols', '100' => 'Continue',
'200' => 'OK', '101' => 'Switching Protocols',
'201' => 'Created', '200' => 'OK',
'202' => 'Accepted', '201' => 'Created',
'203' => 'Non-Authoritative Information', '202' => 'Accepted',
'204' => 'No Content', '203' => 'Non-Authoritative Information',
'205' => 'Reset Content', '204' => 'No Content',
'206' => 'Partial Content', '205' => 'Reset Content',
'300' => 'Multiple Choices', '206' => 'Partial Content',
'301' => 'Moved Permanently', '300' => 'Multiple Choices',
'302' => 'Found', '301' => 'Moved Permanently',
'303' => 'See Other', '302' => 'Found',
'304' => 'Not Modified', '303' => 'See Other',
'305' => 'Use Proxy', '304' => 'Not Modified',
'400' => 'Bad Request', '305' => 'Use Proxy',
'401' => 'Unauthorized', '400' => 'Bad Request',
'402' => 'Payment Required', '401' => 'Unauthorized',
'403' => 'Forbidden', '402' => 'Payment Required',
'404' => 'Not Found', '403' => 'Forbidden',
'405' => 'Method Not Allowed', '404' => 'Not Found',
'406' => 'Not Acceptable', '405' => 'Method Not Allowed',
'407' => 'Proxy Authentication Required', '406' => 'Not Acceptable',
'408' => 'Request Timeout', '407' => 'Proxy Authentication Required',
'409' => 'Conflict', '408' => 'Request Timeout',
'410' => 'Gone', '409' => 'Conflict',
'411' => 'Length Required', '410' => 'Gone',
'412' => 'Precondition Failed', '411' => 'Length Required',
'413' => 'Request Entity Too Large', '412' => 'Precondition Failed',
'414' => 'Request-URI Too Long', '413' => 'Request Entity Too Large',
'415' => 'Unsupported Media Type', '414' => 'Request-URI Too Long',
'416' => 'Requested Range Not Satisfiable', '415' => 'Unsupported Media Type',
'417' => 'Expectation Failed', '416' => 'Requested Range Not Satisfiable',
'429' => 'Too Many Requests', '417' => 'Expectation Failed',
'500' => 'Internal Server Error', '429' => 'Too Many Requests',
'501' => 'Not Implemented', '500' => 'Internal Server Error',
'502' => 'Bad Gateway', '501' => 'Not Implemented',
'503' => 'Service Unavailable', '502' => 'Bad Gateway',
'504' => 'Gateway Timeout', '503' => 'Service Unavailable',
'505' => 'HTTP Version Not Supported' '504' => 'Gateway Timeout',
]; '505' => 'HTTP Version Not Supported'
];
}
/** /**
* Fetch data from an http url * Fetch data from an http url
@ -135,15 +137,29 @@ function getContents(
$response['content'] = $cache->loadData(); $response['content'] = $cache->loadData();
break; break;
default: default:
throw new HttpException( if (Debug::isEnabled()) {
sprintf( // Include a part of the response body in the exception message
'%s resulted in `%s %s`', throw new HttpException(
$url, sprintf(
$result['code'], '%s resulted in `%s %s: %s`',
RSSBRIDGE_HTTP_STATUS_CODES[$result['code']] ?? '' $url,
), $result['code'],
$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'],
Response::STATUS_CODES[$result['code']] ?? '',
),
$result['code']
);
}
} }
if ($returnFull === true) { if ($returnFull === true) {
return $response; return $response;