📜  Node.js process.emitWarning() 方法

📅  最后修改于: 2022-05-13 01:56:25.356000             🧑  作者: Mango

Node.js process.emitWarning() 方法

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

句法:

process.emitWarning(warning[, options])

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

  • 警告:用于表示要发出的警告。
  • 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
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
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

输出:

示例 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

输出:

示例 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

输出:

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