Skip to content

Commit

Permalink
Annotate string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Jun 14, 2022
1 parent 60b3ce8 commit e86be9c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/Operations/AOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,31 @@ 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);
}
}

return $response;
}
}
}
42 changes: 42 additions & 0 deletions tests/ErrorsTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Mailosaur_Test;


use Mailosaur\MailosaurClient;
use Mailosaur\Models\ServerCreateOptions;
use Mailosaur\Models\MailosaurException;

class ErrorsTests extends \PHPUnit\Framework\TestCase
{
public function testUnauthorized()
{
try {
$client = new MailosaurClient('invalid_key');
$client->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());
}
}
}
4 changes: 2 additions & 2 deletions tests/ServersTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}

0 comments on commit e86be9c

Please sign in to comment.