From be3e9013f6211fb409114194d9880a6cc004486b Mon Sep 17 00:00:00 2001 From: "ryan.bibby" Date: Fri, 27 Sep 2019 14:04:29 +0100 Subject: [PATCH 1/2] Switched ApiException to use error title rather than detail as detail is not always set --- src/Exception/ApiException.php | 2 +- tests/ClientTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index 66f33380..01393244 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -24,7 +24,7 @@ public function __construct($response) } if (!empty($this->errors)) { - $this->message = $this->errors[0]->detail; + $this->message = $this->errors[0]->title; } $this->response = $response; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index b5420942..f966de83 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -257,7 +257,7 @@ public function wraps_client_exceptions_as_ukfast_exceptions() $client->request('GET', '/'); } catch (ApiException $e) { $this->assertEquals(1, count($e->getErrors())); - $this->assertEquals('Testing errors detail', $e->getMessage()); + $this->assertEquals('Testing errors', $e->getMessage()); return; } @@ -273,7 +273,7 @@ public function wraps_server_exceptions_as_ukfast_exceptions() new Response(500, [], json_encode([ 'errors' => [[ 'title' => 'Testing errors', - 'detail' => 'Testing errors detail', + 'detail' => 'Testing errors', 'status' => 500, ]] ])), @@ -286,7 +286,7 @@ public function wraps_server_exceptions_as_ukfast_exceptions() $client->request('GET', '/'); } catch (ApiException $e) { $this->assertEquals(1, count($e->getErrors())); - $this->assertEquals('Testing errors detail', $e->getMessage()); + $this->assertEquals('Testing errors', $e->getMessage()); return; } From 381e8958b3ac8bc68ddbe1f0182a4fedb10820fd Mon Sep 17 00:00:00 2001 From: "ryan.bibby" Date: Fri, 27 Sep 2019 14:40:22 +0100 Subject: [PATCH 2/2] Switched ApiException to use title for exception message only if detail does not exist --- src/Exception/ApiException.php | 7 ++++++- tests/ClientTest.php | 29 ++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index 01393244..813393d2 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -24,7 +24,12 @@ public function __construct($response) } if (!empty($this->errors)) { - $this->message = $this->errors[0]->title; + $message = $this->errors[0]->detail; + if (empty($message)) { + $message = $this->errors[0]->title; + } + + $this->message = $message; } $this->response = $response; diff --git a/tests/ClientTest.php b/tests/ClientTest.php index f966de83..6bfc0f4d 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -257,7 +257,7 @@ public function wraps_client_exceptions_as_ukfast_exceptions() $client->request('GET', '/'); } catch (ApiException $e) { $this->assertEquals(1, count($e->getErrors())); - $this->assertEquals('Testing errors', $e->getMessage()); + $this->assertEquals('Testing errors detail', $e->getMessage()); return; } @@ -293,6 +293,33 @@ public function wraps_server_exceptions_as_ukfast_exceptions() $this->expectException(ApiException::class); } + /** + * @test + */ + public function defaults_exception_message_to_title_if_no_detail_is_set() + { + $mock = new MockHandler([ + new Response(500, [], json_encode([ + 'errors' => [[ + 'title' => 'Testing errors title', + 'status' => 500, + ]] + ])), + ]); + $handler = HandlerStack::create($mock); + $guzzle = new Guzzle(['handler' => $handler]); + $client = new Client($guzzle); + + try { + $client->request('GET', '/'); + } catch (ApiException $e) { + $this->assertEquals('Testing errors title', $e->getMessage()); + return; + } + + $this->expectException(ApiException::class); + } + /** * @test */