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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ try {
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Bad credentials');
} else if (error instanceof NotFoundError) {
console.error('No such test');
} else if (error instanceof RateLimitError) {
console.error('Slow down, retry after', error.retryAfter);
} else {
Expand All @@ -793,6 +795,7 @@ try {
|--------|-------------|
| 400 | `ValidationError` (has `.fields`) |
| 401 | `AuthenticationError` |
| 404 | `NotFoundError` |
| 429 | `RateLimitError` (has `.retryAfter`) |
| other non-2xx | `TestingBotError` |
| no response (transport failure) | `NetworkError` |
Expand Down
7 changes: 6 additions & 1 deletion lib/http-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const {
TestingBotError,
AuthenticationError,
RateLimitError,
ValidationError
ValidationError,
NotFoundError
} = require('./errors');

function extractMessage (body, status) {
Expand All @@ -29,6 +30,10 @@ function buildHttpError (status, body, headers) {
case 401:
error = new AuthenticationError(message);
break;
case 404:
error = new NotFoundError('Resource');
error.message = message;
break;
case 429:
error = new RateLimitError(message, headers && headers['retry-after']);
break;
Expand Down
10 changes: 10 additions & 0 deletions test/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ describe('Typed errors', function () {
assert.strictEqual(e.retryAfter, '30');
});

it('maps 404 to NotFoundError, preserving message + body', function () {
const body = { error: '404 Not Found. Could not find test x' };
const e = buildHttpError(404, body);
assert.ok(e instanceof NotFoundError);
assert.ok(e instanceof TestingBotError);
assert.strictEqual(e.statusCode, 404);
assert.strictEqual(e.message, body.error);
assert.strictEqual(e.response, body);
});

it('maps other statuses to TestingBotError, keeping status + body', function () {
const body = { error: 'boom' };
const e = buildHttpError(500, body);
Expand Down
Loading