Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update constructors of Stripe exception classes #559

Merged
merged 1 commit into from
Aug 22, 2019
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
17 changes: 9 additions & 8 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,31 +193,32 @@ private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $err
$param = isset($errorData['param']) ? $errorData['param'] : null;
$code = isset($errorData['code']) ? $errorData['code'] : null;
$type = isset($errorData['type']) ? $errorData['type'] : null;
$declineCode = isset($errorData['decline_code']) ? $errorData['decline_code'] : null;

switch ($rcode) {
case 400:
// 'rate_limit' code is deprecated, but left here for backwards compatibility
// for API versions earlier than 2015-09-08
if ($code == 'rate_limit') {
return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders);
return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders, $code);
}
if ($type == 'idempotency_error') {
return new Error\Idempotency($msg, $rcode, $rbody, $resp, $rheaders);
return new Error\Idempotency($msg, $rcode, $rbody, $resp, $rheaders, $code);
}

// no break
case 404:
return new Error\InvalidRequest($msg, $param, $rcode, $rbody, $resp, $rheaders);
return new Error\InvalidRequest($msg, $param, $rcode, $rbody, $resp, $rheaders, $code);
case 401:
return new Error\Authentication($msg, $rcode, $rbody, $resp, $rheaders);
return new Error\Authentication($msg, $rcode, $rbody, $resp, $rheaders, $code);
case 402:
return new Error\Card($msg, $param, $code, $rcode, $rbody, $resp, $rheaders);
return new Error\Card($msg, $param, $code, $rcode, $rbody, $resp, $rheaders, $declineCode);
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
case 403:
return new Error\Permission($msg, $rcode, $rbody, $resp, $rheaders);
return new Error\Permission($msg, $rcode, $rbody, $resp, $rheaders, $code);
case 429:
return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders);
return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders, $code);
default:
return new Error\Api($msg, $rcode, $rbody, $resp, $rheaders);
return new Error\Api($msg, $rcode, $rbody, $resp, $rheaders, $code);
}
}

Expand Down
7 changes: 7 additions & 0 deletions lib/Error/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace Stripe\Error;

/**
* Api is a generic error that may be raised in cases where none of the other named errors cover
* the problem. It could also be raised in the case that a new error has been introduced in the API,
* but this version of the PHP SDK doesn't know how to handle it.
*
* @package Stripe\Error
*/
class Api extends Base
{
}
6 changes: 6 additions & 0 deletions lib/Error/ApiConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Stripe\Error;

/**
* ApiConnection is raised in the event that the SDK can't connect to Stripe's servers. That can be
* for a variety of different reasons from a downed network to a bad TLS certificate.
*
* @package Stripe\Error
*/
class ApiConnection extends Base
{
}
5 changes: 5 additions & 0 deletions lib/Error/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Stripe\Error;

/**
* Authentication is raised when invalid credentials are used to connect to Stripe's servers.
*
* @package Stripe\Error
*/
class Authentication extends Base
{
}
49 changes: 29 additions & 20 deletions lib/Error/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,75 @@

use Exception;

/**
* Base is the base error from which all other more specific Stripe errors derive.
*
* @package Stripe\Error
*/
abstract class Base extends Exception
{
protected $httpBody;
protected $httpHeaders;
protected $httpStatus;
protected $jsonBody;
protected $requestId;
protected $stripeCode;

public function __construct(
$message,
$httpStatus = null,
$httpBody = null,
$jsonBody = null,
$httpHeaders = null
$httpHeaders = null,
$stripeCode = null
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
) {
parent::__construct($message);
$this->httpStatus = $httpStatus;
$this->httpBody = $httpBody;
$this->jsonBody = $jsonBody;
$this->httpHeaders = $httpHeaders;
$this->requestId = null;

// TODO: make this a proper constructor argument in the next major
// release.
$this->stripeCode = isset($jsonBody["error"]["code"]) ? $jsonBody["error"]["code"] : null;
$this->stripeCode = $stripeCode;

$this->requestId = null;
if ($httpHeaders && isset($httpHeaders['Request-Id'])) {
$this->requestId = $httpHeaders['Request-Id'];
}
}

public function getStripeCode()
public function getHttpBody()
{
return $this->stripeCode;
return $this->httpBody;
}

public function getHttpStatus()
public function getHttpHeaders()
{
return $this->httpStatus;
return $this->httpHeaders;
}

public function getHttpBody()
public function getHttpStatus()
{
return $this->httpBody;
return $this->httpStatus;
}

public function getJsonBody()
{
return $this->jsonBody;
}

public function getHttpHeaders()
public function getRequestId()
{
return $this->httpHeaders;
return $this->requestId;
}

public function getRequestId()
public function getStripeCode()
{
return $this->requestId;
return $this->stripeCode;
}

public function __toString()
{
$id = $this->requestId ? " from API request '{$this->requestId}'": "";
$message = explode("\n", parent::__toString());
$message[0] .= $id;
return implode("\n", $message);
$statusStr = ($this->getHttpStatus() == null) ? "" : "(Status {$this->getHttpStatus()}) ";
$idStr = ($this->getRequestId() == null) ? "" : "(Request {$this->getRequestId()}) ";
return "{$statusStr}{$idStr}{$this->getMessage()}";
}
}
25 changes: 12 additions & 13 deletions lib/Error/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@

namespace Stripe\Error;

/**
* Card is raised when a user enters a card that can't be charged for some reason.
*
* @package Stripe\Error
*/
class Card extends Base
{
protected $declineCode;
protected $stripeParam;

public function __construct(
$message,
$stripeParam,
$stripeCode,
$httpStatus,
$httpBody,
$jsonBody,
$httpHeaders = null
$httpHeaders = null,
$declineCode = null
) {
parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders);
parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders, $stripeCode);
$this->declineCode = $declineCode;
$this->stripeParam = $stripeParam;

// TODO: once Error\Base accepts the error code as an argument, pass it
// in the call to parent::__construct() and stop setting it here.
$this->stripeCode = $stripeCode;

// This one is not like the others because it was added later and we're
// trying to do our best not to change the public interface of this class'
// constructor.
// TODO: make this a proper constructor argument in the next major
// release.
$this->declineCode = isset($jsonBody["error"]["decline_code"]) ? $jsonBody["error"]["decline_code"] : null;
}

public function getDeclineCode()
Expand Down
5 changes: 5 additions & 0 deletions lib/Error/Idempotency.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Stripe\Error;

/**
* Idempotency is raised in cases where an idempotency key was used improperly.
*
* @package Stripe\Error
*/
class Idempotency extends Base
{
}
12 changes: 10 additions & 2 deletions lib/Error/InvalidRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

namespace Stripe\Error;

/**
* InvalidRequest is raised when a request is initiated with invalid parameters.
*
* @package Stripe\Error
*/
class InvalidRequest extends Base
{
protected $stripeParam;

public function __construct(
$message,
$stripeParam,
$httpStatus = null,
$httpBody = null,
$jsonBody = null,
$httpHeaders = null
$httpHeaders = null,
$stripeCode = null
) {
parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders);
parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders, $stripeCode);
$this->stripeParam = $stripeParam;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/Error/OAuth/InvalidClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace Stripe\Error\OAuth;

/**
* InvalidClient is raised when authentication fails.
* InvalidClient is raised when the client_id does not belong to you, the stripe_user_id does not
* exist or is not connected to your application, or the API key mode (live or test mode) does not
* match the client_id mode.
*
* @package Stripe\Error\OAuth
*/
class InvalidClient extends OAuthBase
{
Expand Down
9 changes: 5 additions & 4 deletions lib/Error/OAuth/InvalidGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Stripe\Error\OAuth;

/**
* InvalidGrant is raised when a specified code doesn't exist, is
* expired, has been used, or doesn't belong to you; a refresh token doesn't
* exist, or doesn't belong to you; or if an API key's mode (live or test)
* doesn't match the mode of a code or refresh token.
* InvalidGrant is raised when a specified code doesn't exist, is expired, has been used, or doesn't
* belong to you; a refresh token doesn't exist, or doesn't belong to you; or if an API key's mode
* (live or test) doesn't match the mode of a code or refresh token.
*
* @package Stripe\Error\OAuth
*/
class InvalidGrant extends OAuthBase
{
Expand Down
6 changes: 4 additions & 2 deletions lib/Error/OAuth/InvalidRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Stripe\Error\OAuth;

/**
* InvalidRequest is raised when a code, refresh token, or grant type
* parameter is not provided, but was required.
* InvalidRequest is raised when a code, refresh token, or grant type parameter is not provided, but
* was required.
*
* @package Stripe\Error\OAuth
*/
class InvalidRequest extends OAuthBase
{
Expand Down
2 changes: 2 additions & 0 deletions lib/Error/OAuth/InvalidScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

/**
* InvalidScope is raised when an invalid scope parameter is provided.
*
* @package Stripe\Error\OAuth
*/
class InvalidScope extends OAuthBase
{
Expand Down
9 changes: 8 additions & 1 deletion lib/Error/OAuth/OAuthBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace Stripe\Error\OAuth;

/**
* OAuthBase is raised when the OAuth API returns an error.
*
* @package Stripe\Error\OAuth
*/
class OAuthBase extends \Stripe\Error\Base
{
protected $errorCode;

public function __construct(
$code,
$description,
Expand All @@ -12,7 +19,7 @@ public function __construct(
$jsonBody = null,
$httpHeaders = null
) {
parent::__construct($description, $httpStatus, $httpBody, $jsonBody, $httpHeaders);
parent::__construct($description, $httpStatus, $httpBody, $jsonBody, $httpHeaders, null);
$this->errorCode = $code;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/Error/OAuth/UnsupportedGrantType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Stripe\Error\OAuth;

/**
* UnsupportedGrantType is raised when an unuspported grant type
* parameter is specified.
* UnsupportedGrantType is raised when an unuspported grant type parameter is specified.
*
* @package Stripe\Error\OAuth
*/
class UnsupportedGrantType extends OAuthBase
{
Expand Down
5 changes: 3 additions & 2 deletions lib/Error/OAuth/UnsupportedResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Stripe\Error\OAuth;

/**
* UnsupportedResponseType is raised when an unsupported response type
* parameter is specified.
* UnsupportedResponseType is raised when an unsupported response type parameter is specified.
*
* @package Stripe\Error\OAuth
*/
class UnsupportedResponseType extends OAuthBase
{
Expand Down
5 changes: 5 additions & 0 deletions lib/Error/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Stripe\Error;

/**
* Permission is raised in cases where access was attempted on a resource that wasn't allowed.
*
* @package Stripe\Error
*/
class Permission extends Base
{
}
6 changes: 6 additions & 0 deletions lib/Error/RateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

namespace Stripe\Error;

/**
* RateLimit is raised in cases where an account is putting too much load on Stripe's API servers
* (usually by performing too many requests). Please back off on request rate.
*
* @package Stripe\Error
*/
class RateLimit extends InvalidRequest
{
}
Loading