Skip to content

Commit 54096cb

Browse files
committed
feat: overhauls exceptions with new classes and inheritence
fix:ecode to code chore: first batch of errors migrated fix: exception inheritence and locations feat: import all exceptions feat: convert remainder of errors fix: tests relating to errors fix: lint feat: use remaining error types depending on status code fix: throws docblock comments fix: last minute things
1 parent 1cbd4f9 commit 54096cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+458
-109
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- `validateWebhook`, `getLowestSmartRate`, and `receiveEvent` are now under `EasyPost\Util\Util` as they do not make any API calls
1616
- Internal only utilities have been moved to `EasyPost\Util\InternalUtil`
1717
- The beta `EndShipper` class has been removed, please use the generally available `EndShipper` class
18+
- The `ecode` property of an `Error` is now just `code`
1819

1920
## v5.8.0 (2022-12-01)
2021

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 78 --only-percentage
1313

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

lib/EasyPost/Constant/Constants.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,20 @@ abstract class Constants
1919
'FedexAccount',
2020
'UpsAccount'
2121
];
22+
23+
// Exception messages (many of these are intended to be used with `sprintf()`)
24+
const ARRAY_REQUIRED_ERROR = 'You must pass an array as the first argument to EasyPost API method calls.';
25+
const COMMUNICATION_ERROR = 'Unexpected error communicating with %s. If this problem persists please let us know at ' . self::SUPPORT_EMAIL . '. %s';
26+
const DECODE_WEBHOOK_ERROR = 'There was a problem decoding the webhook.';
27+
const INVALID_PARAMETER_ERROR_WITH_SUGGESTION = 'Invalid %s value, must be one of: %s';
28+
const INVALID_PAYMENT_METHOD_ERROR = 'The chosen payment method is not valid. Please try again.';
29+
const INVALID_SIGNATURE_ERROR = 'Webhook received does not contain an HMAC signature.';
30+
const INVALID_WEBHOOK_VALIDATION_ERROR = 'Webhook received did not originate from EasyPost or had a webhook secret mismatch.';
31+
const MISSING_PARAMETER_ERROR = '%s is required.';
32+
const NO_BILLING_ERROR = 'Billing has not been setup for this user. Please add a payment method.';
33+
const NO_ID_URL_ERROR = 'Could not determine which URL to request: %s instance has invalid ID: %s';
34+
const NO_RATES_ERROR = 'No rates found.';
35+
const NO_RESPONSE_ERROR = 'Did not receive a response from %s.';
36+
const SEND_STRIPE_DETAILS_ERROR = 'Could not send card details to Stripe, please try again later.';
37+
const UNDEFINED_PROPERTY_ERROR = 'EasyPost Notice: Undefined property of %s instance: %s';
2238
}

lib/EasyPost/EasyPostClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace EasyPost;
44

55
use EasyPost\Constant\Constants;
6-
use EasyPost\Exception\Error;
6+
use EasyPost\Exception\General\MissingParameterException;
77
use EasyPost\Service\AddressService;
88
use EasyPost\Service\BaseService;
99
use EasyPost\Service\BatchService;
@@ -122,7 +122,7 @@ public function __construct($apiKey, $timeout = Constants::TIMEOUT, $apiBase = C
122122
$this->webhook = new WebhookService($this);
123123

124124
if (!$this->apiKey) {
125-
throw new Error('No API key provided. See https://www.easypost.com/docs for details, or contact ' . Constants::SUPPORT_EMAIL . ' for assistance.');
125+
throw new MissingParameterException('No API key provided. See https://www.easypost.com/docs for details, or contact ' . Constants::SUPPORT_EMAIL . ' for assistance.');
126126
}
127127
}
128128

lib/EasyPost/EasyPostObject.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace EasyPost;
44

5+
use EasyPost\Constant\Constants;
56
use EasyPost\Util\InternalUtil;
67
use EasyPost\Util\Util;
78

@@ -111,7 +112,7 @@ public function __get($k)
111112
return $this->_values[$k];
112113
} else {
113114
$class = get_class($this);
114-
error_log("EasyPost Notice: Undefined property of {$class} instance: {$k}");
115+
error_log(sprintf(Constants::UNDEFINED_PROPERTY_ERROR, $class, $k));
115116

116117
return null;
117118
}

lib/EasyPost/Exception/Error.php renamed to lib/EasyPost/Exception/Api/ApiException.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php
22

3-
namespace EasyPost\Exception;
3+
namespace EasyPost\Exception\Api;
4+
5+
use EasyPost\Exception\General\EasyPostException;
46

57
/**
68
* @package EasyPost
79
* @property string $code
8-
* @property string $message
910
* @property FieldError[] $errors
1011
*/
11-
class Error extends \Exception
12+
class ApiException extends EasyPostException
1213
{
13-
private $httpBody;
14-
private $httpStatus;
15-
private $jsonBody;
14+
protected $httpBody;
15+
protected $httpStatus;
16+
protected $jsonBody;
17+
protected $message;
1618
public $code;
17-
public $ecode;
1819
public $errors;
19-
public $message;
2020

2121
/**
22-
* Constructor.
22+
* EasyPostException constructor.
2323
*
2424
* @param string $message
2525
* @param int $httpStatus
@@ -33,16 +33,19 @@ public function __construct($message = null, $httpStatus = null, $httpBody = nul
3333

3434
try {
3535
$this->jsonBody = json_decode($httpBody, true);
36+
37+
// Setup `errors` property
3638
if (isset($this->jsonBody) && !empty($this->jsonBody['error']['errors'])) {
3739
$this->errors = $this->jsonBody['error']['errors'];
3840
} else {
3941
$this->errors = null;
4042
}
4143

44+
// Setup `code` property
4245
if (isset($this->jsonBody) && !empty($this->jsonBody['error']['code'])) {
43-
$this->ecode = $this->jsonBody['error']['code'];
46+
$this->code = $this->jsonBody['error']['code'];
4447
} else {
45-
$this->ecode = null;
48+
$this->code = null;
4649
}
4750
} catch (\Exception $e) {
4851
$this->jsonBody = null;
@@ -76,7 +79,7 @@ public function getHttpBody()
7679
*/
7780
public function prettyPrint()
7881
{
79-
print($this->ecode . ' (' . $this->getHttpStatus() . '): ' .
82+
print($this->code . ' (' . $this->getHttpStatus() . '): ' .
8083
$this->getMessage() . "\n");
8184
if (!empty($this->errors)) {
8285
print("Field errors:\n");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class EncodingException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class ExternalApiException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class ForbiddenException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class GatewayTimeoutException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class HttpException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class InternalServerException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class InvalidRequestException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class JsonException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class MethodNotAllowedException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class NotFoundException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class PaymentException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class RateLimitException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class RedirectException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class ServiceUnavailableException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class TimeoutException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class UnauthorizedException extends ApiException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\Api;
4+
5+
class UnknownApiException extends ApiException
6+
{
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\General;
4+
5+
class EasyPostException extends \Exception
6+
{
7+
/**
8+
* EasyPostException constructor.
9+
*
10+
* @param string $message
11+
*/
12+
public function __construct($message = null)
13+
{
14+
parent::__construct($message);
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\General;
4+
5+
class FilteringException extends EasyPostException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\General;
4+
5+
class InvalidObjectException extends EasyPostException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\General;
4+
5+
class InvalidParameterException extends EasyPostException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\General;
4+
5+
class MissingParameterException extends EasyPostException
6+
{
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace EasyPost\Exception\General;
4+
5+
class SignatureVerificationException extends EasyPostException
6+
{
7+
}

0 commit comments

Comments
 (0)