dsi-icl/optimise

View on GitHub
packages/optimise-remote-control/src/utils/error_helper.js

Summary

Maintainability
D
1 day
Test Coverage
/**
 *
 * @param {*} error_obj Personalised error message
 * @param {*} error_stack Raised error
 */
function ErrorStack(error_obj, error_stack) {
    const error = {};
    let error_message = '';

    error.toString = () => JSON.stringify(this);

    // Extract current error message
    if (typeof error_obj === 'string')
        error_message = error_obj;
    else if (typeof error_obj === 'object' && error_obj.hasOwnProperty('message'))
        error_message = error_obj.message;
    else if (typeof error_obj === 'object' && error_obj.hasOwnProperty('error'))
        error_message = error_obj.error;
    else if (error_obj === undefined || error_obj === null)
        error_message = 'Undefined';
    else
        error_message = error_obj.toString();
    error.error = error_message;

    //Extract error stack
    if (error_stack === undefined && error_obj.hasOwnProperty('stack'))
        error_stack = error_obj.stack;
    if (error_stack !== undefined) {
        if (error_stack instanceof Error)
            error.stack = { error: error_stack.message };
        else
            error.stack = error_stack;
    }

    return error;
}

export default ErrorStack;