Make a clear distinction between programmer errors ("internal errors") and runtime errors, and allow configuring the server to let the programmer errors bubble up in development and unit testing. This saves a huge amount of time because it generates ordinary stack traces when things go wrong. And, if these errors occur in production, we'll catch them, and display **shields | internal error** which is the equivalent of a 500 error.
96 lines
3.2 KiB
JavaScript
96 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
const chai = require('chai');
|
|
const { assert, expect } = chai;
|
|
const { checkErrorResponse } = require('./error-helper');
|
|
const { NotFound, InvalidResponse } = require('../services/errors');
|
|
|
|
chai.use(require('chai-as-promised'));
|
|
|
|
describe('Standard Error Handler', function() {
|
|
it('makes inaccessible badge', function() {
|
|
const badgeData = {'text': []};
|
|
assert.equal(true, checkErrorResponse(badgeData, 'something other than null', {}));
|
|
assert.equal('inaccessible', badgeData.text[1]);
|
|
assert.equal('red', badgeData.colorscheme);
|
|
});
|
|
|
|
it('makes not found badge', function() {
|
|
const badgeData = {'text': []};
|
|
assert.equal(true, checkErrorResponse(badgeData, null, {statusCode: 404}));
|
|
assert.equal('not found', badgeData.text[1]);
|
|
assert.equal('lightgrey', badgeData.colorscheme);
|
|
});
|
|
|
|
it('makes not found badge with custom error', function() {
|
|
const badgeData = {'text': []};
|
|
assert.equal(true, checkErrorResponse(badgeData, null, {statusCode: 404}, 'custom message'));
|
|
assert.equal('custom message', badgeData.text[1]);
|
|
assert.equal('lightgrey', badgeData.colorscheme);
|
|
});
|
|
|
|
it('makes invalid badge', function() {
|
|
const badgeData = {'text': []};
|
|
assert.equal(true, checkErrorResponse(badgeData, null, {statusCode: 500}));
|
|
assert.equal('invalid', badgeData.text[1]);
|
|
assert.equal('lightgrey', badgeData.colorscheme);
|
|
});
|
|
|
|
it('return false on 200 status', function() {
|
|
assert.equal(false, checkErrorResponse({'text': []}, null, {statusCode: 200}));
|
|
});
|
|
});
|
|
|
|
describe('async error handler', function() {
|
|
context('when status is 200', function() {
|
|
it('passes through the inputs', async function() {
|
|
const args = { buffer: 'buffer', res: { statusCode: 200 } };
|
|
expect(await checkErrorResponse.asPromise()(args))
|
|
.to.deep.equal(args);
|
|
});
|
|
});
|
|
|
|
context('when status is 404', function() {
|
|
const res = { statusCode: 404 };
|
|
|
|
it('throws NotFound', async function() {
|
|
try {
|
|
await checkErrorResponse.asPromise()({ res });
|
|
expect.fail('Expected to throw');
|
|
} catch (e) {
|
|
expect(e).to.be.an.instanceof(NotFound);
|
|
expect(e.message).to.equal('Not Found');
|
|
expect(e.prettyMessage).to.equal('not found');
|
|
}
|
|
});
|
|
|
|
it('displays the custom not found message', async function() {
|
|
const notFoundMessage = 'no goblins found';
|
|
const res = { statusCode: 404 };
|
|
try {
|
|
await checkErrorResponse.asPromise({ notFoundMessage })({ res });
|
|
expect.fail('Expected to throw');
|
|
} catch (e) {
|
|
expect(e).to.be.an.instanceof(NotFound);
|
|
expect(e.message).to.equal('Not Found: no goblins found');
|
|
expect(e.prettyMessage).to.equal('no goblins found');
|
|
}
|
|
});
|
|
});
|
|
|
|
context('when status is 500', function() {
|
|
const res = { statusCode: 500 };
|
|
|
|
it('throws InvalidResponse', async function() {
|
|
try {
|
|
await checkErrorResponse.asPromise()({ res });
|
|
expect.fail('Expected to throw');
|
|
} catch (e) {
|
|
expect(e).to.be.an.instanceof(InvalidResponse);
|
|
expect(e.message).to.equal('Invalid Response: Got status code 500 (expected 200)');
|
|
expect(e.prettyMessage).to.equal('invalid');
|
|
}
|
|
});
|
|
});
|
|
});
|