@@ -706,14 +706,15 @@ console.log(process.env.test);
706
706
// => 1
707
707
```
708
708
709
- ## process.emitWarning(warning[ , name ] [ , ctor ] )
709
+ ## process.emitWarning(warning[ , type [ , code ] ][ , ctor ] )
710
710
<!-- YAML
711
711
added: v6.0.0
712
712
-->
713
713
714
714
* ` warning ` {String | Error} The warning to emit.
715
- * ` name ` {String} When ` warning ` is a String, ` name ` is the name to use
716
- for the warning. Default: ` Warning ` .
715
+ * ` type ` {String} When ` warning ` is a String, ` type ` is the name to use
716
+ for the * type* of warning being emitted. Default: ` Warning ` .
717
+ * ` code ` {String} A unique identifier for the warning instance being emitted.
717
718
* ` ctor ` {Function} When ` warning ` is a String, ` ctor ` is an optional
718
719
function used to limit the generated stack trace. Default
719
720
` process.emitWarning `
@@ -729,11 +730,16 @@ process.emitWarning('Something happened!');
729
730
```
730
731
731
732
``` js
732
- // Emit a warning using a string and a name ...
733
+ // Emit a warning using a string and a type ...
733
734
process .emitWarning (' Something Happened!' , ' CustomWarning' );
734
735
// Emits: (node:56338) CustomWarning: Something Happened!
735
736
```
736
737
738
+ ``` js
739
+ process .emitWarning (' Something happened!' , ' CustomWarning' , ' WARN001' );
740
+ // Emits: (node:56338) CustomWarning [WARN001]: Something Happened!
741
+ ```
742
+
737
743
In each of the previous examples, an ` Error ` object is generated internally by
738
744
` process.emitWarning() ` and passed through to the
739
745
[ ` process.on('warning') ` ] [ process_warning ] event.
@@ -742,21 +748,24 @@ In each of the previous examples, an `Error` object is generated internally by
742
748
process .on (' warning' , (warning ) => {
743
749
console .warn (warning .name );
744
750
console .warn (warning .message );
751
+ console .warn (warning .code );
745
752
console .warn (warning .stack );
746
753
});
747
754
```
748
755
749
756
If ` warning ` is passed as an ` Error ` object, it will be passed through to the
750
- ` process.on('warning') ` event handler unmodified (and the optional ` name `
751
- and ` ctor ` arguments will be ignored):
757
+ ` process.on('warning') ` event handler unmodified (and the optional ` type ` ,
758
+ ` code ` and ` ctor ` arguments will be ignored):
752
759
753
760
``` js
754
761
// Emit a warning using an Error object...
755
762
const myWarning = new Error (' Warning! Something happened!' );
763
+ // Use the Error name property to specify the type name
756
764
myWarning .name = ' CustomWarning' ;
765
+ myWarning .code = ' WARN001' ;
757
766
758
767
process .emitWarning (myWarning);
759
- // Emits: (node:56338) CustomWarning: Warning! Something Happened!
768
+ // Emits: (node:56338) CustomWarning [WARN001] : Warning! Something Happened!
760
769
```
761
770
762
771
A ` TypeError ` is thrown if ` warning ` is anything other than a string or ` Error `
@@ -765,7 +774,7 @@ object.
765
774
Note that while process warnings use ` Error ` objects, the process warning
766
775
mechanism is ** not** a replacement for normal error handling mechanisms.
767
776
768
- The following additional handling is implemented if the warning ` name ` is
777
+ The following additional handling is implemented if the warning ` type ` is
769
778
` DeprecationWarning ` :
770
779
771
780
* If the ` --throw-deprecation ` command-line flag is used, the deprecation
0 commit comments