diff options
author | James M Snell <jasnell@gmail.com> | 2017-04-28 09:06:10 -0700 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-05-01 11:53:47 -0700 |
commit | dd20e68b0feb966c2a7439c947bbb4d46e3b19fe (patch) | |
tree | a43357ad4f099b554ede9160aef2d2edf3a3d9ce /doc | |
parent | 23fc082409e46b5d85d66c74affb7a9422f8f489 (diff) | |
download | android-node-v8-dd20e68b0feb966c2a7439c947bbb4d46e3b19fe.tar.gz android-node-v8-dd20e68b0feb966c2a7439c947bbb4d46e3b19fe.tar.bz2 android-node-v8-dd20e68b0feb966c2a7439c947bbb4d46e3b19fe.zip |
process: add optional detail to process emitWarning
Adds a new method signature variant for process.emitWarning()
that accepts an options object. The options object may include
a new `detail` option that allows additional detail text to be
associated with the warning. By default, this additional text
will be printed to stderr along with the warning, and included
on the Warning Error object using the `.detail` property.
e.g.
```js
process.emitWarning('A message', {
code: 'WARNING123',
detail: 'This is additional detail'
});
// Emits:
// (node {pid}) [WARNING123] Warning: A message
// This is additional detail
```
PR-URL: https://github.com/nodejs/node/pull/12725
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/process.md | 56 |
1 files changed, 51 insertions, 5 deletions
diff --git a/doc/api/process.md b/doc/api/process.md index 3f853e5511..133e3f7de5 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -637,6 +637,52 @@ process's [`ChildProcess.disconnect()`][]. If the Node.js process was not spawned with an IPC channel, `process.disconnect()` will be `undefined`. +## process.emitWarning(warning[, options]) +<!-- YAML +added: REPLACEME +--> + +* `warning` {string|Error} The warning to emit. +* `options` {Object} + * `type` {string} When `warning` is a String, `type` is the name to use + for the *type* of warning being emitted. Default: `Warning`. + * `code` {string} A unique identifier for the warning instance being emitted. + * `ctor` {Function} When `warning` is a String, `ctor` is an optional + function used to limit the generated stack trace. Default + `process.emitWarning` + * `detail` {string} Additional text to include with the error. + +The `process.emitWarning()` method can be used to emit custom or application +specific process warnings. These can be listened for by adding a handler to the +[`process.on('warning')`][process_warning] event. + +```js +// Emit a warning with a code and additional detail. +process.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 +``` + +In this example, an `Error` object is generated internally by +`process.emitWarning()` and passed through to the +[`process.on('warning')`][process_warning] event. + +```js +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' +}); +``` + +If `warning` is passed as an `Error` object, the `options` argument is ignored. + ## process.emitWarning(warning[, type[, code]][, ctor]) <!-- YAML added: v6.0.0 @@ -655,13 +701,13 @@ specific process warnings. These can be listened for by adding a handler to the [`process.on('warning')`][process_warning] event. ```js -// Emit a warning using a string... +// Emit a warning using a string. process.emitWarning('Something happened!'); // Emits: (node: 56338) Warning: Something happened! ``` ```js -// Emit a warning using a string and a type... +// Emit a warning using a string and a type. process.emitWarning('Something Happened!', 'CustomWarning'); // Emits: (node:56338) CustomWarning: Something Happened! ``` @@ -689,14 +735,14 @@ If `warning` is passed as an `Error` object, it will be passed through to the `code` and `ctor` arguments will be ignored): ```js -// Emit a warning using an Error object... -const myWarning = new Error('Warning! Something happened!'); +// Emit a warning using an Error object. +const myWarning = new Error('Something happened!'); // Use the Error name property to specify the type name myWarning.name = 'CustomWarning'; myWarning.code = 'WARN001'; process.emitWarning(myWarning); -// Emits: (node:56338) [WARN001] CustomWarning: Warning! Something happened! +// Emits: (node:56338) [WARN001] CustomWarning: Something happened! ``` A `TypeError` is thrown if `warning` is anything other than a string or `Error` |