|
5 | 5 | use EasyPost\Constant\Constants; |
6 | 6 | use EasyPost\EasyPostClient; |
7 | 7 | use EasyPost\EasypostObject; |
| 8 | +use EasyPost\Exception\Api\GatewayTimeoutException; |
8 | 9 | use EasyPost\Exception\Api\HttpException; |
9 | 10 | use EasyPost\Exception\Api\InternalServerException; |
10 | 11 | use EasyPost\Exception\Api\InvalidRequestException; |
11 | 12 | use EasyPost\Exception\Api\JsonException; |
| 13 | +use EasyPost\Exception\Api\MethodNotAllowedException; |
| 14 | +use EasyPost\Exception\Api\NotFoundException; |
| 15 | +use EasyPost\Exception\Api\PaymentException; |
| 16 | +use EasyPost\Exception\Api\RateLimitException; |
12 | 17 | use EasyPost\Exception\Api\RedirectException; |
| 18 | +use EasyPost\Exception\Api\ServiceUnavailableException; |
13 | 19 | use EasyPost\Exception\Api\TimeoutException; |
| 20 | +use EasyPost\Exception\Api\UnauthorizedException; |
| 21 | +use EasyPost\Exception\Api\UnknownApiException; |
14 | 22 | use GuzzleHttp\Client; |
15 | 23 |
|
16 | 24 | class Requestor |
@@ -231,12 +239,59 @@ public static function handleApiError($httpBody, $httpStatus, $response) |
231 | 239 | $message = $response['error']; |
232 | 240 | } |
233 | 241 |
|
234 | | - if ($httpStatus >= 300 || $httpStatus < 400) { |
235 | | - throw new RedirectException($message, $httpStatus, $httpBody); |
236 | | - } elseif ($httpStatus >= 400 || $httpStatus < 500) { |
237 | | - throw new InvalidRequestException($message, $httpStatus, $httpBody); |
238 | | - } elseif ($httpStatus >= 500) { |
239 | | - throw new InternalServerException($message, $httpStatus, $httpBody); |
| 242 | + switch ($httpStatus) { |
| 243 | + case 100: |
| 244 | + $errorType = UnknownApiException::class; |
| 245 | + case 101: |
| 246 | + $errorType = UnknownApiException::class; |
| 247 | + case 102: |
| 248 | + $errorType = UnknownApiException::class; |
| 249 | + case 103: |
| 250 | + $errorType = UnknownApiException::class; |
| 251 | + case 300: |
| 252 | + $errorType = RedirectException::class; |
| 253 | + case 301: |
| 254 | + $errorType = RedirectException::class; |
| 255 | + case 302: |
| 256 | + $errorType = RedirectException::class; |
| 257 | + case 303: |
| 258 | + $errorType = RedirectException::class; |
| 259 | + case 304: |
| 260 | + $errorType = RedirectException::class; |
| 261 | + case 305: |
| 262 | + $errorType = RedirectException::class; |
| 263 | + case 306: |
| 264 | + $errorType = RedirectException::class; |
| 265 | + case 307: |
| 266 | + $errorType = RedirectException::class; |
| 267 | + case 308: |
| 268 | + $errorType = RedirectException::class; |
| 269 | + case 401: |
| 270 | + $errorType = UnauthorizedException::class; |
| 271 | + case 402: |
| 272 | + $errorType = PaymentException::class; |
| 273 | + case 403: |
| 274 | + $errorType = UnauthorizedException::class; |
| 275 | + case 404: |
| 276 | + $errorType = NotFoundException::class; |
| 277 | + case 405: |
| 278 | + $errorType = MethodNotAllowedException::class; |
| 279 | + case 408: |
| 280 | + $errorType = TimeoutException::class; |
| 281 | + case 422: |
| 282 | + $errorType = InvalidRequestException::class; |
| 283 | + case 429: |
| 284 | + $errorType = RateLimitException::class; |
| 285 | + case 500: |
| 286 | + $errorType = InternalServerException::class; |
| 287 | + case 503: |
| 288 | + $errorType = ServiceUnavailableException::class; |
| 289 | + case 504: |
| 290 | + $errorType = GatewayTimeoutException::class; |
| 291 | + default: |
| 292 | + $errorType = UnknownApiException::class; |
240 | 293 | } |
| 294 | + |
| 295 | + throw new $errorType($message, $httpStatus, $httpBody); |
241 | 296 | } |
242 | 297 | } |
0 commit comments