Skip to content

Commit

Permalink
Use log-process-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 14, 2019
1 parent e480909 commit 60f24cb
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 54 deletions.
2 changes: 1 addition & 1 deletion nodemon.debug.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"exec": "cd ./examples && NODE_ENV=dev node --inspect-brk --no-warnings --stack-trace-limit=20 index.js",
"exec": "cd ./examples && NODE_ENV=dev node --inspect-brk --stack-trace-limit=20 index.js",
"ext": ".js,.json,.yml,.yaml"
}
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"exec": "cd ./examples && NODE_ENV=dev node --inspect --no-warnings --stack-trace-limit=20 index.js",
"exec": "cd ./examples && NODE_ENV=dev node --inspect --stack-trace-limit=20 index.js",
"ext": ".js,.json,.yml,.yaml"
}
79 changes: 76 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"js-yaml": "^3.12.1",
"json5": "^2.1.0",
"li": "^1.3.0",
"log-process-errors": "^2.0.0",
"moize": "^5.4.1",
"mongodb": "^2.2.36",
"mustache": "^2.3.2",
Expand Down
5 changes: 5 additions & 0 deletions src/run/exit/graceful_exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ const { emitStopEvent } = require('./stop_event')
const mmGracefulExit = async function({
protocolAdapters,
dbAdapters,
stopProcessErrors,
config,
}) {
const measures = []
const { exit } = await mGracefulExit({
protocolAdapters,
dbAdapters,
stopProcessErrors,
config,
measures,
})
Expand Down Expand Up @@ -46,9 +48,12 @@ const eGracefulExit = addErrorHandler(oGracefulExit, gracefulExitHandler)
const gracefulExit = async function({
protocolAdapters,
dbAdapters,
stopProcessErrors,
config,
measures,
}) {
stopProcessErrors()

const protocolPromises = closeProtocols({
protocolAdapters,
config,
Expand Down
8 changes: 7 additions & 1 deletion src/run/exit/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ const { gracefulExit } = require('./graceful_exit')
// Make sure the server stops when graceful exits are possible
// Also send related events
// We cannot handle `process.exit()` since graceful exit is async
const setupGracefulExit = function({ protocolAdapters, dbAdapters, config }) {
const setupGracefulExit = function({
protocolAdapters,
dbAdapters,
stopProcessErrors,
config,
}) {
const exitFunc = gracefulExit.bind(null, {
protocolAdapters,
dbAdapters,
stopProcessErrors,
config,
})

Expand Down
65 changes: 17 additions & 48 deletions src/run/process.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const process = require('process')
const { inspect } = require('util')
const logProcessErrors = require('log-process-errors')

const { logEvent } = require('../log')
const { createPb } = require('../errors')
Expand All @@ -12,60 +11,30 @@ const { createPb } = require('../errors')
// Note that process events fired that do not belong to autoserver might be
// caught as well.
const processErrorHandler = function({ config }) {
process.on('unhandledRejection', unhandledHandler.bind(null, config))
process.on('multipleResolves', multipleResolvesHandler.bind(null, config))
process.on('rejectionHandled', rejectionHandledHandler.bind(null, config))
process.on('warning', warningHandler.bind(null, config))
}

const unhandledHandler = async function(config, value) {
const { suffix, innererror } = getPromiseValue(value)
const message = `A promise was rejected${suffix} and not handled right away`

await emitProcessEvent({ message, innererror, config })
}

// eslint-disable-next-line max-params
const multipleResolvesHandler = async function(config, type, promise, value) {
const { suffix, innererror } = getPromiseValue(value)
const message = `A promise was ${type}d${suffix} after being already settled`

await emitProcessEvent({ message, innererror, config })
}

const getPromiseValue = function(value) {
if (value instanceof Error) {
return { suffix: '', innererror: value }
}

return { suffix: ` with value '${value}'` }
}

const rejectionHandledHandler = async function(config, promise) {
const message = `A promise was rejected but handled too late: ${inspect(
promise,
)}`

await emitProcessEvent({ message, config })
const stopProcessErrors = logProcessErrors({
...LOG_PROCESS_ERRORS_OPTS,
log: emitProcessEvent.bind(null, { config }),
})
return { stopProcessErrors }
}

const warningHandler = async function(config, error) {
const { message, code, detail } = error
const nextLine = [code, detail]
.filter(string => string !== undefined)
.join(':')
const messageA = `${message}\n${nextLine}`

await emitProcessEvent({ message: messageA, innererror: error, config })
const LOG_PROCESS_ERRORS_OPTS = {
colors: false,
exitOn: [],
// See https://github.com/nodejs/node/issues/24321
// We could log it as a `message` instead but it would lack the stack trace,
// making it less useful.
level: { multipleResolves: 'silent' },
}

// Report process problems as events with event 'failure'
const emitProcessEvent = async function({ message, innererror, config }) {
const error = createPb(message, { reason: 'ENGINE', innererror })
const emitProcessEvent = function({ config }, message, level) {
const error = createPb(message, { reason: 'ENGINE' })

await logEvent({
return logEvent({
event: 'failure',
phase: 'process',
level,
params: { error },
config,
})
Expand Down

0 comments on commit 60f24cb

Please sign in to comment.