Skip to content

Commit

Permalink
Merge pull request #69 from turbo124/main
Browse files Browse the repository at this point in the history
Improve error handling message
  • Loading branch information
turbo124 committed Sep 20, 2021
2 parents 61c9cf3 + d875fcf commit 05e56a5
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 24 deletions.
27 changes: 19 additions & 8 deletions src/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ class ApiException extends \Exception

public static function createFromResponse($response)
{
$object = static::parseResponseBody($response);
$error = static::parseResponseBody($response);

$field = null;
if (! empty($object->field)) {
$field = $object->field;
}
// die(var_dump($object));

return new self(
"Error executing API call ({$object->message})}",
"Error executing API call ({$error})}",
$response->getStatusCode(),
$field
);
Expand All @@ -34,13 +30,28 @@ public static function createFromResponse($response)
protected static function parseResponseBody($response)
{
$body = (string) $response->getBody();

$error = "";
$object = @json_decode($body);

if(property_exists($object, "message"))
$error = $object->message;

if(property_exists($object, "errors"))
{
$properties = array_keys(get_object_vars($object->errors));

if(is_array($properties))
$error_array = $object->errors->{$properties[0]};

if(is_array($error_array))
$error = $error_array[0];

}

if (json_last_error() !== JSON_ERROR_NONE) {
throw new self("Unable to decode response: '{$body}'.");
}

return $object;
return $error;
}
}
2 changes: 1 addition & 1 deletion tests/BulkActionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class BulkActionsTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testArchive()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class ClientsTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testClients()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/CompaniesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class CompaniesTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testCompanies()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/CreditsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class CreditsTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testCredits()
{
Expand Down Expand Up @@ -75,7 +75,7 @@ public function testCreditPost()
$ninja = new InvoiceNinja($this->token);
$ninja->setUrl($this->url);

$credits = $ninja->credits->create(['client_id' => '7LDdwRb1YK']);
$credits = $ninja->credits->create(['clint_id' => '7LDdwRb1YK']);

$this->assertTrue(is_array($credits));

Expand Down
2 changes: 1 addition & 1 deletion tests/ExpensesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class ExpensesTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testExpenses()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/InvoicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class InvoicesTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testInvoices()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/PaymentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class PaymentsTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testPayments()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ProductsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class ProductsTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testProducts()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class ProjectsTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testProjects()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/QuotesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class QuotesTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testQuotes()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/RecurringInvoicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class RecurringInvoicesTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testInvoices()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/StaticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StaticsTest extends TestCase
{

protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testGetCurrencies()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TasksTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testTasks()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TaxRatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TaxRatesTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public $faker;

Expand Down
2 changes: 1 addition & 1 deletion tests/VendorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class VendorsTest extends TestCase
{
protected string $token = "company-token-test";
protected string $url = "https://ninja.test";
protected string $url = "http://ninja.test:8000";

public function testVendors()
{
Expand Down

0 comments on commit 05e56a5

Please sign in to comment.