-
-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathMessagingApiExceptionConverter.php
More file actions
173 lines (131 loc) · 5.87 KB
/
Copy pathMessagingApiExceptionConverter.php
File metadata and controls
173 lines (131 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Exception;
use Beste\Clock\SystemClock;
use DateInterval;
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;
use Kreait\Firebase\Exception\Messaging\InvalidMessage;
use Kreait\Firebase\Exception\Messaging\MessagingError;
use Kreait\Firebase\Exception\Messaging\NotFound;
use Kreait\Firebase\Exception\Messaging\QuotaExceeded;
use Kreait\Firebase\Exception\Messaging\ServerError;
use Kreait\Firebase\Exception\Messaging\ServerUnavailable;
use Kreait\Firebase\Http\ErrorResponseParser;
use Psr\Clock\ClockInterface;
use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use function is_numeric;
/**
* @internal
*/
class MessagingApiExceptionConverter
{
private readonly ErrorResponseParser $responseParser;
private readonly ClockInterface $clock;
public function __construct(?ClockInterface $clock = null)
{
$this->responseParser = new ErrorResponseParser();
$this->clock = $clock ?? SystemClock::create();
}
public function convertException(Throwable $exception): MessagingException
{
if ($exception instanceof RequestException) {
return $this->convertGuzzleRequestException($exception);
}
if ($exception instanceof NetworkExceptionInterface) {
return new ApiConnectionFailed(
message: 'Unable to connect to the API: '.$exception->getMessage(),
previous: $exception
);
}
return new MessagingError(message: $exception->getMessage(), previous: $exception);
}
public function convertResponse(ResponseInterface $response, ?Throwable $previous = null): MessagingException
{
$code = $response->getStatusCode();
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 StatusCode::STATUS_BAD_REQUEST:
$convertedError = new InvalidMessage($message, previous: $previous);
break;
case StatusCode::STATUS_UNAUTHORIZED:
case StatusCode::STATUS_FORBIDDEN:
$convertedError = new AuthenticationError($message, previous: $previous);
break;
case StatusCode::STATUS_NOT_FOUND:
$convertedError = new NotFound($message, previous: $previous);
break;
case StatusCode::STATUS_TOO_MANY_REQUESTS:
$convertedError = new QuotaExceeded($message, previous: $previous);
$retryAfter = $this->getRetryAfter($response);
if ($retryAfter !== null) {
$convertedError = $convertedError->withRetryAfter($retryAfter);
}
break;
case StatusCode::STATUS_INTERNAL_SERVER_ERROR:
$convertedError = new ServerError($message, previous: $previous);
break;
case StatusCode::STATUS_BAD_GATEWAY:
$contentType = mb_strtolower($response->getHeaderLine('Content-Type'));
$retryAfter = $this->getRetryAfter($response);
if (!str_contains($contentType, 'json')) {
// Adding 30 seconds as a fallback retry after because the HTML Response suggests it
// See https://github.com/kreait/firebase-php/issues/988
$retryAfter ??= ($this->clock->now()->add(new DateInterval('PT30S')));
$message = 'The server encountered a temporary error and could not complete your request.';
}
$convertedError = new ServerUnavailable($message, previous: $previous);
if ($retryAfter !== null) {
$convertedError = $convertedError->withRetryAfter($retryAfter);
}
break;
case StatusCode::STATUS_SERVICE_UNAVAILABLE:
$convertedError = new ServerUnavailable($message, previous: $previous);
$retryAfter = $this->getRetryAfter($response);
if ($retryAfter !== null) {
$convertedError = $convertedError->withRetryAfter($retryAfter);
}
break;
default:
$convertedError = new MessagingError($message, $code, $previous);
break;
}
return $convertedError->withErrors($errors);
}
private function convertGuzzleRequestException(RequestException $e): MessagingException
{
$response = $e->getResponse();
if ($response !== null) {
return $this->convertResponse($response, $e);
}
return new MessagingError(message: $e->getMessage(), previous: $e);
}
private function getRetryAfter(ResponseInterface $response): ?DateTimeImmutable
{
$retryAfter = $response->getHeaderLine('Retry-After');
if ($retryAfter === '') {
return null;
}
if (is_numeric($retryAfter)) {
return $this->clock->now()->modify("+{$retryAfter} seconds");
}
try {
return new DateTimeImmutable($retryAfter);
} catch (Throwable) {
// We can't afford to throw exceptions in an exception handler :)
// Here, if the Retry-After header doesn't have a numeric value
// or a date that can be handled by DateTimeImmutable, we just
// throw it away, sorry not sorry ¯\_(ツ)_/¯
return null;
}
}
}