diff --git a/src/Operations/AOperation.php b/src/Operations/AOperation.php index f82d264..5f8e60c 100644 --- a/src/Operations/AOperation.php +++ b/src/Operations/AOperation.php @@ -43,17 +43,26 @@ protected function request($path, array $options = array()) $response = curl_exec($curl); $requestState = curl_getinfo($curl); + $message = ''; if ($requestState['http_code'] != 200 && $requestState['http_code'] != 204) { switch ($requestState['http_code']) { case 400: - throw new MailosaurException('Request had one or more invalid parameters.', 'invalid_request', $requestState['http_code'], $response); + try { + $json = json_decode($response); + foreach ($json->{'errors'} as &$err) { + $message .= '(' . $err->field . ') ' . $err->detail[0]->description . '\r\n'; + } + } catch (Exception $ex) { + $message = 'Request had one or more invalid parameters.'; + } + throw new MailosaurException($message, 'invalid_request', $requestState['http_code'], $response); case 401: throw new MailosaurException('Authentication failed, check your API key.', 'authentication_error', $requestState['http_code'], $response); case 403: throw new MailosaurException('Insufficient permission to perform that task.', 'permission_error', $requestState['http_code'], $response); case 404: - throw new MailosaurException('Request did not find any matching resources.', 'invalid_request', $requestState['http_code'], $response); + throw new MailosaurException('Not found, check input parameters.', 'invalid_request', $requestState['http_code'], $response); default: throw new MailosaurException('An API error occurred, see httpResponse for further information.', 'api_error', $requestState['http_code'], $response); } @@ -61,4 +70,4 @@ protected function request($path, array $options = array()) return $response; } -} \ No newline at end of file +} diff --git a/tests/ErrorsTests.php b/tests/ErrorsTests.php new file mode 100644 index 0000000..51ccf02 --- /dev/null +++ b/tests/ErrorsTests.php @@ -0,0 +1,42 @@ +servers->all(); + } catch(MailosaurException $e) { + $this->assertEquals('Authentication failed, check your API key.', $e->getMessage()); + } + } + + public function testNotFound() + { + try { + $client = new MailosaurClient(getenv('MAILOSAUR_API_KEY')); + $client->servers->get('not_found'); + } catch(MailosaurException $e) { + $this->assertEquals('Not found, check input parameters.', $e->getMessage()); + } + } + + public function testBadRequest() + { + try { + $client = new MailosaurClient(getenv('MAILOSAUR_API_KEY')); + $options = new ServerCreateOptions(); + $client->servers->create($options); + } catch(MailosaurException $e) { + $this->assertEquals('(name) Please provide a name for your server\r\n', $e->getMessage()); + } + } +} diff --git a/tests/ServersTests.php b/tests/ServersTests.php index 74a2c02..35a557f 100644 --- a/tests/ServersTests.php +++ b/tests/ServersTests.php @@ -85,10 +85,10 @@ public function testFailedCreate() $options = new ServerCreateOptions(); self::$client->servers->create($options); } catch(MailosaurException $e) { - $this->assertEquals('Request had one or more invalid parameters.', $e->getMessage()); + $this->assertEquals('(name) Please provide a name for your server\r\n', $e->getMessage()); $this->assertEquals('invalid_request', $e->errorType); $this->assertEquals(400, $e->httpStatusCode); $this->assertStringContainsString('{"type":', $e->httpResponseBody); } } -} \ No newline at end of file +}