@dannybster/coco-the-bear-http-errors
Version:
A collection of error objects that represent http response objects for CoCo The Bear.
53 lines (47 loc) • 1.63 kB
JavaScript
const errorHandler = require('../src/error-handler');
const module401 = require('../src/401-unauthorized-error');
const module404 = require('../src/404-not-found-error');
const module503 = require('../src/503-service-unavailable-error');
const testUtils = require('@dannybster/coco-the-bear-test-utils');
const errorModules = [
{
name: 'UNAUTHORIZED',
status: 401,
module: module401,
},
{
name: 'NOT FOUND',
status: 404,
module: module404,
},
{
name: 'SERVICE UNAVAILABLE',
status: 503,
module: module503,
},
];
describe('error-handler.js', function () {
describe('If the error handler is called with a non http error', function () {
it('It should call next with the error.', function testNonHTTPErrorCallsNextOnly(done) {
const error = new Error('A non HTTP error.');
errorHandler(error, null, null, testUtils.assertCalledOnce((err) => {
err.should.eql(error);
done();
}));
});
});
errorModules.forEach((errorModule) => {
describe(`If the error handler is called with a HTTP ${errorModule.name} error`, function () {
it(`it should respond with an HTTP ${errorModule.status} and the specified response body.`, function testHandle404() {
const responseBody = { error: errorModule.status };
const error = errorModule.module.createError('An error', {}, responseBody);
const res = testUtils.createMockResponse();
errorHandler(error, null, res, null);
res.parameters.should.eql({
status: errorModule.status,
json: responseBody,
});
});
});
});
});