Repository navigation
process: Add code to warnings, assign codes to deprecations #10116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Add the ability to assign an optional code to process warnings + add additional type checking to ensure that names and codes can only be strings.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -706,14 +706,15 @@ console.log(process.env.test); | |
| // => 1 | ||
| ``` | ||
|
|
||
| ## process.emitWarning(warning[, name][, ctor]) | ||
| ## process.emitWarning(warning[, type[, code]][, ctor]) | ||
| <!-- YAML | ||
| added: v6.0.0 | ||
| --> | ||
|
|
||
| * `warning` {String | Error} The warning to emit. | ||
| * `name` {String} When `warning` is a String, `name` is the name to use | ||
| for the warning. Default: `Warning`. | ||
| * `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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "number" when I think "code", just me? This seems like an "id", its even described as an identifier in the docs.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use of |
||
| * `ctor` {Function} When `warning` is a String, `ctor` is an optional | ||
| function used to limit the generated stack trace. Default | ||
| `process.emitWarning` | ||
|
|
@@ -729,11 +730,16 @@ process.emitWarning('Something happened!'); | |
| ``` | ||
|
|
||
| ```js | ||
| // Emit a warning using a string and a name... | ||
| // Emit a warning using a string and a type... | ||
| process.emitWarning('Something Happened!', 'CustomWarning'); | ||
| // Emits: (node:56338) CustomWarning: Something Happened! | ||
| ``` | ||
|
|
||
| ```js | ||
| process.emitWarning('Something happened!', 'CustomWarning', 'WARN001'); | ||
| // Emits: (node:56338) CustomWarning [WARN001]: Something Happened! | ||
| ``` | ||
|
|
||
| In each of the previous examples, an `Error` object is generated internally by | ||
| `process.emitWarning()` and passed through to the | ||
| [`process.on('warning')`][process_warning] event. | ||
|
|
@@ -742,21 +748,24 @@ In each of the previous examples, an `Error` object is generated internally by | |
| process.on('warning', (warning) => { | ||
| console.warn(warning.name); | ||
| console.warn(warning.message); | ||
| console.warn(warning.code); | ||
| console.warn(warning.stack); | ||
| }); | ||
| ``` | ||
|
|
||
| If `warning` is passed as an `Error` object, it will be passed through to the | ||
| `process.on('warning')` event handler unmodified (and the optional `name` | ||
| and `ctor` arguments will be ignored): | ||
| `process.on('warning')` event handler unmodified (and the optional `type`, | ||
| `code` and `ctor` arguments will be ignored): | ||
|
|
||
| ```js | ||
| // Emit a warning using an Error object... | ||
| const myWarning = new Error('Warning! Something happened!'); | ||
| // Use the Error name property to specify the type name | ||
| myWarning.name = 'CustomWarning'; | ||
| myWarning.code = 'WARN001'; | ||
|
|
||
| process.emitWarning(myWarning); | ||
| // Emits: (node:56338) CustomWarning: Warning! Something Happened! | ||
| // Emits: (node:56338) CustomWarning [WARN001]: Warning! Something Happened! | ||
| ``` | ||
|
|
||
| A `TypeError` is thrown if `warning` is anything other than a string or `Error` | ||
|
|
@@ -765,7 +774,7 @@ object. | |
| Note that while process warnings use `Error` objects, the process warning | ||
| mechanism is **not** a replacement for normal error handling mechanisms. | ||
|
|
||
| The following additional handling is implemented if the warning `name` is | ||
| The following additional handling is implemented if the warning `type` is | ||
| `DeprecationWarning`: | ||
|
|
||
| * If the `--throw-deprecation` command-line flag is used, the deprecation | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a options object
{type, code, ctor}is in order here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is basically just intended for internal use, so doesn't need a nice API, and changing the call signature would cause a fair amount of internal churn.
Honestly, I don't think it should be an API at all, maybe we can deprecate it? Seriously, why do we want to expose this to users of node? We expect them to be using it to emit their own warnings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fwiw it was deliberately added as an outward-facing API in c6656db (released in v6.0.0) with that intention, yes. I don’t mind it being there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was exposed this way because we had users asking to be able to use it also. I'm definitely -1 on deprecating it as I have found it to be quite useful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, then I agree with @silverwind, its become an ugly API, with three strings in a row, 2 of which are optional, and remembering the arg order isn't reasonably possible. @addaleax the problem with APIs is they rot with time, becoming harder and harder to use and support, like this one is now, which is why I am generally in favour of not adding unnecessary APIs, even if when they are useful. I guess given the alternative of adding a second options-based call pattern, and supporting the old 3 argument call for backwards compatibility, and documenting both of them, or just adding another optional string arg, better to do the latter. If we ever add a 4th string, though, it will no longer be reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sam-github My thoughts exactly. Supporting two call signatures is easy and enables easier future additons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API already supports two call signatures. The first argument can be passed in as a pre-constructed
Errorobject with all of the relevant properties set:is equivalent to:
Having an options object when we can already pass in an
Errorobject would be rather pointless, IMOThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That Error behaviour isn't well documented. I read carefully, and I see now that its implied by by the "If warning is passed as an Error object, it will be passed through .... unmodified" sentence.
I think the docs could be clearer, but that's an aside from this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not just implied, it's explicitly stated in the arguments list that
warningcan be a string or anError, and an example of passing anErroris shown immediately following theIf warning is passed as an Error object...sentence. Immediately following the example is a clear statement that aTypeErrorwill be thrown iswarningis anything other than a string orErrorobject. The docs seem pretty explicit about it.