Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #102 from ste93cry/exceptions-refactoring
Browse files Browse the repository at this point in the history
Refactor the exceptions and how they depends on each other
  • Loading branch information
mavimo committed Feb 25, 2019
2 parents 65f2073 + c157282 commit 5996448
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
*
* @author Stefano Arlandini <sarlandini@alice.it>
*/
class CloseIoAuthenticationException extends CloseIoException
class CloseIoAuthenticationException extends CloseIoResponseException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
*
* @author Stefano Arlandini <sarlandini@alice.it>
*/
class CloseIoBadRequestException extends CloseIoException
class CloseIoBadRequestException extends CloseIoResponseException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
*
* @author Stefano Arlandini <sarlandini@alice.it>
*/
class CloseIoResourceNotFoundException extends CloseIoException
class CloseIoResourceNotFoundException extends CloseIoResponseException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,15 @@ public function __construct(CloseIoResponse $response, Throwable $previous = nul
*/
public static function create(CloseIoResponse $response): self
{
$data = $response->getDecodedBody();
$message = $data['error'] ?? self::UNKNOWN_ERROR_MESSAGE;

switch ($response->getHttpStatusCode()) {
case StatusCodeInterface::STATUS_UNAUTHORIZED:
return new static($response, new CloseIoAuthenticationException($message, 0));
return new CloseIoAuthenticationException($response);
case StatusCodeInterface::STATUS_TOO_MANY_REQUESTS:
return new static($response, new CloseIoThrottleException($message, 0));
return new CloseIoThrottleException($response);
case StatusCodeInterface::STATUS_NOT_FOUND:
return new static($response, new CloseIoResourceNotFoundException($message, 0));
return new CloseIoResourceNotFoundException($response);
case StatusCodeInterface::STATUS_BAD_REQUEST:
return new static($response, new CloseIoBadRequestException($message, 0));
return new CloseIoBadRequestException($response);
default:
return new static($response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
*
* @author Stefano Arlandini <sarlandini@alice.it>
*/
class CloseIoThrottleException extends CloseIoException
class CloseIoThrottleException extends CloseIoResponseException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,14 @@ public function constructorDataProvider(): array
/**
* @dataProvider createDataProvider
*/
public function testCreate(int $httpStatusCode, string $responseBody, string $expectedErrorMessage, ?string $expectedPreviousExceptionClass = null): void
public function testCreate(int $httpStatusCode, string $responseBody, string $expectedExceptionClass, string $expectedErrorMessage): void
{
$request = new CloseIoRequest(RequestMethodInterface::METHOD_GET, 'http://www.example.com');
$response = new CloseIoResponse($request, $httpStatusCode, $responseBody);
$exception = CloseIoResponseException::create($response);

$this->assertInstanceOf($expectedExceptionClass, $exception);
$this->assertEquals($expectedErrorMessage, $exception->getMessage());

if (null !== $expectedPreviousExceptionClass) {
$this->assertInstanceOf($expectedPreviousExceptionClass, $exception->getPrevious());
}
}

public function createDataProvider(): array
Expand All @@ -67,30 +64,31 @@ public function createDataProvider(): array
[
StatusCodeInterface::STATUS_UNAUTHORIZED,
'{"error":"foo"}',
'foo',
CloseIoAuthenticationException::class,
'foo',
],
[
StatusCodeInterface::STATUS_TOO_MANY_REQUESTS,
'{"error":"foo"}',
'foo',
CloseIoThrottleException::class,
'foo',
],
[
StatusCodeInterface::STATUS_NOT_FOUND,
'{"error":"foo"}',
'foo',
CloseIoResourceNotFoundException::class,
'foo',
],
[
StatusCodeInterface::STATUS_BAD_REQUEST,
'{"error":"foo"}',
'foo',
CloseIoBadRequestException::class,
'foo',
],
[
StatusCodeInterface::STATUS_SERVICE_UNAVAILABLE,
'{}',
CloseIoResponseException::class,
'Unknown error from Close.io REST API.',
],
];
Expand Down

0 comments on commit 5996448

Please sign in to comment.