diff --git a/src/MessageBird/Common/ResponseError.php b/src/MessageBird/Common/ResponseError.php index 687c78b0..ee8d9e19 100644 --- a/src/MessageBird/Common/ResponseError.php +++ b/src/MessageBird/Common/ResponseError.php @@ -11,6 +11,7 @@ */ class ResponseError { + const EXCEPTION_MESSAGE = 'Got error response from the server: %s'; const SUCCESS = 1; @@ -25,7 +26,7 @@ class ResponseError const CHAT_API_AUTH_ERROR = 1001; - public $errors = array (); + public $errors = array(); /** * Load the error data into an array. @@ -33,26 +34,26 @@ class ResponseError * * @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; @@ -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); } } diff --git a/tests/integration/ResponseErrorTest.php b/tests/integration/ResponseErrorTest.php new file mode 100644 index 00000000..55720d10 --- /dev/null +++ b/tests/integration/ResponseErrorTest.php @@ -0,0 +1,41 @@ +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'); + } +}