Skip to content

Commit

Permalink
Add clearer error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Jun 14, 2022
1 parent ef9be86 commit 9c2d34d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/mailosaur.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ class MailosaurClient {
httpError(response) {
const httpStatusCode = response.statusCode;
const httpResponseBody = response.body ? JSON.stringify(response.body) : null;
let message = '';

switch (httpStatusCode) {
case 400:
return new MailosaurError('Request had one or more invalid parameters.', 'invalid_request', httpStatusCode, httpResponseBody);
try {
const json = JSON.parse(httpResponseBody);
json.errors.forEach(err => {
message += `(${err.field}) ${err.detail[0].description}\r\n`;
});
} catch (e) {
message = 'Request had one or more invalid parameters.';
}
return new MailosaurError(message, 'invalid_request', httpStatusCode, httpResponseBody);
case 401:
return new MailosaurError('Authentication failed, check your API key.', 'authentication_error', httpStatusCode, httpResponseBody);
case 403:
return new MailosaurError('Insufficient permission to perform that task.', 'permission_error', httpStatusCode, httpResponseBody);
case 404:
return new MailosaurError('Request did not find any matching resources.', 'invalid_request', httpStatusCode, httpResponseBody);
return new MailosaurError('Not found, check input parameters.', 'invalid_request', httpStatusCode, httpResponseBody);
default:
return new MailosaurError('An API error occurred, see httpResponse for further information.', 'api_error', httpStatusCode, httpResponseBody);
}
Expand Down
28 changes: 28 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { assert } = require('chai');
const MailosaurClient = require('../lib/mailosaur');

describe('error handling', () => {
it('Unauthorized', (done) => {
const client = new MailosaurClient('invalid_key');
client.servers.list().catch((err) => {
assert.equal(err.toString(), 'Error: Authentication failed, check your API key.');
done();
});
});

it('Not Found', (done) => {
const client = new MailosaurClient(process.env.MAILOSAUR_API_KEY);
client.servers.get('not_found').catch((err) => {
assert.equal(err.toString(), 'Error: Not found, check input parameters.');
done();
});
});

it('Bad Request', (done) => {
const client = new MailosaurClient(process.env.MAILOSAUR_API_KEY);
client.servers.create({}).catch((err) => {
assert.equal(err.toString(), 'Error: (name) Please provide a name for your server\r\n');
done();
});
});
});
2 changes: 1 addition & 1 deletion test/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('servers', () => {
client.servers.create({})
.catch((err) => {
assert.instanceOf(err, MailosaurError);
assert.equal(err.message, 'Request had one or more invalid parameters.');
assert.equal(err.message, '(name) Please provide a name for your server\r\n');
assert.equal(err.errorType, 'invalid_request');
assert.equal(err.httpStatusCode, 400);
assert.isTrue(err.httpResponseBody.indexOf('{"type":') !== -1);
Expand Down

0 comments on commit 9c2d34d

Please sign in to comment.