Skip to content

Commit

Permalink
Update constructors of Stripe exception classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Dec 11, 2018
1 parent 3493e70 commit 84dbb9d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 41 deletions.
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);
}

// intentional fall-through
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);
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
37 changes: 21 additions & 16 deletions lib/Error/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,62 @@

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
) {
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()
Expand Down
20 changes: 7 additions & 13 deletions lib/Error/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,22 @@

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
7 changes: 5 additions & 2 deletions lib/Error/InvalidRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

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
4 changes: 3 additions & 1 deletion lib/Error/OAuth/OAuthBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class OAuthBase extends \Stripe\Error\Base
{
protected $errorCode;

public function __construct(
$code,
$description,
Expand All @@ -12,7 +14,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
4 changes: 3 additions & 1 deletion lib/Error/SignatureVerification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

class SignatureVerification extends Base
{
protected $sigHeader;

public function __construct(
$message,
$sigHeader,
$httpBody = null
) {
parent::__construct($message, null, $httpBody, null, null);
parent::__construct($message, null, $httpBody, null, null, null);
$this->sigHeader = $sigHeader;
}

Expand Down

0 comments on commit 84dbb9d

Please sign in to comment.