-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #705 from stripe/ob-error-object
Add ErrorObject to Stripe exceptions
- Loading branch information
Showing
9 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
/** | ||
* Class ErrorObject | ||
* | ||
* @property string $charge For card errors, the ID of the failed charge. | ||
* @property string $code For some errors that could be handled | ||
* programmatically, a short string indicating the error code reported. | ||
* @property string $decline_code For card errors resulting from a card issuer | ||
* decline, a short string indicating the card issuer's reason for the | ||
* decline if they provide one. | ||
* @property string $doc_url A URL to more information about the error code | ||
* reported. | ||
* @property string $message A human-readable message providing more details | ||
* about the error. For card errors, these messages can be shown to your | ||
* users. | ||
* @property string $param If the error is parameter-specific, the parameter | ||
* related to the error. For example, you can use this to display a message | ||
* near the correct form field. | ||
* @property PaymentIntent $payment_intent The PaymentIntent object for errors | ||
* returned on a request involving a PaymentIntent. | ||
* @property PaymentMethod $payment_method The PaymentMethod object for errors | ||
* returned on a request involving a PaymentMethod. | ||
* @property SetupIntent $setup_intent The SetupIntent object for errors | ||
* returned on a request involving a SetupIntent. | ||
* @property StripeObject $source The source object for errors returned on a | ||
* request involving a source. | ||
* @property string $type The type of error returned. One of | ||
* `api_connection_error`, `api_error`, `authentication_error`, | ||
* `card_error`, `idempotency_error`, `invalid_request_error`, or | ||
* `rate_limit_error`. | ||
* | ||
* @package Stripe | ||
*/ | ||
class ErrorObject extends StripeObject | ||
{ | ||
/** | ||
* Refreshes this object using the provided values. | ||
* | ||
* @param array $values | ||
* @param null|string|array|Util\RequestOptions $opts | ||
* @param boolean $partial Defaults to false. | ||
*/ | ||
public function refreshFrom($values, $opts, $partial = false) | ||
{ | ||
// Unlike most other API resources, the API will omit attributes in | ||
// error objects when they have a null value. We manually set default | ||
// values here to facilitate generic error handling. | ||
$values = array_merge([ | ||
'charge' => null, | ||
'code' => null, | ||
'decline_code' => null, | ||
'doc_url' => null, | ||
'message' => null, | ||
'param' => null, | ||
'payment_intent' => null, | ||
'payment_method' => null, | ||
'setup_intent' => null, | ||
'source' => null, | ||
'type' => null, | ||
], $values); | ||
parent::refreshFrom($values, $opts, $partial); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
/** | ||
* Class OAuthErrorObject | ||
* | ||
* @property string $error | ||
* @property string $error_description | ||
* | ||
* @package Stripe | ||
*/ | ||
class OAuthErrorObject extends StripeObject | ||
{ | ||
/** | ||
* Refreshes this object using the provided values. | ||
* | ||
* @param array $values | ||
* @param null|string|array|Util\RequestOptions $opts | ||
* @param boolean $partial Defaults to false. | ||
*/ | ||
public function refreshFrom($values, $opts, $partial = false) | ||
{ | ||
// Unlike most other API resources, the API will omit attributes in | ||
// error objects when they have a null value. We manually set default | ||
// values here to facilitate generic error handling. | ||
$values = array_merge([ | ||
'error' => null, | ||
'error_description' => null, | ||
], $values); | ||
parent::refreshFrom($values, $opts, $partial); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
class OAuthBaseTest extends TestCase | ||
{ | ||
public function createFixture() | ||
{ | ||
return $this->getMockForAbstractClass(\Stripe\Error\OAuth\OAuthBase::class, [ | ||
'code', | ||
'description', | ||
200, | ||
'{"error": "code", "error_description": "description"}', | ||
['error' => 'code', 'error_description' => 'description'], | ||
[ | ||
'Some-Header' => 'Some Value', | ||
'Request-Id' => 'req_test', | ||
], | ||
]); | ||
} | ||
|
||
public function testGetters() | ||
{ | ||
$e = $this->createFixture(); | ||
$this->assertSame(200, $e->getHttpStatus()); | ||
$this->assertSame('{"error": "code", "error_description": "description"}', $e->getHttpBody()); | ||
$this->assertSame(['error' => 'code', 'error_description' => 'description'], $e->getJsonBody()); | ||
$this->assertSame('Some Value', $e->getHttpHeaders()['Some-Header']); | ||
$this->assertSame('req_test', $e->getRequestId()); | ||
$this->assertNotNull($e->getError()); | ||
$this->assertSame('code', $e->getError()->error); | ||
$this->assertSame('description', $e->getError()->error_description); | ||
} | ||
|
||
public function testToString() | ||
{ | ||
$e = $this->createFixture(); | ||
$this->assertContains("(Request req_test)", (string)$e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
class ErrorObjectTest extends TestCase | ||
{ | ||
public function testDefaultValues() | ||
{ | ||
$error = ErrorObject::constructFrom([]); | ||
|
||
$this->assertNull($error->charge); | ||
$this->assertNull($error->code); | ||
$this->assertNull($error->decline_code); | ||
$this->assertNull($error->doc_url); | ||
$this->assertNull($error->message); | ||
$this->assertNull($error->param); | ||
$this->assertNull($error->payment_intent); | ||
$this->assertNull($error->payment_method); | ||
$this->assertNull($error->setup_intent); | ||
$this->assertNull($error->source); | ||
$this->assertNull($error->type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
class OAuthErrorObjectTest extends TestCase | ||
{ | ||
public function testDefaultValues() | ||
{ | ||
$error = OAuthErrorObject::constructFrom([]); | ||
|
||
$this->assertNull($error->error); | ||
$this->assertNull($error->error_description); | ||
} | ||
} |