tests for handleESError util funtion

This commit is contained in:
Matthew Bargar 2015-11-24 18:47:29 -05:00
parent bd3d96dc0b
commit 45a3f98659
2 changed files with 45 additions and 0 deletions

View file

@ -0,0 +1,41 @@
var expect = require('expect.js');
var Boom = require('boom');
var esErrors = require('elasticsearch').errors;
var handleESError = require('../handle_es_error');
describe('handleESError', function () {
it('should transform elasticsearch errors into boom errors with the same status code', function () {
var conflict = handleESError(new esErrors.Conflict());
expect(conflict.isBoom).to.be(true);
expect(conflict.output.statusCode).to.be(409);
var forbidden = handleESError(new esErrors[403]);
expect(forbidden.isBoom).to.be(true);
expect(forbidden.output.statusCode).to.be(403);
var notFound = handleESError(new esErrors.NotFound());
expect(notFound.isBoom).to.be(true);
expect(notFound.output.statusCode).to.be(404);
var badRequest = handleESError(new esErrors.BadRequest());
expect(badRequest.isBoom).to.be(true);
expect(badRequest.output.statusCode).to.be(400);
});
it('should return an unknown error without transforming it', function () {
var unknown = new Error('mystery error');
expect(handleESError(unknown)).to.be(unknown);
});
it('should return a boom 503 server timeout error for ES connection errors', function () {
expect(handleESError(new esErrors.ConnectionFault()).output.statusCode).to.be(503);
expect(handleESError(new esErrors.ServiceUnavailable()).output.statusCode).to.be(503);
expect(handleESError(new esErrors.NoConnections()).output.statusCode).to.be(503);
expect(handleESError(new esErrors.RequestTimeout()).output.statusCode).to.be(503);
});
it('should throw an error if called with a non-error argument', function () {
expect(handleESError).withArgs('notAnError').to.throwException();
});
});

View file

@ -3,6 +3,10 @@ const esErrors = require('elasticsearch').errors;
const _ = require('lodash');
module.exports = function handleESError(error) {
if (!(error instanceof Error)) {
throw new Error('Expected an instance of Error');
}
if (error instanceof esErrors.ConnectionFault ||
error instanceof esErrors.ServiceUnavailable ||
error instanceof esErrors.NoConnections ||