Skip to content

Commit

Permalink
Added method to get the response id from the failed request response. (
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed Jan 2, 2018
1 parent e201559 commit 33b8e35
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Http/Handler/ResponseHandlerError.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace bunq\Http\Handler;

use bunq\Exception\ApiException;
use bunq\Exception\BunqException;
use bunq\Exception\ExceptionFactory;
use Gitonomy\Git\Tests\ReferenceTest;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -29,6 +31,11 @@ class ResponseHandlerError extends ResponseHandlerBase
*/
const STATUS_CODE_OK = 200;

/**
* The index of the first item in an array.
*/
const INDEX_FIRST = 0;

/**
* @param ResponseInterface $response
*
Expand All @@ -40,7 +47,8 @@ public function execute(ResponseInterface $response): ResponseInterface
if ($response->getStatusCode() !== self::STATUS_CODE_OK) {
throw ExceptionFactory::createExceptionForResponse(
$this->fetchErrorMessages($response),
$response->getStatusCode()
$response->getStatusCode(),
$this->getResponseId($response)
);
}

Expand Down Expand Up @@ -80,4 +88,25 @@ private function fetchErrorDescriptions(array $errorArray): array

return $errorDescriptions;
}

/**
* @param ResponseInterface $response
*
* @return string
* @throws BunqException
*/
private function getResponseId(ResponseInterface $response): string
{
$header = $response->getHeader(self::FIELD_BUNQ_CLIENT_RESPONSE_ID_UPPER_CASED);

if (empty($header)) {
$header = $response->getHeader(self::FIELD_BUNQ_CLIENT_RESPONSE_ID_UPPER_CASED);
}

if (empty($header)) {
throw new BunqException(self::ERROR_COULD_NOT_DETERMINE_RESPONSE_ID_HEADER);
}

return $header[self::INDEX_FIRST];
}
}

0 comments on commit 33b8e35

Please sign in to comment.