Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- `validateWebhook`, `getLowestSmartRate`, and `receiveEvent` are now under `EasyPost\Util\Util` as they do not make any API calls
- Internal only utilities have been moved to `EasyPost\Util\InternalUtil`
- The beta `EndShipper` class has been removed, please use the generally available `EndShipper` class
- The `ecode` property of an `Error` is now just `code`

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

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ clean:
## coverage - Runs the test suite and generates a coverage report
coverage:
composer coverage
bin/coverage-check build/logs/clover.xml 82 --only-percentage
bin/coverage-check build/logs/clover.xml 80 --only-percentage

## docs - Generate documentation for the library
docs:
Expand Down
16 changes: 16 additions & 0 deletions lib/EasyPost/Constant/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ abstract class Constants
'FedexAccount',
'UpsAccount'
];

// Exception messages (many of these are intended to be used with `sprintf()`)
const ARRAY_REQUIRED_ERROR = 'You must pass an array as the first argument to EasyPost API method calls.';
const COMMUNICATION_ERROR = 'Unexpected error communicating with %s. If this problem persists please let us know at ' . self::SUPPORT_EMAIL . '. %s';
const DECODE_WEBHOOK_ERROR = 'There was a problem decoding the webhook.';
const INVALID_PARAMETER_ERROR_WITH_SUGGESTION = 'Invalid %s value, must be one of: %s';
const INVALID_PAYMENT_METHOD_ERROR = 'The chosen payment method is not valid. Please try again.';
const INVALID_SIGNATURE_ERROR = 'Webhook received does not contain an HMAC signature.';
const INVALID_WEBHOOK_VALIDATION_ERROR = 'Webhook received did not originate from EasyPost or had a webhook secret mismatch.';
const MISSING_PARAMETER_ERROR = '%s is required.';
const NO_BILLING_ERROR = 'Billing has not been setup for this user. Please add a payment method.';
const NO_ID_URL_ERROR = 'Could not determine which URL to request: %s instance has invalid ID: %s';
const NO_RATES_ERROR = 'No rates found.';
const NO_RESPONSE_ERROR = 'Did not receive a response from %s.';
const SEND_STRIPE_DETAILS_ERROR = 'Could not send card details to Stripe, please try again later.';
const UNDEFINED_PROPERTY_ERROR = 'EasyPost Notice: Undefined property of %s instance: %s';
}
4 changes: 2 additions & 2 deletions lib/EasyPost/EasyPostClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace EasyPost;

use EasyPost\Constant\Constants;
use EasyPost\Exception\Error;
use EasyPost\Exception\General\MissingParameterException;
use EasyPost\Service\AddressService;
use EasyPost\Service\BaseService;
use EasyPost\Service\BatchService;
Expand Down Expand Up @@ -122,7 +122,7 @@ public function __construct($apiKey, $timeout = Constants::TIMEOUT, $apiBase = C
$this->webhook = new WebhookService($this);

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

Expand Down
3 changes: 2 additions & 1 deletion lib/EasyPost/EasyPostObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EasyPost;

use EasyPost\Constant\Constants;
use EasyPost\Util\InternalUtil;
use EasyPost\Util\Util;

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

return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php

namespace EasyPost\Exception;
namespace EasyPost\Exception\Api;

use EasyPost\Exception\General\EasyPostException;

/**
* @package EasyPost
* @property string $code
* @property string $message
* @property FieldError[] $errors
*/
class Error extends \Exception
class ApiException extends EasyPostException
{
public $code;
public $errors;
protected $message;
private $httpBody;
private $httpStatus;
private $jsonBody;
public $code;
public $ecode;
public $errors;
public $message;

/**
* Constructor.
* ApiException constructor.
*
* @param string $message
* @param int $httpStatus
Expand All @@ -33,16 +33,19 @@ public function __construct($message = null, $httpStatus = null, $httpBody = nul

try {
$this->jsonBody = json_decode($httpBody, true);

// Setup `errors` property
if (isset($this->jsonBody) && !empty($this->jsonBody['error']['errors'])) {
$this->errors = $this->jsonBody['error']['errors'];
} else {
$this->errors = null;
}

// Setup `code` property
if (isset($this->jsonBody) && !empty($this->jsonBody['error']['code'])) {
$this->ecode = $this->jsonBody['error']['code'];
$this->code = $this->jsonBody['error']['code'];
} else {
$this->ecode = null;
$this->code = null;
}
} catch (\Exception $e) {
$this->jsonBody = null;
Expand Down Expand Up @@ -76,7 +79,7 @@ public function getHttpBody()
*/
public function prettyPrint()
{
print($this->ecode . ' (' . $this->getHttpStatus() . '): ' .
print($this->code . ' (' . $this->getHttpStatus() . '): ' .
$this->getMessage() . "\n");
if (!empty($this->errors)) {
print("Field errors:\n");
Expand Down
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/EncodingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class EncodingException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/ExternalApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class ExternalApiException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/ForbiddenException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class ForbiddenException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/GatewayTimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class GatewayTimeoutException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/HttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class HttpException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/InternalServerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class InternalServerException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/InvalidRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class InvalidRequestException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/JsonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class JsonException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/MethodNotAllowedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class MethodNotAllowedException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class NotFoundException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/PaymentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class PaymentException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/RateLimitException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class RateLimitException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/RedirectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class RedirectException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/ServiceUnavailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class ServiceUnavailableException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/TimeoutException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class TimeoutException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class UnauthorizedException extends ApiException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/Api/UnknownApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\Api;

class UnknownApiException extends ApiException
{
}
16 changes: 16 additions & 0 deletions lib/EasyPost/Exception/General/EasyPostException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace EasyPost\Exception\General;

class EasyPostException extends \Exception
{
/**
* EasyPostException constructor.
*
* @param string $message
*/
public function __construct($message = null)
{
parent::__construct($message);
}
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/General/FilteringException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\General;

class FilteringException extends EasyPostException
{
}
7 changes: 7 additions & 0 deletions lib/EasyPost/Exception/General/InvalidObjectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\General;

class InvalidObjectException extends EasyPostException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\General;

class InvalidParameterException extends EasyPostException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\General;

class MissingParameterException extends EasyPostException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace EasyPost\Exception\General;

class SignatureVerificationException extends EasyPostException
{
}
Loading