Skip to content

Commit

Permalink
Better exception throwing and handling
Browse files Browse the repository at this point in the history
  • Loading branch information
luciferous committed Mar 16, 2011
1 parent 52399b6 commit 447067c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
39 changes: 33 additions & 6 deletions Services/Twilio.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,40 @@ public function createData($path, array $params = array())
private function _processResponse($response)
{
list($status, $headers, $body) = $response;
if (empty($headers['content-type'])) {
throw new DomainException('Response header is missing Content-Type');
}
switch ($headers['content-type']) {
case 'application/json':
return $this->_processJsonResponse($status, $headers, $body);
break;
case 'text/xml':
return $this->_processXmlResponse($status, $headers, $body);
break;
}
throw new DomainException('Response was neither JSON nor XML');
}

private function _processJsonResponse($status, $headers, $body) {
$decoded = json_decode($body);
if (200 <= $status && $status < 300) {
if ($headers['content-type'] == 'application/json') {
$object = json_decode($body);
return $object;
}
throw new ErrorException('not json');
return $decoded;
}
throw new ErrorException("$status: $body");
throw new Services_Twilio_RestException(
(int)$decoded->status,
$decoded->message,
isset($decoded->code) ? $decoded->code : null,
isset($decoded->more_info) ? $decoded->more_info : null
);
}

private function _processXmlResponse($status, $headers, $body) {
$decoded = simplexml_load_string($body);
throw new Services_Twilio_RestException(
(int)$decoded->Status,
(string)$decoded->Message,
(string)$decoded->Code,
(string)$decoded->MoreInfo
);
}
}
8 changes: 6 additions & 2 deletions Services/Twilio/AutoPagingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public function next()
$this->loadIfNecessary();
return next($this->items);
}
catch (ErrorException $e) { }
catch (Services_Twilio_RestException $e) {
// Swallow the out-of-range error
}
}

public function rewind()
Expand All @@ -56,7 +58,9 @@ public function valid()
$this->loadIfNecessary();
return key($this->items) !== null;
}
catch (ErrorException $e) { }
catch (Services_Twilio_RestException $e) {
// Swallow the out-of-range error
}
return false;
}

Expand Down
25 changes: 25 additions & 0 deletions Services/Twilio/RestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

class Services_Twilio_RestException
extends Exception
{
protected $status;
protected $info;

public function __construct($status, $message, $code = 0, $info = '')
{
$this->status = $status;
$this->info = $info;
parent::__construct($message, $code);
}

public function getStatus()
{
return $this->status;
}

public function getInfo()
{
return $this->info;
}
}
4 changes: 3 additions & 1 deletion tests/TwilioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ function testArrayAccessForListResources() {
));
$http->shouldReceive('get')->once()
->with('/2010-04-01/Accounts/AC123/Calls.json?Page=1&PageSize=10')
->andReturn(array(400, array('content-type' => 'application/json'), ''));
->andReturn(array(400, array('content-type' => 'application/json'),
'{"status":400,"message":"foo"}'
));
$client = new Services_Twilio('AC123', '123', '2010-04-01', $http);
foreach ($client->account->calls as $call) {
$this->assertEquals('CA123', $call->sid);
Expand Down

0 comments on commit 447067c

Please sign in to comment.