From 1ee076aac60482fc9ac3bcd521e6b54ddedf5175 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 16 Aug 2019 16:30:38 +0100 Subject: [PATCH] throw exception on API error --- src/Client.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 27061fc..d04cd3a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -42,9 +42,14 @@ class Client{ curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $this->user.':'.$this->pass); } - $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result = curl_exec($ch); + $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - return json_decode($result); + + if(in_array($status_code,[200,201])){ + return json_decode($result); + }else{ + throw new Exception("API $status_code Code: ".$result); + } } }