UNPKG

sharp-error-handler

Version:

an error handler that integrates with Hapi/boom to manage all server errors through JSON file

116 lines (101 loc) 4.97 kB
"use strict"; const errorsData = require('./errorsData'); const ErrorHandler = require('../index.js'); const { expect } = require('chai'); const _ = require('lodash'); describe('ErrorHandler', () => { describe('createServerErrors', () => { it('should create ServerErrors', () => { const ServerErrors = ErrorHandler.createServerErrors(errorsData); _.forEach(ServerErrors, (errorClass, errName) => { expect(_.has(errorsData, _.lowerFirst(errName))).to.be.true; expect(_.upperFirst(errName)).to.deep.equal(errName); expect(new errorClass()).to.be.an.instanceOf(Error); }); }); }); describe('wrapErrors', () => { it('should succeviely wrapErrors with msgs and statusCodes', () => { const ServerErrors = ErrorHandler.createServerErrors(errorsData); _.forEach(ServerErrors, (errorClass, errorName) => { const customError = new errorClass(); const wrappedError = ErrorHandler.wrapError(null, customError); expect(wrappedError.output.payload.message).to.deep.equal(errorsData[_.lowerFirst(errorName)].message); expect(wrappedError.output.statusCode).to.deep.equal(errorsData[_.lowerFirst(errorName)].statusCode); expect(_.keys(wrappedError)).to.include('isCreatedServerError'); }); }); it('should succeviely wrapErrors with no statusCodes & return 500', () => { const iErrors = { "errorWithNoStatus": { "message": "go" } }; const ServerErrors = ErrorHandler.createServerErrors(iErrors); const customError = new ServerErrors.ErrorWithNoStatus(); const wrappedError = ErrorHandler.wrapError(null, customError); expect(wrappedError.output.statusCode).to.equal(500); expect(wrappedError.output.payload.message).to.equal("An internal server error occurred"); expect(_.keys(wrappedError)).to.include('isCreatedServerError'); }); it('should succeviely wrapErrors with no messages & return custom message depending on statusCode', () => { const iErrors = { "errorWithNoMessage": { "statusCode": 400 } }; const ServerErrors = ErrorHandler.createServerErrors(iErrors); const customError = new ServerErrors.ErrorWithNoMessage(); const wrappedError = ErrorHandler.wrapError(null, customError); expect(wrappedError.output.statusCode).to.equal(400); expect(wrappedError.output.payload.message).to.equal("Bad Request"); expect(_.keys(wrappedError)).to.include('isCreatedServerError'); }); it('should test that the data is added to the string message', () => { const id = "7568654467"; const ServerErrors = ErrorHandler.createServerErrors(errorsData); const customError = new ServerErrors.InvalidId(id); const wrappedError = ErrorHandler.wrapError(null, customError); expect(wrappedError.output.statusCode).to.equal(400); expect(wrappedError.output.payload.message).to.include(id); expect(_.keys(wrappedError)).to.include('isCreatedServerError'); }); it('should test that it wraps unhandled errors sucessively', () => { const wrappedError = ErrorHandler.wrapError(null, new Error()); expect(wrappedError.output.statusCode).to.equal(500); expect(wrappedError.output.payload.message).to.equal("An internal server error occurred"); expect(_.keys(wrappedError)).to.not.include('isCreatedServerError'); }); }); describe('wrapErrors with extrafields', () => { it('should return all fields written in the extra fields attribute', () => { const errorsWithExtraFields = _.mapValues(_.cloneDeep(errorsData), (value) => { value.extraFields = { 'ERROR_CODE': 1 } return value; }); const ServerErrors = ErrorHandler.createServerErrors(errorsWithExtraFields); _.forEach(ServerErrors, (errorClass, errorName) => { const customError = new errorClass(); const wrappedError = ErrorHandler.wrapError(null, customError); expect(wrappedError.output.payload.message).to.deep.equal(errorsData[_.lowerFirst(errorName)].message); expect(wrappedError.output.statusCode).to.deep.equal(errorsData[_.lowerFirst(errorName)].statusCode); expect(wrappedError.output.payload.ERROR_CODE).to.equal(1); expect(_.keys(wrappedError)).to.include('isCreatedServerError'); }); }); it('should fail if the extraFields attribute is not an object', () => { const errorsWithExtraFields = _.mapValues(_.cloneDeep(errorsData), (value) => { value.extraFields = "hello"; return value; }); const ServerErrors = ErrorHandler.createServerErrors(errorsWithExtraFields); _.forEach(ServerErrors, (errorClass, errorName) => { const customError = new errorClass(); const fn = () => ErrorHandler.wrapError(null, customError); expect(fn).to.throw(Error); }); }); }) });