-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMindeeHttpException.php
More file actions
161 lines (151 loc) · 5.53 KB
/
Copy pathMindeeHttpException.php
File metadata and controls
161 lines (151 loc) · 5.53 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
<?php
declare(strict_types=1);
/**
* @file
* Mindee HTTP Exceptions.
*/
namespace Mindee\Error;
use function array_key_exists;
use function is_array;
use function is_string;
/**
* Exceptions relating to HTTP calls.
*
* Handles uncaught error codes.
*/
class MindeeHttpException extends MindeeException
{
/**
* @var string|null API code as sent by the server.
*/
public ?string $apiCode;
/**
* @var string|array<string, int|float|string|bool|null|array<array-key, mixed>>|null API details field as sent by the server.
*/
public string|array|null $apiDetails;
/**
* @var string|array<string, int|float|string|bool|null|array<array-key, mixed>>|null API message field as sent by the server.
*/
public string|array|null $apiMessage;
/**
* @param array<string, int|float|string|bool|null|array<array-key, mixed>> $httpError Array containing the error data.
* @param string $url Remote URL the error was found on.
* @param integer $statusCode Error code.
*/
public function __construct(array $httpError, string $url, public int $statusCode)
{
if (array_key_exists('code', $httpError)) {
$this->apiCode = $httpError['code'];
} else {
$this->apiCode = null;
}
if (array_key_exists('details', $httpError)) {
$this->apiDetails = $httpError['details'];
} else {
$this->apiDetails = null;
}
if (array_key_exists('message', $httpError)) {
$this->apiMessage = $httpError['message'];
} else {
$this->apiMessage = null;
}
if (is_array($this->apiDetails)) {
$details = "\n" . json_encode($this->apiDetails, JSON_PRETTY_PRINT) . "\n";
} else {
$details = (string) ($this->apiDetails);
}
parent::__construct("$url $this->statusCode HTTP error: $details - $this->apiMessage");
}
/**
* Builds an appropriate error object from the server reply.
*
* @param array<string, int|float|string|bool|null|array<array-key, mixed>>|string|null $response Parsed server response.
* @return string[]
* @throws MindeeException Throws if the error itself can't be built.
*/
public static function createErrorObj(array|string|null $response): array
{
if (is_string($response)) {
if (str_contains($response, 'Maximum pdf pages')) {
$errorArray = [
'code' => 'TooManyPages',
'message' => 'Maximum amount of pdf pages reached.',
'details' => $response,
];
} elseif (str_contains($response, 'Max file size is')) {
$errorArray = [
'code' => 'FileTooLarge',
'message' => 'Maximum file size reached.',
'details' => $response,
];
} elseif (str_contains($response, 'Invalid file type')) {
$errorArray = [
'code' => 'InvalidFiletype',
'message' => 'Invalid file type.',
'details' => $response,
];
} elseif (str_contains($response, 'Gateway timeout')) {
$errorArray = [
'code' => 'RequestTimeout',
'message' => 'Request timed out.',
'details' => $response,
];
} elseif (str_contains($response, 'Too Many Requests')) {
$errorArray = [
'code' => 'TooManyRequests',
'message' => 'Too Many Requests.',
'details' => $response,
];
} else {
$errorArray = [
'code' => 'UnknownError',
'message' => 'Server sent back an unexpected reply.',
'details' => $response,
];
}
return $errorArray;
}
if (
is_array($response)
&& array_key_exists('api_request', $response)
&& array_key_exists('error', $response['api_request'])
) {
return $response['api_request']['error'];
}
if (!isset($response)) {
throw new MindeeException(
"Request to the API failed.",
ErrorCode::API_REQUEST_FAILED
);
}
throw new MindeeException(
'Could not build a specific HTTP exception from: ' . json_encode($response, JSON_PRETTY_PRINT),
ErrorCode::EXCEPTION_BUILD_FAILED
);
}
/**
* @param string $url Remote URL the error was found on.
* @param array<string, mixed>|string|null $response Raw server response.
*/
public static function handleError(string $url, array|string|null $response): self
{
if (is_array($response)) {
$dataResponse = $response['data'] ?? ["data" => null];
} else {
$dataResponse = ["data" => null];
}
$errorObj = self::createErrorObj($dataResponse);
if (array_key_exists("code", $response) && is_numeric($response['code'])) {
$code = (int) ($response['code']);
} else {
$code = 500;
}
if ($code >= 400 && $code <= 499) {
return new MindeeHttpClientException($errorObj, $url, $code);
}
if ($code >= 500 && $code <= 599) {
return new MindeeHttpClientException($errorObj, $url, $code);
}
return new self($errorObj, $url, $code);
}
}