process.emitWarning(warning[, options])
添加於:v8.0.0
參數
warning
<string> | <Error> 要發出的警告。options
<Object>type
<string>什麽時候warning
是一個String
,type
是用於類型發出警告。默認:'Warning'
.code
<string> 發出的警告實例的唯一標識符。ctor
<Function> 當warning
是String
時,ctor
是一個可選函數,用於限製生成的堆棧跟蹤。 默認:process.emitWarning
。detail
<string> 要包含在錯誤中的附加文本。
process.emitWarning()
方法可用於發出自定義或應用程序特定的進程警告。可以通過向
事件添加處理程序來監聽這些。'warning'
import { emitWarning } from 'node:process';
// Emit a warning with a code and additional detail.
emitWarning('Something happened!', {
code: 'MY_WARNING',
detail: 'This is some additional information'
});
// Emits:
// (node:56338) [MY_WARNING] Warning: Something happened!
// This is some additional information
const { emitWarning } = require('node:process');
// Emit a warning with a code and additional detail.
emitWarning('Something happened!', {
code: 'MY_WARNING',
detail: 'This is some additional information'
});
// Emits:
// (node:56338) [MY_WARNING] Warning: Something happened!
// This is some additional information
在此示例中,Error
對象由 process.emitWarning()
在內部生成並傳遞給
處理程序。'warning'
import process from 'node:process';
process.on('warning', (warning) => {
console.warn(warning.name); // 'Warning'
console.warn(warning.message); // 'Something happened!'
console.warn(warning.code); // 'MY_WARNING'
console.warn(warning.stack); // Stack trace
console.warn(warning.detail); // 'This is some additional information'
});
const process = require('node:process');
process.on('warning', (warning) => {
console.warn(warning.name); // 'Warning'
console.warn(warning.message); // 'Something happened!'
console.warn(warning.code); // 'MY_WARNING'
console.warn(warning.stack); // Stack trace
console.warn(warning.detail); // 'This is some additional information'
});
如果 warning
作為 Error
對象傳遞,則忽略 options
參數。
相關用法
- Node.js process.emitWarning(warning[, type[, code]][, ctor])用法及代碼示例
- Node.js process.emitWarning()用法及代碼示例
- Node.js process.execPath用法及代碼示例
- Node.js process.execArgv用法及代碼示例
- Node.js process.env用法及代碼示例
- Node.js process.env()用法及代碼示例
- Node.js process.exitCode用法及代碼示例
- Node.js process.exit([code])用法及代碼示例
- Node.js process.exit()用法及代碼示例
- Node.js process.stdin用法及代碼示例
- Node.js process.arch()用法及代碼示例
- Node.js process.nextTick(callback[, ...args])用法及代碼示例
- Node.js process.noDeprecation用法及代碼示例
- Node.js process.setUncaughtExceptionCaptureCallback()用法及代碼示例
- Node.js process.getgid()用法及代碼示例
- Node.js process.setgid(id)用法及代碼示例
- Node.js process.chdir(directory)用法及代碼示例
- Node.js process.setgid()用法及代碼示例
- Node.js process.getuid()用法及代碼示例
- Node.js process.ppid用法及代碼示例
- Node.js process.report.reportOnSignal用法及代碼示例
- Node.js process.report.directory用法及代碼示例
- Node.js process.umask(mask)用法及代碼示例
- Node.js process.setgroups(groups)用法及代碼示例
- Node.js process.setegid(id)用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 process.emitWarning(warning[, options])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。