Skip to content

Commit

Permalink
Modernize exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Aug 23, 2019
1 parent 2fb84f0 commit b6b25a9
Show file tree
Hide file tree
Showing 82 changed files with 863 additions and 614 deletions.
4 changes: 2 additions & 2 deletions examples/oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'grant_type' => 'authorization_code',
'code' => $code,
]);
} catch (\Stripe\Error\OAuth\OAuthBase $e) {
} catch (\Stripe\Exception\OAuth\ExceptionInterface $e) {
exit("Error: " . $e->getMessage());
}

Expand All @@ -38,7 +38,7 @@
\Stripe\OAuth::deauthorize([
'stripe_user_id' => $accountId,
]);
} catch (\Stripe\Error\OAuth\OAuthBase $e) {
} catch (\Stripe\Exception\OAuth\ExceptionInterface $e) {
exit("Error: " . $e->getMessage());
}

Expand Down
43 changes: 24 additions & 19 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@
require(dirname(__FILE__) . '/lib/HttpClient/ClientInterface.php');
require(dirname(__FILE__) . '/lib/HttpClient/CurlClient.php');

// Errors
require(dirname(__FILE__) . '/lib/Error/Base.php');
require(dirname(__FILE__) . '/lib/Error/Api.php');
require(dirname(__FILE__) . '/lib/Error/ApiConnection.php');
require(dirname(__FILE__) . '/lib/Error/Authentication.php');
require(dirname(__FILE__) . '/lib/Error/Card.php');
require(dirname(__FILE__) . '/lib/Error/Idempotency.php');
require(dirname(__FILE__) . '/lib/Error/InvalidRequest.php');
require(dirname(__FILE__) . '/lib/Error/Permission.php');
require(dirname(__FILE__) . '/lib/Error/RateLimit.php');
require(dirname(__FILE__) . '/lib/Error/SignatureVerification.php');
// Exceptions
require(dirname(__FILE__) . '/lib/Exception/ExceptionInterface.php');
require(dirname(__FILE__) . '/lib/Exception/ExceptionTrait.php');
require(dirname(__FILE__) . '/lib/Exception/ApiConnectionException.php');
require(dirname(__FILE__) . '/lib/Exception/ApiException.php');
require(dirname(__FILE__) . '/lib/Exception/AuthenticationException.php');
require(dirname(__FILE__) . '/lib/Exception/BadMethodCallException.php');
require(dirname(__FILE__) . '/lib/Exception/CardException.php');
require(dirname(__FILE__) . '/lib/Exception/IdempotencyException.php');
require(dirname(__FILE__) . '/lib/Exception/InvalidArgumentException.php');
require(dirname(__FILE__) . '/lib/Exception/InvalidRequestException.php');
require(dirname(__FILE__) . '/lib/Exception/PermissionException.php');
require(dirname(__FILE__) . '/lib/Exception/RateLimitException.php');
require(dirname(__FILE__) . '/lib/Exception/SignatureVerificationException.php');
require(dirname(__FILE__) . '/lib/Exception/UnexpectedValueException.php');

// OAuth errors
require(dirname(__FILE__) . '/lib/Error/OAuth/OAuthBase.php');
require(dirname(__FILE__) . '/lib/Error/OAuth/InvalidClient.php');
require(dirname(__FILE__) . '/lib/Error/OAuth/InvalidGrant.php');
require(dirname(__FILE__) . '/lib/Error/OAuth/InvalidRequest.php');
require(dirname(__FILE__) . '/lib/Error/OAuth/InvalidScope.php');
require(dirname(__FILE__) . '/lib/Error/OAuth/UnsupportedGrantType.php');
require(dirname(__FILE__) . '/lib/Error/OAuth/UnsupportedResponseType.php');
// OAuth exceptions
require(dirname(__FILE__) . '/lib/Exception/OAuth/ExceptionInterface.php');
require(dirname(__FILE__) . '/lib/Exception/OAuth/ExceptionTrait.php');
require(dirname(__FILE__) . '/lib/Exception/OAuth/InvalidClientException.php');
require(dirname(__FILE__) . '/lib/Exception/OAuth/InvalidGrantException.php');
require(dirname(__FILE__) . '/lib/Exception/OAuth/InvalidRequestException.php');
require(dirname(__FILE__) . '/lib/Exception/OAuth/InvalidScopeException.php');
require(dirname(__FILE__) . '/lib/Exception/OAuth/UnsupportedGrantTypeException.php');
require(dirname(__FILE__) . '/lib/Exception/OAuth/UnsupportedResponseTypeException.php');

// API operations
require(dirname(__FILE__) . '/lib/ApiOperations/All.php');
Expand Down
2 changes: 1 addition & 1 deletion lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private function serializeAdditionalOwners($legalEntity, $additionalOwners)
$originalValue = [];
}
if (($originalValue) && (count($originalValue) > count($additionalOwners))) {
throw new \InvalidArgumentException(
throw new Exception\InvalidArgumentException(
"You cannot delete an item from an array, you must instead set a new array"
);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/AlipayAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function instanceUrl()
$path = 'sources';
} else {
$msg = "Alipay accounts cannot be accessed without a customer ID.";
throw new Error\InvalidRequest($msg, null);
throw new Exception\UnexpectedValueException($msg);
}
$parentExtn = urlencode(Util\Util::utf8($parent));
$extn = urlencode(Util\Util::utf8($this['id']));
Expand All @@ -40,7 +40,7 @@ public function instanceUrl()
* @param array|string $_id
* @param array|string|null $_opts
*
* @throws \Stripe\Error\InvalidRequest
* @throws \Stripe\Exception\BadMethodCallException
*
* @deprecated Alipay accounts are deprecated. Please use the sources API instead.
* @link https://stripe.com/docs/sources/alipay
Expand All @@ -50,15 +50,15 @@ public static function retrieve($_id, $_opts = null)
$msg = "Alipay accounts cannot be retrieved without a customer ID. " .
"Retrieve an Alipay account using `Customer::retrieveSource(" .
"'customer_id', 'alipay_account_id')`.";
throw new Error\InvalidRequest($msg, null);
throw new Exception\BadMethodCallException($msg);
}

/**
* @param string $_id
* @param array|null $_params
* @param array|string|null $_options
*
* @throws \Stripe\Error\InvalidRequest
* @throws \Stripe\Exception\BadMethodCallException
*
* @deprecated Alipay accounts are deprecated. Please use the sources API instead.
* @link https://stripe.com/docs/sources/alipay
Expand All @@ -68,6 +68,6 @@ public static function update($_id, $_params = null, $_options = null)
$msg = "Alipay accounts cannot be updated without a customer ID. " .
"Update an Alipay account using `Customer::updateSource(" .
"'customer_id', 'alipay_account_id', \$updateParams)`.";
throw new Error\InvalidRequest($msg, null);
throw new Exception\BadMethodCallException($msg);
}
}
2 changes: 1 addition & 1 deletion lib/ApiOperations/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function all($params = null, $opts = null)
list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
if (!($obj instanceof \Stripe\Collection)) {
throw new \Stripe\Error\Api(
throw new \Stripe\Exception\UnexpectedValueException(
'Expected type ' . \Stripe\Collection::class . ', got "' . get_class($obj) . '" instead.'
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ApiOperations/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait Request
/**
* @param array|null|mixed $params The list of parameters to validate
*
* @throws \Stripe\Error\Api if $params exists and is not an array
* @throws \Stripe\Exception\InvalidArgumentException if $params exists and is not an array
*/
protected static function _validateParams($params = null)
{
Expand All @@ -21,7 +21,7 @@ protected static function _validateParams($params = null)
. "method calls. (HINT: an example call to create a charge "
. "would be: \"Stripe\\Charge::create(['amount' => 100, "
. "'currency' => 'usd', 'source' => 'tok_1234'])\")";
throw new \Stripe\Error\Api($message);
throw new \Stripe\Exception\InvalidArgumentException($message);
}
}

Expand Down
Loading

0 comments on commit b6b25a9

Please sign in to comment.