Skip to content

Commit

Permalink
Merge pull request #118 from rbibby/master
Browse files Browse the repository at this point in the history
Switched ApiException to use error title rather than detail as detail…
  • Loading branch information
bluemorbo authored Sep 30, 2019
2 parents 31d45c6 + 381e895 commit 9484b9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Exception/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public function __construct($response)
}

if (!empty($this->errors)) {
$this->message = $this->errors[0]->detail;
$message = $this->errors[0]->detail;
if (empty($message)) {
$message = $this->errors[0]->title;
}

$this->message = $message;
}

$this->response = $response;
Expand Down
31 changes: 29 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]]
])),
Expand All @@ -286,7 +286,34 @@ 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;
}

$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;
}

Expand Down

0 comments on commit 9484b9e

Please sign in to comment.