From 6f9bb059e81b4bbb132e1e98aefa844d5cc6683d Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 23 Apr 2023 14:50:39 +0900 Subject: [PATCH] Use helper class to handle various error conditions during HTTP request --- common/framework/HTTP.php | 11 ++++- common/framework/helpers/HTTPHelper.php | 54 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 common/framework/helpers/HTTPHelper.php diff --git a/common/framework/HTTP.php b/common/framework/HTTP.php index 0dd8f37a1..8b940b972 100644 --- a/common/framework/HTTP.php +++ b/common/framework/HTTP.php @@ -168,10 +168,17 @@ class HTTP // Send the request. $start_time = microtime(true); - $response = $guzzle->request($method, $url, $settings); - $status_code = $response->getStatusCode() ?: 0; + try + { + $response = $guzzle->request($method, $url, $settings); + } + catch (\Throwable $e) + { + $response = new Helpers\HTTPHelper($e); + } // Measure elapsed time and add a debug entry. + $status_code = $response->getStatusCode() ?: 0; $elapsed_time = microtime(true) - $start_time; self::_debug($url, $status_code, $elapsed_time); diff --git a/common/framework/helpers/HTTPHelper.php b/common/framework/helpers/HTTPHelper.php new file mode 100644 index 000000000..6770e9355 --- /dev/null +++ b/common/framework/helpers/HTTPHelper.php @@ -0,0 +1,54 @@ +_exception_class = get_class($exception); + $this->_exception_code = $exception->getCode(); + $this->_error_message = $exception->getMessage(); + } + + /** + * Methods to implement ResponseInterface. + */ + public function getStatusCode(): int + { + return 0; + } + public function getReasonPhrase(): string + { + return $this->_error_message; + } + public function withStatus($code, $reasonPhrase = ''): ResponseInterface + { + $new = clone $this; + $new->_error_message = $reasonPhrase; + return $new; + } +}