當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。