Skip to content

Commit

Permalink
Refactored Exceptions naming
Browse files Browse the repository at this point in the history
  • Loading branch information
thePanz committed Jul 14, 2017
1 parent ae2101d commit 6a0503e
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 91 deletions.
6 changes: 3 additions & 3 deletions src/Api/HttpApi.php
Expand Up @@ -6,8 +6,8 @@

use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Pnz\MattermostClient\Exception\ApiException;
use Pnz\MattermostClient\Exception\Domain as DomainExceptions;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Hydrator\Hydrator;
use Pnz\MattermostClient\Hydrator\ModelHydrator;
use Pnz\MattermostClient\Model\Error;
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function handleResponse(ResponseInterface $response, $class)
*
* @param ResponseInterface $response
*
* @throws GenericApiException
* @throws ApiException
*/
protected function handleErrors(ResponseInterface $response)
{
Expand All @@ -179,7 +179,7 @@ protected function handleErrors(ResponseInterface $response)
case 501:
throw new DomainExceptions\DisabledFeatureException($response, $error);
default:
throw new GenericApiException($response, $error);
throw new ApiException($response, $error);
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/Exception.php

This file was deleted.

35 changes: 28 additions & 7 deletions src/Exception/ApiException.php
Expand Up @@ -7,18 +7,39 @@
use Pnz\MattermostClient\Model\Error;
use Psr\Http\Message\ResponseInterface;

interface ApiException
class ApiException extends \Exception implements DomainException
{
/**
* @return
* @return ResponseInterface
* @var Error
*/
public function getResponse();
protected $error;

/**
* @var ResponseInterface
*/
protected $response;

public function __construct(ResponseInterface $response, Error $error = null)
{
$this->error = $error;
$this->response = $response;

parent::__construct($error ? $error->getMessage() : '', $response->getStatusCode());
}

/**
* Returns the underlying Error, if available.
*
* @return Error|null
*/
public function getError();
public function getError()
{
return $this->error;
}

/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
}
5 changes: 2 additions & 3 deletions src/Exception/Domain/DisabledFeatureException.php
Expand Up @@ -2,9 +2,8 @@

namespace Pnz\MattermostClient\Exception\Domain;

use Pnz\MattermostClient\Exception\DomainException;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Exception\ApiException;

final class DisabledFeatureException extends GenericApiException implements DomainException
final class DisabledFeatureException extends ApiException
{
}
5 changes: 2 additions & 3 deletions src/Exception/Domain/LoginFailedException.php
Expand Up @@ -2,9 +2,8 @@

namespace Pnz\MattermostClient\Exception\Domain;

use Pnz\MattermostClient\Exception\DomainException;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Exception\ApiException;

final class LoginFailedException extends GenericApiException implements DomainException
final class LoginFailedException extends ApiException
{
}
5 changes: 2 additions & 3 deletions src/Exception/Domain/MissingAccessTokenException.php
Expand Up @@ -2,9 +2,8 @@

namespace Pnz\MattermostClient\Exception\Domain;

use Pnz\MattermostClient\Exception\DomainException;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Exception\ApiException;

final class MissingAccessTokenException extends GenericApiException implements DomainException
final class MissingAccessTokenException extends ApiException
{
}
5 changes: 2 additions & 3 deletions src/Exception/Domain/NotFoundException.php
Expand Up @@ -2,9 +2,8 @@

namespace Pnz\MattermostClient\Exception\Domain;

use Pnz\MattermostClient\Exception\DomainException;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Exception\ApiException;

final class NotFoundException extends GenericApiException implements DomainException
final class NotFoundException extends ApiException
{
}
5 changes: 2 additions & 3 deletions src/Exception/Domain/PermissionDeniedException.php
Expand Up @@ -2,9 +2,8 @@

namespace Pnz\MattermostClient\Exception\Domain;

use Pnz\MattermostClient\Exception\DomainException;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Exception\ApiException;

final class PermissionDeniedException extends GenericApiException implements DomainException
final class PermissionDeniedException extends ApiException
{
}
5 changes: 2 additions & 3 deletions src/Exception/Domain/ValidationException.php
Expand Up @@ -2,9 +2,8 @@

namespace Pnz\MattermostClient\Exception\Domain;

use Pnz\MattermostClient\Exception\DomainException;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Exception\ApiException;

final class ValidationException extends GenericApiException implements DomainException
final class ValidationException extends ApiException
{
}
19 changes: 18 additions & 1 deletion src/Exception/DomainException.php
@@ -1,7 +1,24 @@
<?php

declare(strict_types=1);

namespace Pnz\MattermostClient\Exception;

interface DomainException extends ApiException
use Pnz\MattermostClient\Model\Error;
use Psr\Http\Message\ResponseInterface;

interface DomainException extends Exception
{
/**
* @return
* @return ResponseInterface
*/
public function getResponse();

/**
* Returns the underlying Error, if available.
*
* @return Error|null
*/
public function getError();
}
10 changes: 10 additions & 0 deletions src/Exception/Exception.php
@@ -0,0 +1,10 @@
<?php

namespace Pnz\MattermostClient\Exception;

/**
* All our exception implements this interface.
*/
interface Exception
{
}
45 changes: 0 additions & 45 deletions src/Exception/GenericApiException.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/Exception/HydrationException.php
Expand Up @@ -2,8 +2,6 @@

namespace Pnz\MattermostClient\Exception;

use Pnz\MattermostClient\Exception;

class HydrationException extends \RuntimeException implements Exception
{
}
2 changes: 0 additions & 2 deletions src/Exception/InvalidArgumentException.php
Expand Up @@ -2,8 +2,6 @@

namespace Pnz\MattermostClient\Exception;

use Pnz\MattermostClient\Exception;

class InvalidArgumentException extends \InvalidArgumentException implements Exception
{
}
6 changes: 3 additions & 3 deletions src/Plugin/MattermostLoginPlugin.php
Expand Up @@ -7,8 +7,8 @@
use Http\Client\Common\Plugin;
use Http\Message\Authentication\Bearer;
use Http\Message\RequestFactory;
use Pnz\MattermostClient\Exception\ApiException;
use Pnz\MattermostClient\Exception\Domain\LoginFailedException;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Model\Error;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -78,7 +78,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
* @param callable $first
*
* @throws LoginFailedException when loginId/password are not valid
* @throws GenericApiException When an unknown response is returned by the API while logging in
* @throws ApiException When an unknown response is returned by the API while logging in
*/
private function authenticate(callable $first)
{
Expand Down Expand Up @@ -112,7 +112,7 @@ private function authenticate(callable $first)
}
// Otherwise fallback to the default exception
default:
throw new GenericApiException($response);
throw new ApiException($response);
}
}
}
6 changes: 3 additions & 3 deletions tests/Api/BaseHttpApiTest.php
Expand Up @@ -7,12 +7,12 @@
use Http\Message\MessageFactory;
use Http\Message\StreamFactory\GuzzleStreamFactory;
use PHPUnit\Framework\TestCase;
use Pnz\MattermostClient\Exception\ApiException;
use Pnz\MattermostClient\Exception\Domain\DisabledFeatureException;
use Pnz\MattermostClient\Exception\Domain\MissingAccessTokenException;
use Pnz\MattermostClient\Exception\Domain\NotFoundException;
use Pnz\MattermostClient\Exception\Domain\PermissionDeniedException;
use Pnz\MattermostClient\Exception\Domain\ValidationException;
use Pnz\MattermostClient\Exception\GenericApiException;
use Pnz\MattermostClient\Hydrator\Hydrator;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -92,9 +92,9 @@ public function getErrorCodesExceptions()
'403' => [PermissionDeniedException::class, 403],
'404' => [NotFoundException::class, 404],
'501' => [DisabledFeatureException::class, 501],
'500' => [GenericApiException::class, 500],
'500' => [ApiException::class, 500],
// Weird response
'000' => [GenericApiException::class, 000],
'000' => [ApiException::class, 000],
];
}
}

0 comments on commit 6a0503e

Please sign in to comment.