From 71d4a7474d234bba9461e28c95841bbd090cc642 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sat, 12 Nov 2016 23:34:35 -0500 Subject: [PATCH 1/2] doc: improve process.emitWarning() example PR-URL: https://github.com/nodejs/node/pull/9590 Reviewed-By: Roman Reiss Reviewed-By: Prince John Wesley Reviewed-By: Colin Ihrig --- doc/api/process.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/api/process.md b/doc/api/process.md index 7b74080352c6f1..4ea0fd864e5ecb 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -821,11 +821,10 @@ so, it is recommended to place the `emitWarning()` behind a simple boolean flag as illustrated in the example below: ```js -var warned = false; function emitMyWarning() { - if (!warned) { + if (!emitMyWarning.warned) { + emitMyWarning.warned = true; process.emitWarning('Only warn once!'); - warned = true; } } emitMyWarning(); From 5242114d89652a217880c0a0f216bf46a51c1379 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sat, 12 Nov 2016 23:38:29 -0500 Subject: [PATCH 2/2] doc: remove redundant warning information process.emitWarning() already describes how to emit custom warnings, so just merely provide a link to that function from the 'warning' event documentation. PR-URL: https://github.com/nodejs/node/pull/9590 Reviewed-By: Roman Reiss Reviewed-By: Prince John Wesley Reviewed-By: Colin Ihrig --- doc/api/process.md | 49 +++++----------------------------------------- 1 file changed, 5 insertions(+), 44 deletions(-) diff --git a/doc/api/process.md b/doc/api/process.md index 4ea0fd864e5ecb..44db505eab8e04 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -312,50 +312,6 @@ $ node --no-warnings The `--trace-warnings` command-line option can be used to have the default console output for warnings include the full stack trace of the warning. -#### Emitting custom warnings - -The [`process.emitWarning()`][process_emit_warning] method can be used to issue -custom or application specific warnings. - -```js -// Emit a warning using a string... -process.emitWarning('Something happened!'); - // Prints: (node 12345) Warning: Something happened! - -// Emit a warning using an object... -process.emitWarning('Something Happened!', 'CustomWarning'); - // Prints: (node 12345) CustomWarning: Something happened! - -// Emit a warning using a custom Error object... -class CustomWarning extends Error { - constructor(message) { - super(message); - this.name = 'CustomWarning'; - Error.captureStackTrace(this, CustomWarning); - } -} -const myWarning = new CustomWarning('Something happened!'); -process.emitWarning(myWarning); - // Prints: (node 12345) CustomWarning: Something happened! -``` - -#### Emitting custom deprecation warnings - -Custom deprecation warnings can be emitted by setting the `name` of a custom -warning to `DeprecationWarning`. For instance: - -```js -process.emitWarning('This API is deprecated', 'DeprecationWarning'); -``` - -Or, - -```js -const err = new Error('This API is deprecated'); -err.name = 'DeprecationWarning'; -process.emitWarning(err); -``` - Launching Node.js using the `--throw-deprecation` command line flag will cause custom deprecation warnings to be thrown as exceptions. @@ -368,6 +324,11 @@ of the custom deprecation. The `*-deprecation` command line flags only affect warnings that use the name `DeprecationWarning`. +#### Emitting custom warnings + +See the [`process.emitWarning()`][process_emit_warning] method for issuing +custom or application-specific warnings. + ### Signal Events