Skip to content

Commit 27f07ec

Browse files
committed
fix and test for elb error
1 parent 3a11784 commit 27f07ec

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,18 @@ public function test503Exception()
3333
$this->assertEquals('Sorry, the API service is temporarily unavailable', $errors[0]['message'] );
3434
}
3535
}
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+
}
3650
}

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)