From 5e7543ee05314b54436df0ac82ef46586fda5f04 Mon Sep 17 00:00:00 2001 From: Aaron Bauman <aaron@messageagency.com> Date: Fri, 30 Dec 2016 14:13:17 -0500 Subject: [PATCH] Tweak RestClient response handling for readability --- src/Rest/RestClient.php | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Rest/RestClient.php b/src/Rest/RestClient.php index b3638f28..1c81b578 100644 --- a/src/Rest/RestClient.php +++ b/src/Rest/RestClient.php @@ -90,29 +90,27 @@ class RestClient { catch (RequestException $e) { // RequestException gets thrown for any response status but 2XX. $this->response = $e->getResponse(); - switch ($this->response->getStatusCode()) { - case 401: - // The session ID or OAuth token used has expired or is invalid: refresh - // token. If refreshToken() throws an exception, or if apiHttpRequest() - // throws anything but a RequestException, let it bubble up. - $this->refreshToken(); - try { - $this->response = new RestResponse($this->apiHttpRequest($path, $params, $method)); - } - catch (RequestException $e) { - $this->response = $e->getResponse(); - throw $e; - } - break; - default: - // Any exceptions besides 401 we bubble up to the caller. - throw $e; + // Any exceptions besides 401 get bubbled up. + if ($this->response->getStatusCode() != 401) { + throw $e; + } + + // The session ID or OAuth token used has expired or is invalid: refresh + // token. If refreshToken() throws an exception, or if apiHttpRequest() + // throws anything but a RequestException, let it bubble up. + $this->refreshToken(); + try { + $this->response = new RestResponse($this->apiHttpRequest($path, $params, $method)); + } + catch (RequestException $e) { + $this->response = $e->getResponse(); + throw $e; } } if (empty($this->response) - || !in_array($this->response->getStatusCode(), [200, 201, 204])) { + || ((int)floor($this->response->getStatusCode() / 100)) != 2) { throw new Exception('Unknown error occurred during API call'); } -- GitLab