Skip to content

Commit

Permalink
bin/unit.mjs: make sure process.exit(1) is called on uncaught errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeh committed Jan 13, 2020
1 parent c0aecdc commit 6aec98b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/bin/unit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from '@magic/fs'
import util from 'util'

import log from '@magic/log'
import is from '@magic/types'

import run from '../run.mjs'

Expand Down Expand Up @@ -87,10 +88,18 @@ const init = async () => {

init()

process
.on('unhandledRejection', error => {
throw error
})
.on('uncaughtException', error => {
throw error
})
const handleError = error => {
if (is.string(error)) {
error = new Error(error)
}

log.error(error.name, error.message)
const stack = error.stack
.replace(error.name, '')
.replace(error.message, '')

log.warn('stacktrace', stack)
process.exit(1)
}

process.on('unhandledRejection', handleError).on('uncaughtException', handleError)

0 comments on commit 6aec98b

Please sign in to comment.