All files / src/modules/implementations/core public-errors.js

100% Statements 28/28
100% Branches 14/14
100% Functions 4/4
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2971x 71x 811x 811x 811x 811x 811x 811x 811x 811x 4566x 4566x 4566x 4566x 811x 4438x 4438x 4438x 811x 214x 214x 2x 214x 2x 2x 214x 811x 71x  
'use strict';
module.exports = () => {
    const errorCodes = Object.freeze({
        SYNTAX: 'LEDLIGHT_SYNTAX',
        INVALID_API_INPUT: 'LEDLIGHT_INVALID_API_INPUT',
        PROJECT_CONFIGURATION: 'LEDLIGHT_PROJECT_CONFIGURATION',
        MISSING_VALUATION_DATA: 'LEDLIGHT_MISSING_VALUATION_DATA',
        DATABASE: 'LEDLIGHT_DATABASE',
    });
    function withCode(error, code) {
        if (error.code === undefined)
            error.code = code;
        return error;
    }
    function createError(code, message, ErrorType) {
        const Constructor = ErrorType ?? Error;
        return withCode(new Constructor(message), code);
    }
    function databaseError(error) {
        if (error.code && !String(error.code).startsWith('SQLITE_'))
            return error;
        if (String(error.code).startsWith('SQLITE_'))
            error.sqliteCode = error.code;
        error.code = errorCodes.DATABASE;
        return error;
    }
    return { createError, databaseError, errorCodes, withCode };
};