From 47a8566940968a3607a77361a0f3f79471ac2de5 Mon Sep 17 00:00:00 2001 From: Andrew McIntosh Date: Sat, 30 Mar 2024 15:48:38 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Remove=20"new=20style"=20a?= =?UTF-8?q?ccounting=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As they no longer exist --- src/Resource/AccountingResource.php | 65 ++++------------------- tests/Resource/AccountingResourceTest.php | 49 +---------------- 2 files changed, 11 insertions(+), 103 deletions(-) diff --git a/src/Resource/AccountingResource.php b/src/Resource/AccountingResource.php index d17860d..c39836f 100644 --- a/src/Resource/AccountingResource.php +++ b/src/Resource/AccountingResource.php @@ -94,56 +94,6 @@ protected function makeRequest(string $method, string $url, array $data = null): return $responseData; } - /** - * Parse the json response for old-style accounting endpoint errors and create a FreshBooksException from it. - * - * @param int $statusCode HTTP status code - * @param array $responseData The json-parsed response - * @param string $rawRespone The raw response body - * @return void - */ - private function createOldResponseError(int $statusCode, array $responseData, string $rawRespone): void - { - $errors = $responseData['response']['errors']; - if (array_key_exists(0, $errors)) { - $message = $errors[0]['message'] ?? 'Unknown error'; - $errorCode = $errors[0]['errno'] ?? null; - throw new FreshBooksException($message, $statusCode, null, $rawRespone, $errorCode, $errors); - } - - $message = $errors['message'] ?? 'Unknown error'; - $errorCode = $errors['errno'] ?? null; - throw new FreshBooksException($message, $statusCode, null, $rawRespone, $errorCode, $errors); - } - - /** - * Parse the json response for new-style accounting endpoint errors and create a FreshBooksException from it. - * - * @param int $statusCode HTTP status code - * @param array $responseData The json-parsed response - * @param string $rawRespone The raw response body - * @return void - */ - private function createNewResponseError(int $statusCode, array $responseData, string $rawRespone): void - { - $message = $responseData['message']; - $errorCode = null; - $details = []; - - foreach ($responseData['details'] as $detail) { - if (in_array('type.googleapis.com/google.rpc.ErrorInfo', $detail)) { - $errorCode = intval($detail['reason']) ?? null; - if (array_key_exists('metadata', $detail)) { - $details[] = $detail['metadata']; - if (array_key_exists('message', $detail['metadata'])) { - $message = $detail['metadata']['message']; - } - } - } - } - throw new FreshBooksException($message, $statusCode, null, $rawRespone, $errorCode, $details); - } - /** * Create a FreshBooksException from the json response from the accounting endpoint. * @@ -155,12 +105,17 @@ private function createNewResponseError(int $statusCode, array $responseData, st protected function handleError(int $statusCode, array $responseData, string $rawRespone): void { if (array_key_exists('response', $responseData) && array_key_exists('errors', $responseData['response'])) { - $this->createOldResponseError($statusCode, $responseData, $rawRespone); - } elseif (array_key_exists('message', $responseData) && array_key_exists('code', $responseData)) { - $this->createNewResponseError($statusCode, $responseData, $rawRespone); - } else { - throw new FreshBooksException('Unknown error', $statusCode, null, $rawRespone); + $errors = $responseData['response']['errors']; + if (array_key_exists(0, $errors)) { + $message = $errors[0]['message'] ?? 'Unknown error'; + $errorCode = $errors[0]['errno'] ?? null; + throw new FreshBooksException($message, $statusCode, null, $rawRespone, $errorCode, $errors); + } + $message = $errors['message'] ?? 'Unknown error'; + $errorCode = $errors['errno'] ?? null; + throw new FreshBooksException($message, $statusCode, null, $rawRespone, $errorCode, $errors); } + throw new FreshBooksException('Unknown error', $statusCode, null, $rawRespone); } private function rejectMissing(string $name): void diff --git a/tests/Resource/AccountingResourceTest.php b/tests/Resource/AccountingResourceTest.php index 42545cb..e07c14b 100644 --- a/tests/Resource/AccountingResourceTest.php +++ b/tests/Resource/AccountingResourceTest.php @@ -93,7 +93,7 @@ public function testGetWrongErrorContent(): void $resource->get($this->accountId, $clientId); } - public function testGetNotFoundOldError(): void + public function testGetNotFoundError(): void { $clientId = 12345; $mockHttpClient = $this->getMockHttpClient( @@ -131,53 +131,6 @@ public function testGetNotFoundOldError(): void } } - public function testGetNotFoundNewError(): void - { - $clientId = 12345; - $mockHttpClient = $this->getMockHttpClient( - 404, - [ - 'code' => 5, - 'message' => 'Request failed with status_code: 404', - 'details' => [ - [ - '@type' => 'type.googleapis.com/google.rpc.ErrorInfo', - 'reason' => '1012', - 'domain' => 'accounting.api.freshbooks.com', - 'metadata' => [ - 'object' => 'client', - 'message' => 'Client not found.', - 'value' => '12345', - 'field' => 'userid' - ] - ] - ] - ] - ); - - $resource = new AccountingResource($mockHttpClient, 'users/clients', Client::class, ClientList::class); - - try { - $resource->get($this->accountId, $clientId); - $this->fail('FreshBooksException was not thrown'); - } catch (FreshBooksException $e) { - $this->assertSame('Client not found.', $e->getMessage()); - $this->assertSame(404, $e->getCode()); - $this->assertSame(1012, $e->getErrorCode()); - $this->assertSame( - [ - [ - 'object' => 'client', - 'message' => 'Client not found.', - 'value' => '12345', - 'field' => 'userid' - ] - ], - $e->getErrorDetails() - ); - } - } - public function testList(): void { $clientId = 12345;