From 8a1bb963fa1c27966f918b4b63da21e54c86e556 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Thu, 22 Aug 2019 14:18:28 -0700 Subject: [PATCH] Add ErrorObject to Stripe exceptions --- init.php | 2 + lib/Error/Base.php | 17 ++++++ lib/Error/OAuth/OAuthBase.php | 9 +++ lib/ErrorObject.php | 66 ++++++++++++++++++++++ lib/OAuthErrorObject.php | 33 +++++++++++ tests/Stripe/Error/BaseTest.php | 12 ++-- tests/Stripe/Error/OAuth/OAuthBaseTest.php | 40 +++++++++++++ tests/Stripe/ErrorObjectTest.php | 23 ++++++++ tests/Stripe/OAuthErrorObjectTest.php | 14 +++++ 9 files changed, 211 insertions(+), 5 deletions(-) create mode 100644 lib/ErrorObject.php create mode 100644 lib/OAuthErrorObject.php create mode 100644 tests/Stripe/Error/OAuth/OAuthBaseTest.php create mode 100644 tests/Stripe/ErrorObjectTest.php create mode 100644 tests/Stripe/OAuthErrorObjectTest.php diff --git a/init.php b/init.php index 7431d2fd4..2822d0c8d 100644 --- a/init.php +++ b/init.php @@ -79,6 +79,7 @@ require(dirname(__FILE__) . '/lib/Discount.php'); require(dirname(__FILE__) . '/lib/Dispute.php'); require(dirname(__FILE__) . '/lib/EphemeralKey.php'); +require(dirname(__FILE__) . '/lib/ErrorObject.php'); require(dirname(__FILE__) . '/lib/Event.php'); require(dirname(__FILE__) . '/lib/ExchangeRate.php'); require(dirname(__FILE__) . '/lib/File.php'); @@ -134,6 +135,7 @@ // OAuth require(dirname(__FILE__) . '/lib/OAuth.php'); +require(dirname(__FILE__) . '/lib/OAuthErrorObject.php'); // Webhooks require(dirname(__FILE__) . '/lib/Webhook.php'); diff --git a/lib/Error/Base.php b/lib/Error/Base.php index 204ca59d5..c6098fa22 100644 --- a/lib/Error/Base.php +++ b/lib/Error/Base.php @@ -11,6 +11,7 @@ */ abstract class Base extends Exception { + protected $error; protected $httpBody; protected $httpHeaders; protected $httpStatus; @@ -37,6 +38,13 @@ public function __construct( if ($httpHeaders && isset($httpHeaders['Request-Id'])) { $this->requestId = $httpHeaders['Request-Id']; } + + $this->error = $this->constructErrorObject(); + } + + public function getError() + { + return $this->error; } public function getHttpBody() @@ -75,4 +83,13 @@ public function __toString() $idStr = ($this->getRequestId() == null) ? "" : "(Request {$this->getRequestId()}) "; return "{$statusStr}{$idStr}{$this->getMessage()}"; } + + protected function constructErrorObject() + { + if (is_null($this->jsonBody) || !array_key_exists('error', $this->jsonBody)) { + return null; + } + + return \Stripe\ErrorObject::constructFrom($this->jsonBody['error']); + } } diff --git a/lib/Error/OAuth/OAuthBase.php b/lib/Error/OAuth/OAuthBase.php index 805646135..7b2890528 100644 --- a/lib/Error/OAuth/OAuthBase.php +++ b/lib/Error/OAuth/OAuthBase.php @@ -27,4 +27,13 @@ public function getErrorCode() { return $this->errorCode; } + + protected function constructErrorObject() + { + if (is_null($this->jsonBody)) { + return null; + } + + return \Stripe\ErrorObject::constructFrom($this->jsonBody); + } } diff --git a/lib/ErrorObject.php b/lib/ErrorObject.php new file mode 100644 index 000000000..1cfe52399 --- /dev/null +++ b/lib/ErrorObject.php @@ -0,0 +1,66 @@ + 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); + } +} diff --git a/lib/OAuthErrorObject.php b/lib/OAuthErrorObject.php new file mode 100644 index 000000000..5d78e1f8b --- /dev/null +++ b/lib/OAuthErrorObject.php @@ -0,0 +1,33 @@ + null, + 'error_description' => null, + ], $values); + parent::refreshFrom($values, $opts, $partial); + } +} diff --git a/tests/Stripe/Error/BaseTest.php b/tests/Stripe/Error/BaseTest.php index 43a3f5dce..e1c1ba183 100644 --- a/tests/Stripe/Error/BaseTest.php +++ b/tests/Stripe/Error/BaseTest.php @@ -4,13 +4,13 @@ class BaseTest extends TestCase { - public function createFixture($params = []) + public function createFixture() { return $this->getMockForAbstractClass(\Stripe\Error\Base::class, [ 'message', 200, - '{"key": "value"}', - ['key' => 'value'], + '{"error": {"code": "some_code"}}', + ['error' => ['code' => 'some_code']], [ 'Some-Header' => 'Some Value', 'Request-Id' => 'req_test', @@ -22,10 +22,12 @@ public function testGetters() { $e = $this->createFixture(); $this->assertSame(200, $e->getHttpStatus()); - $this->assertSame('{"key": "value"}', $e->getHttpBody()); - $this->assertSame(['key' => 'value'], $e->getJsonBody()); + $this->assertSame('{"error": {"code": "some_code"}}', $e->getHttpBody()); + $this->assertSame(['error' => ['code' => 'some_code']], $e->getJsonBody()); $this->assertSame('Some Value', $e->getHttpHeaders()['Some-Header']); $this->assertSame('req_test', $e->getRequestId()); + $this->assertNotNull($e->getError()); + $this->assertSame('some_code', $e->getError()->code); } public function testToString() diff --git a/tests/Stripe/Error/OAuth/OAuthBaseTest.php b/tests/Stripe/Error/OAuth/OAuthBaseTest.php new file mode 100644 index 000000000..6489e8dc1 --- /dev/null +++ b/tests/Stripe/Error/OAuth/OAuthBaseTest.php @@ -0,0 +1,40 @@ +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); + } +} diff --git a/tests/Stripe/ErrorObjectTest.php b/tests/Stripe/ErrorObjectTest.php new file mode 100644 index 000000000..ffde88680 --- /dev/null +++ b/tests/Stripe/ErrorObjectTest.php @@ -0,0 +1,23 @@ +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); + } +} diff --git a/tests/Stripe/OAuthErrorObjectTest.php b/tests/Stripe/OAuthErrorObjectTest.php new file mode 100644 index 000000000..0e9fb51bf --- /dev/null +++ b/tests/Stripe/OAuthErrorObjectTest.php @@ -0,0 +1,14 @@ +assertNull($error->error); + $this->assertNull($error->error_description); + } +}