Skip to content

Commit 14b846a

Browse files
committed
Merge pull request #94 from intercom/jo/spec-server-error
add test for server exceptions
2 parents 0ad5a47 + 27f07ec commit 14b846a

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

src/Intercom/Exception/IntercomException.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ class IntercomException extends BadResponseException
2121
*/
2222
public static function factory(RequestInterface $request, Response $response)
2323
{
24-
if (!static::isValidIntercomError($response->json())) {
25-
$label = 'Unsuccessful response';
26-
$class = __CLASS__;
24+
$response_json = $response->json();
25+
$intercom_unavailable_error = NULL;
26+
27+
if (!static::isValidIntercomError($response_json)) {
28+
if ($response->isServerError()) {
29+
$label = 'Server error response';
30+
$class = __NAMESPACE__ . '\\ServerErrorResponseException';
31+
$intercom_unavailable_error = 'Service Unavailable: Back-end server is at capacity';
32+
} else {
33+
$label = 'Unsuccessful response';
34+
$class = __CLASS__;
35+
}
2736
} elseif ($response->isClientError()) {
2837
$label = 'Client error response';
2938
$class = __NAMESPACE__ . '\\ClientErrorResponseException';
@@ -49,8 +58,12 @@ public static function factory(RequestInterface $request, Response $response)
4958
$e->setRequest($request);
5059

5160
// Sets the errors if the error response is the standard Intercom error type
52-
if (static::isValidIntercomError($response->json())) {
53-
$e->setErrors($response->json()['errors']);
61+
if (static::isValidIntercomError($response_json)) {
62+
$e->setErrors($response_json['errors']);
63+
} elseif($intercom_unavailable_error != NULL) {
64+
$e ->setErrors([array(
65+
'code' => 'service_unavailable',
66+
"message" => $intercom_unavailable_error)]);
5467
}
5568

5669
return $e;

tests/Intercom/Exception/IntercomExceptionTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,33 @@ public function test404Exception()
1818
$this->assertEquals('No such user with id[123456]', $errors[0]['message'] );
1919
}
2020
}
21-
}
21+
22+
public function test503Exception()
23+
{
24+
try {
25+
$this->setMockResponse($this->client, 'Error/Error503.txt');
26+
$response = $this->client->getUser(['id' => '123456']);
27+
$this->fail('An Exception\ClientErrorResponseException for a 503 response should have been raised');
28+
}
29+
catch (Exception\ServerErrorResponseException $expected) {
30+
$errors = $expected->getErrors();
31+
$this->assertEquals(1, count($errors));
32+
$this->assertEquals('service_unavailable', $errors[0]['code'] );
33+
$this->assertEquals('Sorry, the API service is temporarily unavailable', $errors[0]['message'] );
34+
}
35+
}
36+
37+
public function test503ExceptionELB()
38+
{
39+
try {
40+
$this->setMockResponse($this->client, 'Error/Error503ELB.txt');
41+
$response = $this->client->getUser(['id' => '123456']);
42+
$this->fail('An Exception\ClientErrorResponseException for a 503 ELB response should have been raised');
43+
}
44+
catch (Exception\ServerErrorResponseException $expected) {
45+
$errors = $expected->getErrors();
46+
$this->assertEquals(1, count($errors));
47+
$this->assertEquals('Service Unavailable: Back-end server is at capacity', $errors[0]['message'] );
48+
}
49+
}
50+
}

tests/Mock/Error/Error503.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
HTTP/1.1 503 Service Unavailable
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": "service_unavailable",
10+
"message": "Sorry, the API service is temporarily unavailable"
11+
}
12+
]
13+
}

tests/Mock/Error/Error503ELB.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HTTP/1.1 503 Service Unavailable: Back-end server is at capacity
2+
Content-Length: 0
3+
Connection: keep-alive

0 commit comments

Comments
 (0)