当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js process.emitWarning()用法及代码示例


process.emitWarning()方法是过程模块的内置应用程序编程接口,用于发送自定义或application-specific过程警告。

用法:

process.emitWarning(warning[, options])

参数:该函数接受以下参数:

  • warning:它用于表示将要发出的警告。
  • options:它是一个可选参数,可以具有以下属性:
    • type:它用于表示警告为字符串形式时发出的警告类型。
    • code:它用于表示正在发出的警告实例的唯一标识符。
    • ctor:当警告为字符串形式时,它是一个可选函数,用于限制生成的堆栈跟踪。
    • detail:它用于添加附加文本以包含错误。

返回值:此事件只返回回调函数以进行进一步的操作。



范例1:侦听进程时,process.emitWarning()方法将Error对象传递给警告处理程序。

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

输出:

Start …
Working …
Working …
Working …
(node:12621) [Custom_Warning] Warning:Something happened!
Additional information about warning
(Use `node -trace-warnings …` to show where the warning was created)
Warning:Something happened!
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) {
code:‘Custom_Warning’,
detail:‘Additional information about 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

输出:

Start …
Working …
Working …
Working …
(node:12893) [Custom_Warning] Warning:Something happened!
Additional information about warning
(Use `node -trace-warnings …` to show where the warning was created)
Working …
Working …
(node:12893) [007] IMPORTANT:Something needs to be fixed!
Can not proceed further!
Fix this ASAP!

范例3:将Error对象(而不是字符串)作为警告传递。请注意,如果正在传递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

输出:

Start …
Working …
Working …
Working …
(node:13232) Error:Whoops!
(Use `node -trace-warnings …` to show where the warning was created)
Error:Whoops!
at Timeout._onTimeout (/home/aditya/Programming/GFG/New/EmitWarning.js:9:25)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)

参考: https://nodejs.org/api/process.html#process_process_emitwarning_warning_type_code_ctor

相关用法


注:本文由纯净天空筛选整理自adityapande88大神的英文原创作品 Node.js process.emitWarning() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。