From bbad71398bff314b2a0a68f1e151bde2b1a980d1 Mon Sep 17 00:00:00 2001 From: Jim Eagle Date: Tue, 25 Aug 2020 10:13:19 +0100 Subject: [PATCH] Handle 409 Conflict headers (#3) * Handle conflict headers * Fix headers * Handle no headers * Fix * Fix * Dont wrap exceptions * Remove space * Use constants --- src/ApiRequest.php | 27 ++++++++++++++------- src/Exceptions/ApiException.php | 13 +++------- src/Exceptions/Client/ConflictException.php | 27 +++++++++++++++------ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/ApiRequest.php b/src/ApiRequest.php index 4438e3f..45eb8c6 100644 --- a/src/ApiRequest.php +++ b/src/ApiRequest.php @@ -1,11 +1,14 @@ _getRawResult(); }, - function (\Exception $e) { + function (Exception $e) { if($e->getCode() == 403 && stristr($e->getMessage(), 'token')) { $this->_connection->clearToken(); @@ -108,13 +111,17 @@ function (\Exception $e) { } ); } - catch(\Exception $e) + catch(ClientApiException $e) + { + $this->_result = $e; + } + catch(Exception $e) { $this->_result = ApiException::build($e->getCode(), $e->getMessage(), $e); } } - if($this->_result instanceof \Exception) + if($this->_result instanceof Exception) { throw $this->_result; } @@ -124,10 +131,12 @@ function (\Exception $e) { { if($this->_result->getStatusCode() !== 200) { - throw ApiException::build( - $this->_result->getStatusCode(), - $this->_result->getStatusMessage() - ); + $exception = ApiException::build($this->_result->getStatusCode(), $this->_result->getStatusMessage()); + if($exception instanceof ConflictException) + { + $exception->handleHeaders($this->_result->getHeaders()); + } + throw $exception; } } @@ -164,7 +173,7 @@ public function wasSuccessful() { return $this->getRawResult()->getStatusCode() == 200; } - catch(\Exception $e) + catch(Exception $e) { return false; } diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/ApiException.php index de4ddd3..66d2d5e 100644 --- a/src/Exceptions/ApiException.php +++ b/src/Exceptions/ApiException.php @@ -1,6 +1,7 @@ field = !empty($headers[static::FIELD]) ? $headers[static::FIELD][0] : null; + $this->value = !empty($headers[static::VALUE]) ? $headers[static::VALUE][0] : null; + $this->owner = !empty($headers[static::OWNER]) ? $headers[static::OWNER][0] : null; } }