1.0.0 • Published 6 years ago

ns-error v1.0.0

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

NS Error

Module for ServerError, error names, status codes and helper functions.

    const {ERRORS, STATUS, sanitise, ServerError} = require('ns-error')
    
    describe('ServerError', () => {

        it('should construct minimal server error', () => {
            const se = new ServerError(ERRORS.SERVER, STATUS._500_InternalServerError)
            expect(se.name).toBe(ERRORS.SERVER)
            expect(se.status).toBe(STATUS._500_InternalServerError)
            expect(se.message).toBe('')
            expect(se.cause).toBeNull()
            expect(se.details).toBeNull()
            expect(sanitise(se)).toEqual({'error': {'name': 'ServerError'}})
        })

        it('should construct maximal server error', () => {
            const cause = new Error('Catastrophe')
            const message = 'A catastrophe error occurred!'
            const details = 'I just pressed this red button and ... poof'
            const options = {message, details, cause}
            const se = new ServerError(ERRORS.SERVER, STATUS._500_InternalServerError, options)
            expect(se.name).toBe(ERRORS.SERVER)
            expect(se.status).toBe(STATUS._500_InternalServerError)
            expect(se.message).toBe('A catastrophe error occurred!: Catastrophe')
            expect(se.cause).toBe(cause)
            expect(se.details).toBe(details)
            expect(sanitise(se)).toEqual({
                error: {
                    name: ERRORS.SERVER,
                    message: message + ': ' + cause.message,
                    details
                }
            })
            expect(sanitise(se)).toEqual({
                error: {
                    name: 'ServerError',
                    message: 'A catastrophe error occurred!: Catastrophe',
                    details: 'I just pressed this red button and ... poof'
                }
            })
        })
    })