From e5c7ff4ac4271a102b003e86a31df7788106fd36 Mon Sep 17 00:00:00 2001 From: Aleksei Faians Date: Thu, 22 Oct 2020 16:30:52 +0300 Subject: [PATCH] Cloudflare returns a better error description when responding with 401 code. Let's use this description when available. --- src/CloudFlare/Api.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/CloudFlare/Api.php b/src/CloudFlare/Api.php index 21f152f..3c6173d 100644 --- a/src/CloudFlare/Api.php +++ b/src/CloudFlare/Api.php @@ -229,11 +229,20 @@ protected function request($path, array $data = [], $method = 'get') $information = curl_getinfo($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $response = json_decode($http_result); + if (in_array($http_code, [401, 403])) { - throw new UnauthorizedException('You do not have permission to perform this request'); + if (!$response) { + throw new UnauthorizedException('You do not have permission to perform this request'); + } else { + $response->success = false; + $error_obj = new \stdClass(); + $error_obj->code = 0; + $error_obj->message = $response->error; + $response->errors[] = $error_obj; + } } - $response = json_decode($http_result); if (!$response) { $response = new \stdClass(); $response->success = false;