Skip to content

Commit

Permalink
Use StatusCodeInterface consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Oct 11, 2023
1 parent 86ece87 commit 98b9f22
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Firebase/Exception/AppCheckApiExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Kreait\Firebase\Exception;

use Fig\Http\Message\StatusCodeInterface as StatusCode;
use GuzzleHttp\Exception\RequestException;
use Kreait\Firebase\Exception\AppCheck\ApiConnectionFailed;
use Kreait\Firebase\Exception\AppCheck\AppCheckError;
Expand Down Expand Up @@ -49,7 +50,7 @@ private function convertGuzzleRequestException(RequestException $e): AppCheckExc
}

return match ($code) {
401, 403 => new PermissionDenied($message, $code, $e),
StatusCode::STATUS_UNAUTHORIZED, StatusCode::STATUS_FORBIDDEN => new PermissionDenied($message, $code, $e),
default => new AppCheckError($message, $code, $e),
};
}
Expand Down
7 changes: 4 additions & 3 deletions src/Firebase/Exception/DatabaseApiExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Kreait\Firebase\Exception;

use Fig\Http\Message\StatusCodeInterface as StatusCode;
use GuzzleHttp\Exception\RequestException;
use Kreait\Firebase\Exception\Database\ApiConnectionFailed;
use Kreait\Firebase\Exception\Database\DatabaseError;
Expand Down Expand Up @@ -51,9 +52,9 @@ private function convertGuzzleRequestException(RequestException $e): DatabaseExc
}

return match ($code) {
401, 403 => new PermissionDenied($message, $code, $e),
412 => new PreconditionFailed($message, $code, $e),
404 => DatabaseNotFound::fromUri($e->getRequest()->getUri()),
StatusCode::STATUS_UNAUTHORIZED, StatusCode::STATUS_FORBIDDEN => new PermissionDenied($message, $code, $e),
StatusCode::STATUS_PRECONDITION_FAILED => new PreconditionFailed($message, $code, $e),
StatusCode::STATUS_NOT_FOUND => DatabaseNotFound::fromUri($e->getRequest()->getUri()),
default => new DatabaseError($message, $code, $e),
};
}
Expand Down
17 changes: 9 additions & 8 deletions src/Firebase/Exception/MessagingApiExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Beste\Clock\SystemClock;
use DateTimeImmutable;
use Fig\Http\Message\StatusCodeInterface as StatusCode;
use GuzzleHttp\Exception\RequestException;
use Kreait\Firebase\Exception\Messaging\ApiConnectionFailed;
use Kreait\Firebase\Exception\Messaging\AuthenticationError;
Expand Down Expand Up @@ -54,31 +55,31 @@ public function convertResponse(ResponseInterface $response, ?Throwable $previou
{
$code = $response->getStatusCode();

if ($code < 400) {
if ($code < StatusCode::STATUS_BAD_REQUEST) {
throw new InvalidArgumentException('Cannot convert a non-failed response to an exception');
}

$errors = $this->responseParser->getErrorsFromResponse($response);
$message = $this->responseParser->getErrorReasonFromResponse($response);

switch ($code) {
case 400:
case StatusCode::STATUS_BAD_REQUEST:
$convertedError = new InvalidMessage($message);

break;

case 401:
case 403:
case StatusCode::STATUS_UNAUTHORIZED:
case StatusCode::STATUS_FORBIDDEN:
$convertedError = new AuthenticationError($message);

break;

case 404:
case StatusCode::STATUS_NOT_FOUND:
$convertedError = new NotFound($message);

break;

case 429:
case StatusCode::STATUS_TOO_MANY_REQUESTS:
$convertedError = new QuotaExceeded($message);
$retryAfter = $this->getRetryAfter($response);

Expand All @@ -88,12 +89,12 @@ public function convertResponse(ResponseInterface $response, ?Throwable $previou

break;

case 500:
case StatusCode::STATUS_INTERNAL_SERVER_ERROR:
$convertedError = new ServerError($message);

break;

case 503:
case StatusCode::STATUS_SERVICE_UNAVAILABLE:
$convertedError = new ServerUnavailable($message);
$retryAfter = $this->getRetryAfter($response);

Expand Down
3 changes: 2 additions & 1 deletion src/Firebase/Http/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Beste\Json;
use Exception;
use Fig\Http\Message\StatusCodeInterface as StatusCode;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Promise\Create;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static function log(LoggerInterface $logger, MessageFormatter $formatter,
return static fn (callable $handler) => static fn ($request, array $options) => $handler($request, $options)->then(
static function (ResponseInterface $response) use ($logger, $request, $formatter, $logLevel, $errorLogLevel) {
$message = $formatter->format($request, $response);
$messageLogLevel = $response->getStatusCode() >= 400 ? $errorLogLevel : $logLevel;
$messageLogLevel = $response->getStatusCode() >= StatusCode::STATUS_BAD_REQUEST ? $errorLogLevel : $logLevel;

$logger->log($messageLogLevel, $message);

Expand Down

0 comments on commit 98b9f22

Please sign in to comment.