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])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。