Skip to content

Commit 0fe2b7e

Browse files
committed
Handle title and error_title inconsistency in voice API
The requests can be handled by different services which have different error formats. This commit reads both error fields and provides a fallback if neither can be found
1 parent 745a8c2 commit 0fe2b7e

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

src/Call/Collection.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,25 @@ protected function getException(ResponseInterface $response)
144144
$body = json_decode($response->getBody()->getContents(), true);
145145
$status = $response->getStatusCode();
146146

147+
// Error responses aren't consistent. Some are generated within the
148+
// proxy and some are generated within voice itself. This handles
149+
// both cases
150+
151+
// This message isn't very useful, but we shouldn't ever see it
152+
$errorTitle = 'Unexpected error';
153+
154+
if (isset($body['title'])) {
155+
$errorTitle = $body['title'];
156+
}
157+
158+
if (isset($body['error_title'])) {
159+
$errorTitle = $body['error_title'];
160+
}
161+
147162
if($status >= 400 AND $status < 500) {
148-
$e = new Exception\Request($body['error_title'], $status);
163+
$e = new Exception\Request($errorTitle, $status);
149164
} elseif($status >= 500 AND $status < 600) {
150-
$e = new Exception\Server($body['error_title'], $status);
165+
$e = new Exception\Server($errorTitle, $status);
151166
} else {
152167
$e = new Exception\Exception('Unexpected HTTP Status Code');
153168
throw $e;

test/Call/CollectionTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Prophecy\Argument;
1717
use Psr\Http\Message\RequestInterface;
1818
use Zend\Diactoros\Response;
19+
use Nexmo\Client\Exception;
1920

2021
class CollectionTest extends \PHPUnit_Framework_TestCase
2122
{
@@ -132,6 +133,63 @@ public function testCreatePostCall($payload, $method)
132133
$this->assertInstanceOf('Nexmo\Call\Call', $call);
133134
$this->assertEquals('e46fd8bd-504d-4044-9600-26dd18b41111', $call->getId());
134135
}
136+
137+
/**
138+
* @dataProvider postCall
139+
*/
140+
public function testCreatePostCallErrorFromVApi($payload, $method)
141+
{
142+
$this->nexmoClient->send(Argument::that(function(RequestInterface $request) use ($payload){
143+
$this->assertRequestUrl('api.nexmo.com', '/v1/calls', 'POST', $request);
144+
$this->assertRequestBodyIsJson(json_encode($payload), $request);
145+
return true;
146+
}))->willReturn($this->getResponse('error_vapi', '400'));
147+
148+
try {
149+
$call = $this->collection->$method($payload);
150+
$this->fail('Expected to throw request exception');
151+
} catch (Exception\Request $e) {
152+
$this->assertEquals($e->getMessage(), 'Bad Request');
153+
}
154+
}
155+
156+
/**
157+
* @dataProvider postCall
158+
*/
159+
public function testCreatePostCallErrorFromProxy($payload, $method)
160+
{
161+
$this->nexmoClient->send(Argument::that(function(RequestInterface $request) use ($payload){
162+
$this->assertRequestUrl('api.nexmo.com', '/v1/calls', 'POST', $request);
163+
$this->assertRequestBodyIsJson(json_encode($payload), $request);
164+
return true;
165+
}))->willReturn($this->getResponse('error_proxy', '400'));
166+
167+
try {
168+
$call = $this->collection->$method($payload);
169+
$this->fail('Expected to throw request exception');
170+
} catch (Exception\Request $e) {
171+
$this->assertEquals($e->getMessage(), 'Unsupported Media Type');
172+
}
173+
}
174+
175+
/**
176+
* @dataProvider postCall
177+
*/
178+
public function testCreatePostCallErrorUnknownFormat($payload, $method)
179+
{
180+
$this->nexmoClient->send(Argument::that(function(RequestInterface $request) use ($payload){
181+
$this->assertRequestUrl('api.nexmo.com', '/v1/calls', 'POST', $request);
182+
$this->assertRequestBodyIsJson(json_encode($payload), $request);
183+
return true;
184+
}))->willReturn($this->getResponse('error_unknown_format', '400'));
185+
186+
try {
187+
$call = $this->collection->$method($payload);
188+
$this->fail('Expected to throw request exception');
189+
} catch (Exception\Request $e) {
190+
$this->assertEquals($e->getMessage(), "Unexpected error");
191+
}
192+
}
135193

136194
/**
137195
* @dataProvider putCall
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "UNSUPPORTED_MEDIA_TYPE",
3+
"error_title": "Unsupported Media Type"
4+
}
5+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"msg_error": "Unexpected error"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": 400,
3+
"title": "Bad Request",
4+
"invalid_parameters": [
5+
{ "reason": "can contain up to 15 digits prefixed with +", "name": "number" }
6+
]
7+
}

0 commit comments

Comments
 (0)