diff --git a/README.md b/README.md index 480e151..edb059d 100644 --- a/README.md +++ b/README.md @@ -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 { @@ -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` | diff --git a/lib/http-error.js b/lib/http-error.js index 5f738b8..c990865 100644 --- a/lib/http-error.js +++ b/lib/http-error.js @@ -4,7 +4,8 @@ const { TestingBotError, AuthenticationError, RateLimitError, - ValidationError + ValidationError, + NotFoundError } = require('./errors'); function extractMessage (body, status) { @@ -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; diff --git a/test/errors.test.js b/test/errors.test.js index 711bfa3..89e2a41 100644 --- a/test/errors.test.js +++ b/test/errors.test.js @@ -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);