Node.js process.emitWarning() 方法
process.emitWarning() 方法是 process 模块的内置应用程序编程接口,用于发送自定义或特定于应用程序的进程警告。
语法:
process.emitWarning(warning[, options])
参数: 此函数接受以下参数:
- warning: 用于表示要发出的警告。
- options: 这是一个可选参数,它可以具有以下属性:
- type: 用于表示以字符串形式发出的警告的类型。
- code: 用于表示发出的警告实例的唯一标识符。
- ctor: 这是一个可选函数,用于限制在字符串形式的警告中生成的堆栈跟踪。
- detail: 用于添加要与错误一起包含的附加文本。
返回值: 此事件返回一个回调函数,用于进一步操作。
示例1: 当监听进程时, process.emitWarning() 方法将错误对象传递给警告处理程序。
index.js
console.log("Start ...");
// Use setInterval to keep the process running
setInterval(() => {
console.log("Working ...");
}, 1000);
setTimeout(() => {
process.emitWarning('Something happened!', {
code: 'Custom_Warning',
detail: 'Additional information about warning'
});
}, 4000);
// Start listening to the process
process.on('warning', (warning) => {
// If there is a warning then print
// it and stop the process
if (warning) {
console.log(warning);
process.exit(0);
}
});
使用以下命令运行 index.js 文件:
node index.js
输出:
开始...
工作中...
工作中...
工作中...
(节点:12621)[自定义警告]警告:发生了某些事情!
关于警告的其他信息
(使用`node –trace-warnings …`显示警告创建的位置)
警告:发生了某些事情!
at Timeout._onTimeout (/home/aditya/Programming/GFG/New/EmitWarning.js:9:13)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7) {
代码:'Custom_Warning',
详细信息:'关于警告的其他信息'
}
示例2: 根据警告类型发出多个警告并采取行动。
index.js
console.log("Start ...");
// Use setInterval to keep the process running
setInterval(() => {
console.log("Working ...");
}, 1000);
setTimeout(() => {
process.emitWarning('Something happened!', {
code: 'Custom_Warning',
detail: 'Additional information about warning'
});
}, 4000);
setTimeout(() => {
process.emitWarning('Something needs to be fixed!', {
type: 'IMPORTANT',
code: '007',
detail: 'Can not proceed further!'
});
}, 6000);
// Start listening to the process
process.on('warning', (warning) => {
// If there is an important warning
// stop the process
// warning.name = type
if (warning.name === 'IMPORTANT') {
console.log('Fix this ASAP!')
process.exit(0);
}
});
使用以下命令运行 index.js 文件:
node index.js
输出:
开始...
正在处理...
正在处理...
正在处理...
(node:12893) [自定义警告] 警告:发生了一些事情!
关于警告的额外信息
(使用 `node -trace-warnings …` 显示警告创建的位置)
正在处理...
正在处理...
(node:12893) [007] 重要:需要修复一些问题!
无法继续进行!
尽快解决!
示例 3: 如果将 Error 对象作为警告传递,则会忽略可选参数。
index.js
console.log("Start ...");
// Use setInterval to keep the process running
setInterval(() => {
console.log("Working ...");
}, 1000);
setTimeout(() => {
process.emitWarning(new Error('Whoops!'), {
code: 'Custom_Warning',
detail: 'Additional information about warning'
});
}, 4000);
// Start listening to the process
process.on('warning', (warning) => {
// If there is a warning then print
// it and stop the process
if (warning) {
console.warn(warning);
process.exit(0);
}
});
使用以下命令运行 index.js 文件:
node index.js
输出:
开始……
工作中……
工作中……
工作中……
(node:13232) 错误:糟糕!
(使用 "node –trace-warnings …" 显示警告创建的位置)
错误:糟糕!
在 Timeout._onTimeout (/home/aditya/Programming/GFG/New/EmitWarning.js:9:25)
在 listOnTimeout (node:internal/timers:556:17)
在 processTimers (node:internal/timers:499:7)
参考: https://nodejs.org/api/process.html#process_process_emitwarning_warning_type_code_ctor