Skip to content
Merged
Show file tree
Hide file tree
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
44 changes: 29 additions & 15 deletions src/MessageBird/Common/ResponseError.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
class ResponseError
{
const EXCEPTION_MESSAGE = 'Got error response from the server: %s';

const SUCCESS = 1;

Expand All @@ -25,34 +26,34 @@ class ResponseError

const CHAT_API_AUTH_ERROR = 1001;

public $errors = array ();
public $errors = array();

/**
* Load the error data into an array.
* Throw an exception when important errors are found.
*
* @param $body
*
* @throws Exceptions\BalanceException
* @throws Exceptions\AuthenticateException
* @throws Exceptions\BalanceException
*/
public function __construct($body)
{
if (!empty($body->errors)) {
foreach ($body->errors AS $error) {
// Voice API returns errors with a "message" field instead of "description".
// This ensures all errors have a description set.
if (!empty($error->message)) {
$error->description = $error->message;
unset($error->message);
}

if ($error->code === self::NOT_ENOUGH_CREDIT) {
throw new Exceptions\BalanceException;
throw new Exceptions\BalanceException($this->getExceptionMessage($error));
} elseif ($error->code === self::REQUEST_NOT_ALLOWED) {
throw new Exceptions\AuthenticateException;
throw new Exceptions\AuthenticateException($this->getExceptionMessage($error));
} elseif ($error->code === self::CHAT_API_AUTH_ERROR) {
throw new Exceptions\AuthenticateException;
}

// Rewrite error for Voice API.
if (!empty($error->message)) {
$error->description = $error->message;
unset($error->message);
throw new Exceptions\AuthenticateException($this->getExceptionMessage($error));
}

$this->errors[] = $error;
Expand All @@ -61,17 +62,30 @@ public function __construct($body)
}

/**
* Get the error string to show in the Exception message.
* Get the exception message for the provided error.
*
* @param $error
*
* @return string
*/
private function getExceptionMessage($error)
{
return sprintf(self::EXCEPTION_MESSAGE, $error->description);
}

/**
* Get a string of all of this response's concatenated error descriptions.
*
* @return string
*/
public function getErrorString()
{
$errorString = array ();
$errorDescriptions = array();

foreach ($this->errors AS $error) {
$errorString[] = $error->description;
$errorDescriptions[] = $error->description;
}

return implode(', ', $errorString);
return implode(', ', $errorDescriptions);
}
}
41 changes: 41 additions & 0 deletions tests/integration/ResponseErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use MessageBird\Common\ResponseError;
use MessageBird\Exceptions\MessageBirdException;
use MessageBird\Objects\Base;

class ResponseErrorTest extends BaseTest
{
const EXCEPTION_MESSAGE = 'Got error response from the server: %s';

const SINGLE_ERROR_JSON = '{"errors":[{"code":25,"description":"foo"}]}';
const MULTIPLE_ERRORS_JSON = '{"errors":[{"code":9,"description":"foo"},{"code":25,"description":"bar"}]}';

public function testSingleError()
{
$this->assertEquals(
sprintf(self::EXCEPTION_MESSAGE, 'foo'),
$this->getExceptionMessageFromJson(self::SINGLE_ERROR_JSON)
);
}

public function testMultipleErrors()
{
$this->assertEquals(
sprintf(self::EXCEPTION_MESSAGE, 'bar'),
$this->getExceptionMessageFromJson(self::MULTIPLE_ERRORS_JSON)
);
}

private function getExceptionMessageFromJson($json)
{
try {
new ResponseError(json_decode($json));
} catch (MessageBirdException $e) {
// Expected: we want the error message.
return $e->getMessage();
}

$this->fail('No exception thrown');
}
}