From 01703484296889869dc8c9fe77532cd648c5954d Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 12 Jun 2020 17:05:31 -0400 Subject: [PATCH] Handle all assigned HTTP error codes uniformly --- lib/Exception.php | 46 ++++++++++++++++++++++++++++++----- lib/HttpClient/Exception.php | 14 +++++++++++ lib/HttpClient/HttpClient.php | 4 ++- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/lib/Exception.php b/lib/Exception.php index ad1bef9..7a1d222 100644 --- a/lib/Exception.php +++ b/lib/Exception.php @@ -17,17 +17,51 @@ abstract class Exception extends \Exception { 'notJSONFeed' => [0x1113, "Document is not a JSON Feed document"], 'notXMLFeed' => [0x1113, "Document is not a newsfeed"], // Fetching: 0x1200 - 'badRequest' => [0x1201, "Client request was not accaptable to the server"], - 'notFound' => [0x1202, "Resource was not found on server"], - 'notAuthorized' => [0x1203, "Supplied credentials are insufficient to access the resource"], + 'httpStatus400' => [0x1201, "Client request was not acceptable to the server (code 400)"], + 'httpStatus401' => [0x1202, "Supplied credentials are insufficient to access the resource (code 401)"], + 'httpStatus402' => [0x1201, "Client request was not acceptable to the server (code 402)"], + 'httpStatus403' => [0x1212, "Access to the resource is forbidden (code 403)"], + 'httpStatus404' => [0x1203, "The requested resource was not found on the server (code 404)"], + 'httpStatus405' => [0x1201, "Client request was not acceptable to the server (code 405)"], + 'httpStatus406' => [0x1201, "Client request was not acceptable to the server (code 406)"], + 'httpStatus407' => [0x1201, "Client request was not acceptable to the server (code 407)"], + 'httpStatus408' => [0x1201, "Client request was not acceptable to the server (code 408)"], + 'httpStatus409' => [0x1201, "Client request was not acceptable to the server (code 409)"], + 'httpStatus410' => [0x1203, "The requested resource was not found on the server (code 410)"], + 'httpStatus411' => [0x1201, "Client request was not acceptable to the server (code 411)"], + 'httpStatus412' => [0x1201, "Client request was not acceptable to the server (code 412)"], + 'httpStatus413' => [0x1201, "Client request was not acceptable to the server (code 413)"], + 'httpStatus414' => [0x1201, "Client request was not acceptable to the server (code 414)"], + 'httpStatus415' => [0x1201, "Client request was not acceptable to the server (code 415)"], + 'httpStatus416' => [0x1201, "Client request was not acceptable to the server (code 416)"], + 'httpStatus417' => [0x1201, "Client request was not acceptable to the server (code 417)"], + 'httpStatus421' => [0x1201, "Client request was not acceptable to the server (code 421)"], + 'httpStatus422' => [0x1201, "Client request was not acceptable to the server (code 422)"], + 'httpStatus423' => [0x1201, "Client request was not acceptable to the server (code 423)"], + 'httpStatus424' => [0x1201, "Client request was not acceptable to the server (code 424)"], + 'httpStatus425' => [0x1201, "Client request was not acceptable to the server (code 425)"], + 'httpStatus426' => [0x1201, "Client request was not acceptable to the server (code 426)"], + 'httpStatus428' => [0x1201, "Client request was not acceptable to the server (code 428)"], + 'httpStatus429' => [0x1201, "Client request was not acceptable to the server (code 429)"], + 'httpStatus431' => [0x1201, "Client request was not acceptable to the server (code 431)"], + 'httpStatus451' => [0x1212, "Access to the resource is forbidden (code 451)"], + 'httpStatus500' => [0x1211, "The server reported an error (code 500)"], + 'httpStatus501' => [0x1211, "The server reported an error (code 501)"], + 'httpStatus502' => [0x1211, "The server reported an error (code 502)"], + 'httpStatus503' => [0x1211, "The server reported an error (code 503)"], + 'httpStatus504' => [0x1211, "The server reported an error (code 504)"], + 'httpStatus505' => [0x1211, "The server reported an error (code 505)"], + 'httpStatus506' => [0x1211, "The server reported an error (code 506)"], + 'httpStatus507' => [0x1211, "The server reported an error (code 507)"], + 'httpStatus508' => [0x1211, "The server reported an error (code 508)"], + 'httpStatus510' => [0x1211, "The server reported an error (code 510)"], + 'httpStatus511' => [0x1211, "The server reported an error (code 511)"], 'tooManyRedirects' => [0x1204, "The configured number of redirects was exceeded while trying to access the resource"], - 'forbidden' => [0x1211, "Access to the resource is forbidden"], - 'serverError' => [0x1212, "The server reported an error"], ]; public function __construct(string $symbol, \Exception $e = null) { $data = self::SYMBOLS[$symbol] ?? null; - assert(is_array($data)); + assert(is_array($data), "Error symbol is not defined"); [$code, $msg] = $data; parent::__construct($msg, $code, $e); } diff --git a/lib/HttpClient/Exception.php b/lib/HttpClient/Exception.php index 437b1f3..f8ff24a 100644 --- a/lib/HttpClient/Exception.php +++ b/lib/HttpClient/Exception.php @@ -9,4 +9,18 @@ namespace MensBeam\Lax\HttpClient; use MensBeam\Lax\Exception as BaseException; class Exception extends BaseException { + public function __construct(string $symbol, \Exception $e = null) { + if (preg_match("/^httpStatus(\d+)$/", $symbol, $match)) { + $code = (int) $match[1]; + assert($code >= 400, "HTTP status codes under 400 should not produce exceptions"); + if (($code < 500 && $code > 431 && $code !== 451) || in_array($code, [418, 419, 420, 430])) { + // unassigned 4xx code are changed to 400 + $symbol = "httpStatus400"; + } elseif ($code > 511 || $code === 509) { + // unassigned 5xx codes and anything 600 or above is changed to 500 + $symbol = "httpStatus500"; + } + } + parent::__construct($symbol, $e); + } } diff --git a/lib/HttpClient/HttpClient.php b/lib/HttpClient/HttpClient.php index d37c649..5dca90d 100644 --- a/lib/HttpClient/HttpClient.php +++ b/lib/HttpClient/HttpClient.php @@ -35,8 +35,10 @@ class HttpClient implements RequestFactoryInterface, ClientInterface { for ($a = 0; $a <= $stop; $a++) { $response = $this->client->sendRequest($request); $code = $response->getStatusCode(); - if ($code < 300 || $code >= 400 || $code === 304) { + if ($code < 300 || $code === 304) { return $response; + } elseif ($code >= 400) { + throw new Exception("httpStatus".$code); } else { $loc = $response->getHeader("Location"); for ($b = 0; $b < sizeof($loc); $b++) {