all files / src/components/ errorer.js

100% Statements 27/27
100% Branches 13/13
100% Functions 0/0
100% Lines 25/25
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 5412×                       13×                                    
import createDebugger from 'debug'
import { gottaCatchEmAll } from 'gotta-catch-em-all'
import {
    TOGGLE_WATCHING_REJECTIONS_FLAG,
    TOGGLE_PREPARE_TO_EXIT_FLAG
} from '../constants/actionTypes.js'
 
const debug = createDebugger('unistack:errorer')
 
const nodeProcess = process
const log = console
 
const Errorer = ({ dispatch, getState }) => {
    const state = getState()
    const watchingRejections = getStateLeaf(state, 'WATCHING_REJECTIONS_FLAG')
    if (!watchingRejections) return dispatch(watchRejections())
 
    const fatalError = getStateLeaf(state, 'FATAL_ERROR_FLAG')
    const errorInstance = getStateLeaf(state, 'ERROR_INSTANCE')
    const prepareToExit = getStateLeaf(state, 'PREPARE_TO_EXIT_FLAG')
    if (!prepareToExit && errorInstance && fatalError) {
        return dispatch({ type: TOGGLE_PREPARE_TO_EXIT_FLAG })
    }
 
    if (prepareToExit) exitProcess(errorInstance)
}
 
const getStateLeaf = (state, leaf) => {
    switch(leaf) {
        case 'ERROR_INSTANCE':
            return state.error.instance
        case 'FATAL_ERROR_FLAG':
            return state.error.fatal
        case 'WATCHING_REJECTIONS_FLAG':
            return state.error.watchingRejections
        case 'PREPARE_TO_EXIT_FLAG':
            return state.env.prepareToExit
    }
}
 
const watchRejections = () => {
    debug('Attempting to watch rejections...')
    gottaCatchEmAll()
    debug('Watching rejections...')
    return { type: TOGGLE_WATCHING_REJECTIONS_FLAG }
}
 
const exitProcess = (errorInstance) => {
    log.error(errorInstance.stack)
    nodeProcess.exit(1)
}
 
export default Errorer