|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const { exit } = require('process') |
4 | | -const { promisify } = require('util') |
5 | | - |
6 | 3 | const { getInfo } = require('./info') |
7 | 4 | const { getColors } = require('./colors') |
8 | 5 | const { getLevel } = require('./level') |
9 | 6 | const { getMessage } = require('./message') |
| 7 | +const { exitProcess } = require('./exit') |
10 | 8 |
|
11 | 9 | // Generic event handler for all events. |
12 | 10 | const handleEvent = async function({ |
@@ -35,32 +33,16 @@ const handleEvent = async function({ |
35 | 33 | const level = getLevel({ opts, info }) |
36 | 34 | const message = getMessage({ opts, info, level, colors }) |
37 | 35 |
|
38 | | - // We need to `await` it in case we are logging an `uncaughtException` which |
39 | | - // will make the process exit. |
| 36 | + // We need to `await` it in case `opts.exitOn` exits the process. |
40 | 37 | // Without `await` Node.js would still wait until most async tasks (including |
41 | 38 | // stream draining for logging libraries like Winston) have completed. |
42 | 39 | // But there are some cases where it will not. In those cases, `opts.log()` |
43 | 40 | // should be either synchronous or return a promise. |
44 | 41 | await opts.log(message, level, info) |
45 | 42 |
|
46 | | - await exitProcess({ eventName }) |
47 | | -} |
48 | | - |
49 | | -// Exit process on `uncaughtException` |
50 | | -// See https://nodejs.org/api/process.html#process_warning_using_uncaughtexception_correctly |
51 | | -const exitProcess = async function({ eventName }) { |
52 | | - if (eventName !== 'uncaughtException') { |
53 | | - return |
54 | | - } |
55 | | - |
56 | | - // This is only needed as a safety measure |
57 | | - await promisify(setTimeout)(EXIT_TIMEOUT) |
58 | | - |
59 | | - exit(1) |
| 43 | + await exitProcess({ eventName, opts }) |
60 | 44 | } |
61 | 45 |
|
62 | | -const EXIT_TIMEOUT = 3e3 |
63 | | - |
64 | 46 | module.exports = { |
65 | 47 | handleEvent, |
66 | 48 | } |
0 commit comments