Skip to content

Commit 8cb1f3d

Browse files
committed
feat: use remaining error types depending on status code
1 parent 9c99161 commit 8cb1f3d

File tree

4 files changed

+66
-19
lines changed

4 files changed

+66
-19
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ clean:
99
## coverage - Runs the test suite and generates a coverage report
1010
coverage:
1111
composer coverage
12-
bin/coverage-check build/logs/clover.xml 82 --only-percentage
12+
bin/coverage-check build/logs/clover.xml 81 --only-percentage
1313

1414
## docs - Generate documentation for the library
1515
docs:

lib/EasyPost/Exception/Api/ApiException.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@
66

77
/**
88
* @package EasyPost
9-
* @property string $message
109
* @property string $code
11-
* @property string $statusCode
12-
* @property string $httpBody
1310
* @property FieldError[] $errors
1411
*/
1512
class ApiException extends EasyPostException
1613
{
17-
private $httpBody;
18-
private $httpStatus;
19-
private $jsonBody;
14+
protected $httpBody;
15+
protected $httpStatus;
16+
protected $jsonBody;
17+
protected $message;
2018
public $code;
21-
public $ecode;
2219
public $errors;
23-
public $message;
2420

2521
/**
2622
* EasyPostException constructor.

lib/EasyPost/Http/Requestor.php

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
use EasyPost\Constant\Constants;
66
use EasyPost\EasyPostClient;
77
use EasyPost\EasypostObject;
8+
use EasyPost\Exception\Api\GatewayTimeoutException;
89
use EasyPost\Exception\Api\HttpException;
910
use EasyPost\Exception\Api\InternalServerException;
1011
use EasyPost\Exception\Api\InvalidRequestException;
1112
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;
1217
use EasyPost\Exception\Api\RedirectException;
18+
use EasyPost\Exception\Api\ServiceUnavailableException;
1319
use EasyPost\Exception\Api\TimeoutException;
20+
use EasyPost\Exception\Api\UnauthorizedException;
21+
use EasyPost\Exception\Api\UnknownApiException;
1422
use GuzzleHttp\Client;
1523

1624
class Requestor
@@ -231,12 +239,59 @@ public static function handleApiError($httpBody, $httpStatus, $response)
231239
$message = $response['error'];
232240
}
233241

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;
240293
}
294+
295+
throw new $errorType($message, $httpStatus, $httpBody);
241296
}
242297
}

phpunit.xml.dist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@
1010
<include>
1111
<directory suffix=".php">./lib/EasyPost/</directory>
1212
</include>
13-
<exclude>
14-
<!-- We exclude the Billing module because it cannot be easily tested. -->
15-
<directory>./lib/EasyPost/Billing.php</directory>
16-
</exclude>
1713
</coverage>
1814
</phpunit>

0 commit comments

Comments
 (0)