Skip to content

Commit 05e56a5

Browse files
authored
Merge pull request #69 from turbo124/main
Improve error handling message
2 parents 61c9cf3 + d875fcf commit 05e56a5

16 files changed

+35
-24
lines changed

src/Exceptions/ApiException.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@ class ApiException extends \Exception
1616

1717
public static function createFromResponse($response)
1818
{
19-
$object = static::parseResponseBody($response);
19+
$error = static::parseResponseBody($response);
2020

2121
$field = null;
22-
if (! empty($object->field)) {
23-
$field = $object->field;
24-
}
25-
// die(var_dump($object));
2622

2723
return new self(
28-
"Error executing API call ({$object->message})}",
24+
"Error executing API call ({$error})}",
2925
$response->getStatusCode(),
3026
$field
3127
);
@@ -34,13 +30,28 @@ public static function createFromResponse($response)
3430
protected static function parseResponseBody($response)
3531
{
3632
$body = (string) $response->getBody();
37-
33+
$error = "";
3834
$object = @json_decode($body);
3935

36+
if(property_exists($object, "message"))
37+
$error = $object->message;
38+
39+
if(property_exists($object, "errors"))
40+
{
41+
$properties = array_keys(get_object_vars($object->errors));
42+
43+
if(is_array($properties))
44+
$error_array = $object->errors->{$properties[0]};
45+
46+
if(is_array($error_array))
47+
$error = $error_array[0];
48+
49+
}
50+
4051
if (json_last_error() !== JSON_ERROR_NONE) {
4152
throw new self("Unable to decode response: '{$body}'.");
4253
}
4354

44-
return $object;
55+
return $error;
4556
}
4657
}

tests/BulkActionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class BulkActionsTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testArchive()
2323
{

tests/ClientsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class ClientsTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testClients()
2323
{

tests/CompaniesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class CompaniesTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testCompanies()
2323
{

tests/CreditsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class CreditsTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testCredits()
2323
{
@@ -75,7 +75,7 @@ public function testCreditPost()
7575
$ninja = new InvoiceNinja($this->token);
7676
$ninja->setUrl($this->url);
7777

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

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

tests/ExpensesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class ExpensesTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testExpenses()
2323
{

tests/InvoicesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class InvoicesTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testInvoices()
2323
{

tests/PaymentsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class PaymentsTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testPayments()
2323
{

tests/ProductsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class ProductsTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testProducts()
2323
{

tests/ProjectsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class ProjectsTest extends TestCase
1818
{
1919
protected string $token = "company-token-test";
20-
protected string $url = "https://ninja.test";
20+
protected string $url = "http://ninja.test:8000";
2121

2222
public function testProjects()
2323
{

0 commit comments

Comments
 (0)