Skip to content

Commit f6d29a0

Browse files
joyeecheungrefack
authored andcommitted
process: move setup of process warnings into node.js
To clarify the side effects and conditions of the warning setup during bootstrap. PR-URL: nodejs#25263 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 16fdd70 commit f6d29a0

File tree

2 files changed

+87
-80
lines changed

2 files changed

+87
-80
lines changed

lib/internal/bootstrap/node.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,15 @@ function startup() {
107107
process.exit = wrapped.exit;
108108
}
109109

110-
NativeModule.require('internal/process/warning').setup();
110+
const {
111+
onWarning,
112+
emitWarning
113+
} = NativeModule.require('internal/process/warning');
114+
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
115+
process.on('warning', onWarning);
116+
}
117+
process.emitWarning = emitWarning;
118+
111119
const {
112120
nextTick,
113121
runNextTicks

lib/internal/process/warning.js

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
const prefix = `(${process.release.name}:${process.pid}) `;
44
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
55

6-
exports.setup = setupProcessWarnings;
7-
86
// Lazily loaded
97
let fs;
108
let fd;
@@ -51,83 +49,84 @@ function doEmitWarning(warning) {
5149
return () => process.emit('warning', warning);
5250
}
5351

54-
function setupProcessWarnings() {
55-
if (!process.noProcessWarnings && process.env.NODE_NO_WARNINGS !== '1') {
56-
process.on('warning', (warning) => {
57-
if (!(warning instanceof Error)) return;
58-
const isDeprecation = warning.name === 'DeprecationWarning';
59-
if (isDeprecation && process.noDeprecation) return;
60-
const trace = process.traceProcessWarnings ||
61-
(isDeprecation && process.traceDeprecation);
62-
var msg = prefix;
63-
if (warning.code)
64-
msg += `[${warning.code}] `;
65-
if (trace && warning.stack) {
66-
msg += `${warning.stack}`;
67-
} else {
68-
const toString =
69-
typeof warning.toString === 'function' ?
70-
warning.toString : Error.prototype.toString;
71-
msg += `${toString.apply(warning)}`;
72-
}
73-
if (typeof warning.detail === 'string') {
74-
msg += `\n${warning.detail}`;
75-
}
76-
const warningFile = lazyOption();
77-
if (warningFile) {
78-
return writeToFile(msg);
79-
}
80-
writeOut(msg);
81-
});
52+
function onWarning(warning) {
53+
if (!(warning instanceof Error)) return;
54+
const isDeprecation = warning.name === 'DeprecationWarning';
55+
if (isDeprecation && process.noDeprecation) return;
56+
const trace = process.traceProcessWarnings ||
57+
(isDeprecation && process.traceDeprecation);
58+
var msg = prefix;
59+
if (warning.code)
60+
msg += `[${warning.code}] `;
61+
if (trace && warning.stack) {
62+
msg += `${warning.stack}`;
63+
} else {
64+
const toString =
65+
typeof warning.toString === 'function' ?
66+
warning.toString : Error.prototype.toString;
67+
msg += `${toString.apply(warning)}`;
68+
}
69+
if (typeof warning.detail === 'string') {
70+
msg += `\n${warning.detail}`;
71+
}
72+
const warningFile = lazyOption();
73+
if (warningFile) {
74+
return writeToFile(msg);
8275
}
76+
writeOut(msg);
77+
}
8378

84-
// process.emitWarning(error)
85-
// process.emitWarning(str[, type[, code]][, ctor])
86-
// process.emitWarning(str[, options])
87-
process.emitWarning = (warning, type, code, ctor, now) => {
88-
let detail;
89-
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
90-
ctor = type.ctor;
91-
code = type.code;
92-
if (typeof type.detail === 'string')
93-
detail = type.detail;
94-
type = type.type || 'Warning';
95-
} else if (typeof type === 'function') {
96-
ctor = type;
97-
code = undefined;
98-
type = 'Warning';
99-
}
100-
if (type !== undefined && typeof type !== 'string') {
101-
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
102-
}
103-
if (typeof code === 'function') {
104-
ctor = code;
105-
code = undefined;
106-
} else if (code !== undefined && typeof code !== 'string') {
107-
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
108-
}
109-
if (typeof warning === 'string') {
110-
// Improve error creation performance by skipping the error frames.
111-
// They are added in the `captureStackTrace()` function below.
112-
const tmpStackLimit = Error.stackTraceLimit;
113-
Error.stackTraceLimit = 0;
114-
// eslint-disable-next-line no-restricted-syntax
115-
warning = new Error(warning);
116-
Error.stackTraceLimit = tmpStackLimit;
117-
warning.name = String(type || 'Warning');
118-
if (code !== undefined) warning.code = code;
119-
if (detail !== undefined) warning.detail = detail;
120-
Error.captureStackTrace(warning, ctor || process.emitWarning);
121-
} else if (!(warning instanceof Error)) {
122-
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
123-
}
124-
if (warning.name === 'DeprecationWarning') {
125-
if (process.noDeprecation)
126-
return;
127-
if (process.throwDeprecation)
128-
throw warning;
129-
}
130-
if (now) process.emit('warning', warning);
131-
else process.nextTick(doEmitWarning(warning));
132-
};
79+
// process.emitWarning(error)
80+
// process.emitWarning(str[, type[, code]][, ctor])
81+
// process.emitWarning(str[, options])
82+
function emitWarning(warning, type, code, ctor, now) {
83+
let detail;
84+
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
85+
ctor = type.ctor;
86+
code = type.code;
87+
if (typeof type.detail === 'string')
88+
detail = type.detail;
89+
type = type.type || 'Warning';
90+
} else if (typeof type === 'function') {
91+
ctor = type;
92+
code = undefined;
93+
type = 'Warning';
94+
}
95+
if (type !== undefined && typeof type !== 'string') {
96+
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
97+
}
98+
if (typeof code === 'function') {
99+
ctor = code;
100+
code = undefined;
101+
} else if (code !== undefined && typeof code !== 'string') {
102+
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
103+
}
104+
if (typeof warning === 'string') {
105+
// Improve error creation performance by skipping the error frames.
106+
// They are added in the `captureStackTrace()` function below.
107+
const tmpStackLimit = Error.stackTraceLimit;
108+
Error.stackTraceLimit = 0;
109+
// eslint-disable-next-line no-restricted-syntax
110+
warning = new Error(warning);
111+
Error.stackTraceLimit = tmpStackLimit;
112+
warning.name = String(type || 'Warning');
113+
if (code !== undefined) warning.code = code;
114+
if (detail !== undefined) warning.detail = detail;
115+
Error.captureStackTrace(warning, ctor || process.emitWarning);
116+
} else if (!(warning instanceof Error)) {
117+
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
118+
}
119+
if (warning.name === 'DeprecationWarning') {
120+
if (process.noDeprecation)
121+
return;
122+
if (process.throwDeprecation)
123+
throw warning;
124+
}
125+
if (now) process.emit('warning', warning);
126+
else process.nextTick(doEmitWarning(warning));
133127
}
128+
129+
module.exports = {
130+
onWarning,
131+
emitWarning
132+
};

0 commit comments

Comments
 (0)