From 60f24cb3af4347676192db139400fac8b10bd27a Mon Sep 17 00:00:00 2001 From: ehmicky Date: Thu, 14 Feb 2019 10:00:00 +0100 Subject: [PATCH] Use log-process-errors --- nodemon.debug.json | 2 +- nodemon.json | 2 +- package-lock.json | 79 +++++++++++++++++++++++++++++++++-- package.json | 1 + src/run/exit/graceful_exit.js | 5 +++ src/run/exit/setup.js | 8 +++- src/run/process.js | 65 ++++++++-------------------- 7 files changed, 108 insertions(+), 54 deletions(-) diff --git a/nodemon.debug.json b/nodemon.debug.json index feee2c7b9..e384c39ae 100644 --- a/nodemon.debug.json +++ b/nodemon.debug.json @@ -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" } diff --git a/nodemon.json b/nodemon.json index bd8327afc..c8663db0e 100644 --- a/nodemon.json +++ b/nodemon.json @@ -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" } diff --git a/package-lock.json b/package-lock.json index d8d551fa1..74e57eb8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,6 +24,14 @@ "js-tokens": "^4.0.0" } }, + "@babel/runtime": { + "version": "7.3.1", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.3.1.tgz", + "integrity": "sha512-7jGW8ppV0ant637pIqAcFfQDDH1orEPGJb8aXfUozuCU3QqX7rX4DA8iwrbPrR1hcH0FTTHz47yQnk+bl5xHQA==", + "requires": { + "regenerator-runtime": "^0.12.0" + } + }, "@mrmlnc/readdir-enhanced": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz", @@ -1754,8 +1762,7 @@ "core-js": { "version": "2.6.4", "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.4.tgz", - "integrity": "sha512-05qQ5hXShcqGkPZpXEFLIpxayZscVD2kuMBZewxiIPPEagukO4mqgPA9CWhUvFBJfy3ODdK2p9xyHh7FTU9/7A==", - "dev": true + "integrity": "sha512-05qQ5hXShcqGkPZpXEFLIpxayZscVD2kuMBZewxiIPPEagukO4mqgPA9CWhUvFBJfy3ODdK2p9xyHh7FTU9/7A==" }, "core-util-is": { "version": "1.0.2", @@ -3032,7 +3039,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/figures/-/figures-2.0.0.tgz", "integrity": "sha1-OrGi0qYsi/tDGgyUy3l6L84nyWI=", - "dev": true, "requires": { "escape-string-regexp": "^1.0.5" } @@ -5658,6 +5664,23 @@ "resolved": "https://registry.npmjs.org/iterall/-/iterall-1.2.2.tgz", "integrity": "sha512-yynBb1g+RFUPY64fTrFv7nsjRrENBQJaX2UL+2Szc9REFrSNm1rpSXHGzhmAy7a9uv3vlvgBlXnf9RqmPH1/DA==" }, + "jest-get-type": { + "version": "24.0.0", + "resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-24.0.0.tgz", + "integrity": "sha512-z6/Eyf6s9ZDGz7eOvl+fzpuJmN9i0KyTt1no37/dHu8galssxz5ZEgnc1KaV8R31q1khxyhB4ui/X5ZjjPk77w==" + }, + "jest-validate": { + "version": "24.0.0", + "resolved": "https://registry.npmjs.org/jest-validate/-/jest-validate-24.0.0.tgz", + "integrity": "sha512-vMrKrTOP4BBFIeOWsjpsDgVXATxCspC9S1gqvbJ3Tnn/b9ACsJmteYeVx9830UMV28Cob1RX55x96Qq3Tfad4g==", + "requires": { + "camelcase": "^5.0.0", + "chalk": "^2.0.1", + "jest-get-type": "^24.0.0", + "leven": "^2.1.0", + "pretty-format": "^24.0.0" + } + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -5831,6 +5854,11 @@ "flush-write-stream": "^1.0.2" } }, + "leven": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/leven/-/leven-2.1.0.tgz", + "integrity": "sha1-wuep93IJTe6dNCAq6KzORoeHVYA=" + }, "levn": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/levn/-/levn-0.3.0.tgz", @@ -6067,6 +6095,30 @@ "integrity": "sha1-7GZi5IlkCO1KtsVCo5kLcswIACA=", "dev": true }, + "log-process-errors": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/log-process-errors/-/log-process-errors-2.0.0.tgz", + "integrity": "sha512-Lwr2rb9mN0cGGgEGabFJKw/BOmaJF7rfvt67BhzKjJxtOJCsv0ELQwhy+eWt16nCy+8hW7JjVxphe9vEWReS8w==", + "requires": { + "@babel/runtime": "^7.3.1", + "chalk": "^2.4.2", + "core-js": "^2.6.4", + "figures": "^2.0.0", + "jest-validate": "^24.0.0", + "moize": "^5.4.1", + "supports-color": "^6.1.0" + }, + "dependencies": { + "supports-color": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-6.1.0.tgz", + "integrity": "sha512-qe1jfm1Mg7Nq/NSh6XE24gPXROEVsWHxC1LIx//XNlD9iw7YZQGjZNjYN7xGaEG6iKdA8EtNFW6R0gjnVXp+wQ==", + "requires": { + "has-flag": "^3.0.0" + } + } + } + }, "log-symbols": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-2.2.0.tgz", @@ -7293,6 +7345,22 @@ "integrity": "sha512-ZzWuos7TI5CKUeQAtFd6Zhm2s6EpAD/ZLApIhsF9pRvRtM1RFo61dM/4MSRUA0SuLugA/zgrZD8m0BaY46Og7g==", "dev": true }, + "pretty-format": { + "version": "24.0.0", + "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-24.0.0.tgz", + "integrity": "sha512-LszZaKG665djUcqg5ZQq+XzezHLKrxsA86ZABTozp+oNhkdqa+tG2dX4qa6ERl5c/sRDrAa3lHmwnvKoP+OG/g==", + "requires": { + "ansi-regex": "^4.0.0", + "ansi-styles": "^3.2.0" + }, + "dependencies": { + "ansi-regex": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.0.0.tgz", + "integrity": "sha512-iB5Dda8t/UqpPI/IjsejXu5jOGDrzn41wJyljwPH65VCIbk6+1BzFIMJGFwTNrYXT1CrD+B4l19U7awiQ8rk7w==" + } + } + }, "pretty-hrtime": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/pretty-hrtime/-/pretty-hrtime-1.0.3.tgz", @@ -7536,6 +7604,11 @@ "strip-indent": "^2.0.0" } }, + "regenerator-runtime": { + "version": "0.12.1", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.12.1.tgz", + "integrity": "sha512-odxIc1/vDlo4iZcfXqRYFj0vpXFNoGdKMAUieAlFYO6m/nl5e9KR/beGf41z4a1FI+aQgtjhuaSlDxQ0hmkrHg==" + }, "regex-not": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz", diff --git a/package.json b/package.json index 70e5a582c..cefd25ab0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/run/exit/graceful_exit.js b/src/run/exit/graceful_exit.js index 9f2a61350..e1d8b20cd 100644 --- a/src/run/exit/graceful_exit.js +++ b/src/run/exit/graceful_exit.js @@ -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, }) @@ -46,9 +48,12 @@ const eGracefulExit = addErrorHandler(oGracefulExit, gracefulExitHandler) const gracefulExit = async function({ protocolAdapters, dbAdapters, + stopProcessErrors, config, measures, }) { + stopProcessErrors() + const protocolPromises = closeProtocols({ protocolAdapters, config, diff --git a/src/run/exit/setup.js b/src/run/exit/setup.js index c3e77ec7e..f202d53f3 100644 --- a/src/run/exit/setup.js +++ b/src/run/exit/setup.js @@ -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, }) diff --git a/src/run/process.js b/src/run/process.js index 77423fa2d..ccf815bb6 100644 --- a/src/run/process.js +++ b/src/run/process.js @@ -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') @@ -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, })