Skip to content

Commit

Permalink
♻️ Remove "new style" accounting errors
Browse files Browse the repository at this point in the history
As they no longer exist
  • Loading branch information
amcintosh committed Mar 30, 2024
1 parent 151d671 commit 47a8566
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 103 deletions.
65 changes: 10 additions & 55 deletions src/Resource/AccountingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand Down
49 changes: 1 addition & 48 deletions tests/Resource/AccountingResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 47a8566

Please sign in to comment.