Skip to content

Commit f46a878

Browse files
committed
Merge pull request #40 from intercom/dehora/37
Populate exceptions with errors from the response
2 parents 4e1aaa3 + 50f89de commit f46a878

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/Intercom/Exception/IntercomException.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public static function factory(RequestInterface $request, Response $response)
4949
$e->setRequest($request);
5050

5151
// Sets the errors if the error response is the standard Intercom error type
52-
if (!static::isValidIntercomError($response->json())) {
53-
$e->errors = $response->json()['errors'];
52+
if (static::isValidIntercomError($response->json())) {
53+
$e->setErrors($response->json()['errors']);
5454
}
5555

5656
return $e;
@@ -61,7 +61,7 @@ public static function factory(RequestInterface $request, Response $response)
6161
* @param $response_body
6262
* @return bool
6363
*/
64-
private function isValidIntercomError($response_body)
64+
private static function isValidIntercomError($response_body)
6565
{
6666
if (array_key_exists('type', $response_body) &&
6767
$response_body['type'] == 'error.list' &&
@@ -81,4 +81,9 @@ public function getErrors()
8181
{
8282
return $this->errors;
8383
}
84+
85+
public function setErrors($errors)
86+
{
87+
$this->errors = $errors;
88+
}
8489
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Intercom;
4+
5+
class IntercomExceptionTest extends IntercomTestCase
6+
{
7+
public function test404Exception()
8+
{
9+
try {
10+
$this->setMockResponse($this->client, 'Error/Error.txt');
11+
$response = $this->client->getUser(['id' => '123456']);
12+
$this->fail('An Exception\ClientErrorResponseException for a 404 response should have been raised');
13+
}
14+
catch (Exception\ClientErrorResponseException $expected) {
15+
$errors = $expected->getErrors();
16+
$this->assertEquals(1, count($errors));
17+
$this->assertEquals('not_found', $errors[0]['code'] );
18+
$this->assertEquals('No such user with id[123456]', $errors[0]['message'] );
19+
}
20+
}
21+
}

tests/Mock/Error/Error.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
HTTP/1.1 404 Not Found
2+
Content-Type: application/json; charset=utf-8
3+
Date: Mon, 25 Aug 2014 23:57:54 GMT
4+
5+
{
6+
"type": "error.list",
7+
"errors": [
8+
{
9+
"code": "not_found",
10+
"message": "No such user with id[123456]"
11+
}
12+
]
13+
}

0 commit comments

Comments
 (0)