Skip to content

Commit

Permalink
Fixing Fixer error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
florianv committed Apr 23, 2018
1 parent a2c1de3 commit 5bd830f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/Service/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function createRate($url, CurrencyPair $currencyPair)
$data = StringUtil::jsonToArray($content);

if (isset($data['error'])) {
throw new Exception($data['error']);
throw new Exception($this->getErrorMessage($data['error']['code']));
}

if (isset($data['rates'][$currencyPair->getQuoteCurrency()])) {
Expand All @@ -106,4 +106,36 @@ private function createRate($url, CurrencyPair $currencyPair)

return null;
}

/**
* Gets the error message corresponding to the error code.
*
* @param string $code The error code
*
* @return string
*/
private function getErrorMessage($code)
{
$errors = [
404 => 'The requested resource does not exist.',
101 => 'No API Key was specified or an invalid API Key was specified.',
103 => 'The requested API endpoint does not exist.',
104 => 'The maximum allowed API amount of monthly API requests has been reached.',
105 => 'The current subscription plan does not support this API endpoint.',
106 => 'The current request did not return any results.',
102 => 'The account this API request is coming from is inactive.',
201 => 'An invalid base currency has been entered.',
202 => 'One or more invalid symbols have been specified.',
301 => 'No date has been specified.',
302 => 'An invalid date has been specified.',
403 => 'No or an invalid amount has been specified.',
501 => 'No or an invalid timeframe has been specified.',
502 => 'No or an invalid "start_date" has been specified.',
503 => 'No or an invalid "end_date" has been specified.',
504 => 'An invalid timeframe has been specified.',
505 => 'The specified timeframe is too long, exceeding 365 days.',
];

return isset($errors[$code]) ? $errors[$code] : '';
}
}
6 changes: 5 additions & 1 deletion tests/Fixtures/Service/Fixer/error.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"error": "Invalid base"
"success": false,
"error": {
"code": 105,
"type": "base_currency_access_restricted"
}
}
1 change: 1 addition & 0 deletions tests/Tests/Service/FixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function it_supports_all_queries()
/**
* @test
* @expectedException \Exchanger\Exception\Exception
* @expectedExceptionMessage The current subscription plan does not support this API endpoint.
*/
public function it_throws_an_exception_with_error_response()
{
Expand Down

0 comments on commit 5bd830f

Please sign in to comment.