Skip to content

Commit

Permalink
Handle 409 Conflict headers (#3)
Browse files Browse the repository at this point in the history
* Handle conflict headers

* Fix headers

* Handle no headers

* Fix

* Fix

* Dont wrap exceptions

* Remove space

* Use constants
  • Loading branch information
Jleagle committed Aug 25, 2020
1 parent 222db27 commit bbad713
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
27 changes: 18 additions & 9 deletions src/ApiRequest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php
namespace Fortifi\Api\Core;

use Exception;
use Fortifi\Api\Core\Exceptions\ApiException;
use Fortifi\Api\Core\Exceptions\Client\ClientApiException;
use Fortifi\Api\Core\Exceptions\Client\ConflictException;
use Fortifi\Api\Core\Exceptions\Client\ForbiddenException;
use Packaged\Helpers\RetryHelper;
use Packaged\Helpers\ValueAs;
use stdClass;

class ApiRequest implements IApiRequest
{
Expand All @@ -20,7 +23,7 @@ class ApiRequest implements IApiRequest
private $_connection;

/**
* @var \stdClass
* @var stdClass
*/
private $_decoded;

Expand Down Expand Up @@ -98,7 +101,7 @@ public function getRawResult()
function () {
return $this->_getRawResult();
},
function (\Exception $e) {
function (Exception $e) {
if($e->getCode() == 403 && stristr($e->getMessage(), 'token'))
{
$this->_connection->clearToken();
Expand All @@ -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;
}
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -164,7 +173,7 @@ public function wasSuccessful()
{
return $this->getRawResult()->getStatusCode() == 200;
}
catch(\Exception $e)
catch(Exception $e)
{
return false;
}
Expand Down
13 changes: 4 additions & 9 deletions src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Fortifi\Api\Core\Exceptions;

use Exception;
use Fortifi\Api\Core\Exceptions\Client\BadApiRequestException;
use Fortifi\Api\Core\Exceptions\Client\ClientApiException;
use Fortifi\Api\Core\Exceptions\Client\ConflictException;
Expand All @@ -21,11 +22,9 @@
use Fortifi\Api\Core\Exceptions\Server\ServerApiException;
use Fortifi\Api\Core\Exceptions\Server\ServiceUnavailableException;

class ApiException extends \Exception
class ApiException extends Exception
{
public static function build(
$code, $message = null, \Exception $previous = null
)
public static function build($code, $message = null, Exception $previous = null)
{
switch((int)$code)
{
Expand All @@ -44,11 +43,7 @@ public static function build(
case 406:
return new NotAcceptableException($message, $code, $previous);
case 407:
return new ProxyAuthenticationRequiredException(
$message,
$code,
$previous
);
return new ProxyAuthenticationRequiredException($message, $code, $previous);
case 408:
return new RequestTimeoutException($message, $code, $previous);
case 409:
Expand Down
27 changes: 19 additions & 8 deletions src/Exceptions/Client/ConflictException.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
<?php
namespace Fortifi\Api\Core\Exceptions\Client;

use Exception;

class ConflictException extends ClientApiException
{
const FIELD = 'x-fortifi-conflict-field';
const VALUE = 'x-fortifi-conflict-value';
const OWNER = 'x-fortifi-conflict-owner';

public $field;
public $value;
public $owner;

/**
* @inheritDoc
*/
public function __construct(
$message = '', $code = 409, \Exception $previous = null
)
public function __construct($message = '', $code = 409, Exception $previous = null)
{
parent::__construct($message ?: 'Conflict', $code, $previous);
}

public function handleHeaders(array $headers)
{
if(empty($message))
{
$message = 'Conflict';
}
parent::__construct($message, $code, $previous);
$this->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;
}
}

0 comments on commit bbad713

Please sign in to comment.