-
-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathRemoteConfigApiExceptionConverter.php
More file actions
74 lines (59 loc) · 2.29 KB
/
RemoteConfigApiExceptionConverter.php
File metadata and controls
74 lines (59 loc) · 2.29 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
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Exception;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Kreait\Firebase\Exception\RemoteConfig\ApiConnectionFailed;
use Kreait\Firebase\Exception\RemoteConfig\OperationAborted;
use Kreait\Firebase\Exception\RemoteConfig\PermissionDenied;
use Kreait\Firebase\Exception\RemoteConfig\RemoteConfigError;
use Kreait\Firebase\Exception\RemoteConfig\ValidationFailed;
use Kreait\Firebase\Exception\RemoteConfig\VersionMismatch;
use Kreait\Firebase\Http\ErrorResponseParser;
use Throwable;
use function mb_stripos;
/**
* @internal
*/
class RemoteConfigApiExceptionConverter
{
public function __construct(private readonly ErrorResponseParser $responseParser)
{
}
public function convertException(Throwable $exception): RemoteConfigException
{
if ($exception instanceof RequestException) {
return $this->convertGuzzleRequestException($exception);
}
if ($exception instanceof ConnectException) {
return new ApiConnectionFailed(
message: 'Unable to connect to the API: '.$exception->getMessage(),
previous: $exception
);
}
return new RemoteConfigError(message: $exception->getMessage(), previous: $exception);
}
private function convertGuzzleRequestException(RequestException $e): RemoteConfigException
{
$message = $e->getMessage();
$code = $e->getCode();
$response = $e->getResponse();
if ($response !== null) {
$message = $this->responseParser->getErrorReasonFromResponse($response);
$code = $response->getStatusCode();
}
if (mb_stripos($message, 'permission_denied') !== false) {
return new PermissionDenied($message, $code, $e);
}
if (mb_stripos($message, 'aborted') !== false) {
return new OperationAborted($message, $code, $e);
}
if (mb_stripos($message, 'version_mismatch') !== false) {
return new VersionMismatch($message, $code, $e);
}
if (mb_stripos($message, 'validation_error') !== false) {
return new ValidationFailed($message, $code, $e);
}
return new RemoteConfigError($message, $code, $e);
}
}