Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,26 @@ public function __construct($apiToken)
* @param string $result The API response
* @param int $httpCode The HTTP status code
*
* @throws Exception
* @throws ClickatellException
* @return array
*/
protected function handle($result, $httpCode)
{
// Check for non-OK statuses
$decoded = json_decode($result, true);

// Check for OK statuses
$codes = explode(",", static::ACCEPTED_CODES);
$ok_status = in_array($httpCode, $codes);

$is_error = isset($decoded['error']) || !$ok_status;
if ($is_error) {
// If no error key is present in the decoded JSON, return the entire body as an exception.
$error = isset($decoded['error']) ? $decoded['error'] : $result;

if (!in_array($httpCode, $codes)) {
// Decode JSON if possible, if this can't be decoded...something fatal went wrong
// and we will just return the entire body as an exception.
if ($error = json_decode($result, true)) {
$error = $error['error'];
} else {
$error = $result;
}

throw new \Clickatell\ClickatellException($error);
} else {
return json_decode($result, true);
throw new ClickatellException($error);
}

return $decoded;
}

/**
Expand Down