From e361d17262280e56648b5e29da033cdccdcd396c Mon Sep 17 00:00:00 2001 From: Andrew McIntosh Date: Sat, 30 Mar 2024 10:55:01 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Update=20events=20resource=20to=20h?= =?UTF-8?q?andle=20new=20error=20responses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The error response for the events API has changed. The events resource needs to be updated to handle the new error responses. Currently all errors are creating exceptions with "Unknwon error" as the message. Closes #65 --- CHANGELOG.md | 1 + src/Resource/EventsResource.php | 16 +++------- tests/Resource/EventsResourceTest.php | 44 +++++---------------------- 3 files changed, 14 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6263e4a..d24839a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Support for PHP 8.3 +- Handle new API version webhook event errors ## 0.7.0 diff --git a/src/Resource/EventsResource.php b/src/Resource/EventsResource.php index c48d862..d6fb188 100644 --- a/src/Resource/EventsResource.php +++ b/src/Resource/EventsResource.php @@ -45,23 +45,17 @@ protected function getUrl(string $accountId, int $resourceId = null): string */ protected function handleError(int $statusCode, array $responseData, string $rawRespone): void { - if (!array_key_exists('message', $responseData) || !array_key_exists('code', $responseData)) { + if (!array_key_exists('message', $responseData)) { throw new FreshBooksException('Unknown error', $statusCode, null, $rawRespone); } $message = $responseData['message']; - $errorCode = null; $details = []; - - foreach ($responseData['details'] as $detail) { - if ( - in_array('type.googleapis.com/google.rpc.BadRequest', $detail) - && array_key_exists('fieldViolations', $detail) - ) { - $details = $detail['fieldViolations']; - } + if (array_key_exists('details', $responseData)) { + $details = $responseData['details']; } - throw new FreshBooksException($message, $statusCode, null, $rawRespone, $errorCode, $details); + + throw new FreshBooksException($message, $statusCode, null, $rawRespone, null, $details); } /** diff --git a/tests/Resource/EventsResourceTest.php b/tests/Resource/EventsResourceTest.php index fbca632..b801960 100644 --- a/tests/Resource/EventsResourceTest.php +++ b/tests/Resource/EventsResourceTest.php @@ -46,17 +46,8 @@ public function testGetNotFoundError(): void $mockHttpClient = $this->getMockHttpClient( 404, [ - 'code' => 5, - 'message' => 'Requested resource could not be found.', - 'details' => [ - [ - '@type' => 'type.googleapis.com/google.rpc.Help', - 'links' => [ - 'description' => 'API Documentation', - 'url' => 'https://www.freshbooks.com/api/webhooks', - ] - ] - ] + 'errno' => 404, + 'message' => 'Requested resource could not be found.' ] ); @@ -79,26 +70,9 @@ public function testCreateValidationError(): void $mockHttpClient = $this->getMockHttpClient( 400, [ - 'code' => 3, - 'message' => 'Invalid data in this request.', - 'details' => [ - [ - '@type' => 'type.googleapis.com/google.rpc.BadRequest', - 'fieldViolations' => [ - [ - 'field' => 'event', - 'description' => 'Value error, Unrecognized event.' - ] - ] - ], - [ - '@type' => 'type.googleapis.com/google.rpc.Help', - 'links' => [ - 'description' => 'API Documentation', - 'url' => 'https://www.freshbooks.com/api/webhooks', - ] - ] - ] + 'errno' => 3, + 'message' => 'The request was well-formed but was unable to be followed due to semantic errors.' + . '\nevent: Value error, Unrecognized event.' ] ); @@ -108,13 +82,11 @@ public function testCreateValidationError(): void $resource->create($this->accountId, data: []); $this->fail('FreshBooksException was not thrown'); } catch (FreshBooksException $e) { - $this->assertSame('Invalid data in this request.', $e->getMessage()); + $this->assertSame('The request was well-formed but was unable to be followed due to semantic errors.' + . '\nevent: Value error, Unrecognized event.', $e->getMessage()); $this->assertSame(400, $e->getCode()); $this->assertNull($e->getErrorCode()); - $this->assertSame([[ - 'field' => 'event', - 'description' => 'Value error, Unrecognized event.' - ]], $e->getErrorDetails()); + $this->assertSame([], $e->getErrorDetails()); } }