@@ -22,10 +22,58 @@ export class ErrorEvent extends Event {
22
22
*/
23
23
public message ?: string | undefined
24
24
25
- constructor ( type : string , code ?: number , message ?: string ) {
25
+ /**
26
+ * Constructs a new `ErrorEvent` instance. This is typically not called directly,
27
+ * but rather emitted by the `EventSource` object when an error occurs.
28
+ *
29
+ * @param type - The type of the event (should be "error")
30
+ * @param errorEventInitDict - Optional properties to include in the error event
31
+ */
32
+ constructor (
33
+ type : string ,
34
+ errorEventInitDict ?: { message ?: string | undefined ; code ?: number | undefined } ,
35
+ ) {
26
36
super ( type )
27
- this . code = code ?? undefined
28
- this . message = message ?? undefined
37
+ this . code = errorEventInitDict ?. code ?? undefined
38
+ this . message = errorEventInitDict ?. message ?? undefined
39
+ }
40
+
41
+ /**
42
+ * Node.js "hides" the `message` and `code` properties of the `ErrorEvent` instance,
43
+ * when it is `console.log`'ed. This makes it harder to debug errors. To ease debugging,
44
+ * we explicitly include the properties in the `inspect` method.
45
+ *
46
+ * This is automatically called by Node.js when you `console.log` an instance of this class.
47
+ *
48
+ * @param _depth - The current depth
49
+ * @param options - The options passed to `util.inspect`
50
+ * @param inspect - The inspect function to use (prevents having to import it from `util`)
51
+ * @returns A string representation of the error
52
+ */
53
+ [ Symbol . for ( 'nodejs.util.inspect.custom' ) ] (
54
+ _depth : number ,
55
+ options : { colors : boolean } ,
56
+ inspect : ( obj : unknown , inspectOptions : { colors : boolean } ) => string ,
57
+ ) : string {
58
+ return inspect ( inspectableError ( this ) , options )
59
+ }
60
+
61
+ /**
62
+ * Deno "hides" the `message` and `code` properties of the `ErrorEvent` instance,
63
+ * when it is `console.log`'ed. This makes it harder to debug errors. To ease debugging,
64
+ * we explicitly include the properties in the `inspect` method.
65
+ *
66
+ * This is automatically called by Deno when you `console.log` an instance of this class.
67
+ *
68
+ * @param inspect - The inspect function to use (prevents having to import it from `util`)
69
+ * @param options - The options passed to `Deno.inspect`
70
+ * @returns A string representation of the error
71
+ */
72
+ [ Symbol . for ( 'Deno.customInspect' ) ] (
73
+ inspect : ( obj : unknown , inspectOptions : { colors : boolean } ) => string ,
74
+ options : { colors : boolean } ,
75
+ ) : string {
76
+ return inspect ( inspectableError ( this ) , options )
29
77
}
30
78
}
31
79
@@ -73,3 +121,21 @@ export function flattenError(err: unknown): string {
73
121
74
122
return err . message
75
123
}
124
+
125
+ /**
126
+ * Convert an `ErrorEvent` instance into a plain object for inspection.
127
+ *
128
+ * @param err - The `ErrorEvent` instance to inspect
129
+ * @returns A plain object representation of the error
130
+ * @internal
131
+ */
132
+ function inspectableError ( err : ErrorEvent ) {
133
+ return {
134
+ type : err . type ,
135
+ message : err . message ,
136
+ code : err . code ,
137
+ defaultPrevented : err . defaultPrevented ,
138
+ cancelable : err . cancelable ,
139
+ timeStamp : err . timeStamp ,
140
+ }
141
+ }
0 commit comments