Skip to content

Commit

Permalink
Merge pull request #66 from amcintosh/issue-65-new-events-error-form
Browse files Browse the repository at this point in the history
✨ Update events resource to handle new error responses
  • Loading branch information
amcintosh committed Mar 30, 2024
2 parents 6fbda3c + e361d17 commit 36e86b9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Support for PHP 8.3
- Handle new API version webhook event errors

## 0.7.0

Expand Down
16 changes: 5 additions & 11 deletions src/Resource/EventsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
44 changes: 8 additions & 36 deletions tests/Resource/EventsResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
]
);

Expand All @@ -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.'
]
);

Expand All @@ -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());
}
}

Expand Down

0 comments on commit 36e86b9

Please sign in to comment.