Skip to content

Commit 381e895

Browse files
committed
Switched ApiException to use title for exception message only if detail does not exist
1 parent be3e901 commit 381e895

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Exception/ApiException.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ public function __construct($response)
2424
}
2525

2626
if (!empty($this->errors)) {
27-
$this->message = $this->errors[0]->title;
27+
$message = $this->errors[0]->detail;
28+
if (empty($message)) {
29+
$message = $this->errors[0]->title;
30+
}
31+
32+
$this->message = $message;
2833
}
2934

3035
$this->response = $response;

tests/ClientTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public function wraps_client_exceptions_as_ukfast_exceptions()
257257
$client->request('GET', '/');
258258
} catch (ApiException $e) {
259259
$this->assertEquals(1, count($e->getErrors()));
260-
$this->assertEquals('Testing errors', $e->getMessage());
260+
$this->assertEquals('Testing errors detail', $e->getMessage());
261261
return;
262262
}
263263

@@ -293,6 +293,33 @@ public function wraps_server_exceptions_as_ukfast_exceptions()
293293
$this->expectException(ApiException::class);
294294
}
295295

296+
/**
297+
* @test
298+
*/
299+
public function defaults_exception_message_to_title_if_no_detail_is_set()
300+
{
301+
$mock = new MockHandler([
302+
new Response(500, [], json_encode([
303+
'errors' => [[
304+
'title' => 'Testing errors title',
305+
'status' => 500,
306+
]]
307+
])),
308+
]);
309+
$handler = HandlerStack::create($mock);
310+
$guzzle = new Guzzle(['handler' => $handler]);
311+
$client = new Client($guzzle);
312+
313+
try {
314+
$client->request('GET', '/');
315+
} catch (ApiException $e) {
316+
$this->assertEquals('Testing errors title', $e->getMessage());
317+
return;
318+
}
319+
320+
$this->expectException(ApiException::class);
321+
}
322+
296323
/**
297324
* @test
298325
*/

0 commit comments

Comments
 (0)