@@ -5,153 +5,64 @@ const common = require('../common');
55const assert = require ( 'assert' ) ;
66const errors = require ( 'internal/errors' ) ;
77
8- function invalidKey ( key ) {
9- return new RegExp ( `^An invalid error message key was used: ${ key } \\.$` ) ;
10- }
11-
12- errors . E ( 'TEST_ERROR_1' , 'Error for testing purposes: %s' ) ;
13- errors . E ( 'TEST_ERROR_2' , ( a , b ) => `${ a } ${ b } ` ) ;
8+ errors . E ( 'TEST_ERROR_1' , 'Error for testing purposes: %s' ,
9+ Error , TypeError , RangeError ) ;
10+ errors . E ( 'TEST_ERROR_2' , ( a , b ) => `${ a } ${ b } ` , Error ) ;
1411
1512{
16- const err = new errors . Error ( ' TEST_ERROR_1' , 'test' ) ;
13+ const err = new errors . codes . TEST_ERROR_1 ( 'test' ) ;
1714 assert ( err instanceof Error ) ;
1815 assert . strictEqual ( err . name , 'Error [TEST_ERROR_1]' ) ;
1916 assert . strictEqual ( err . message , 'Error for testing purposes: test' ) ;
2017 assert . strictEqual ( err . code , 'TEST_ERROR_1' ) ;
2118}
2219
2320{
24- const err = new errors . TypeError ( 'TEST_ERROR_1' , 'test' ) ;
21+ const err = new errors . codes . TEST_ERROR_1 . TypeError ( 'test' ) ;
2522 assert ( err instanceof TypeError ) ;
2623 assert . strictEqual ( err . name , 'TypeError [TEST_ERROR_1]' ) ;
2724 assert . strictEqual ( err . message , 'Error for testing purposes: test' ) ;
2825 assert . strictEqual ( err . code , 'TEST_ERROR_1' ) ;
2926}
3027
3128{
32- const err = new errors . RangeError ( 'TEST_ERROR_1' , 'test' ) ;
29+ const err = new errors . codes . TEST_ERROR_1 . RangeError ( 'test' ) ;
3330 assert ( err instanceof RangeError ) ;
3431 assert . strictEqual ( err . name , 'RangeError [TEST_ERROR_1]' ) ;
3532 assert . strictEqual ( err . message , 'Error for testing purposes: test' ) ;
3633 assert . strictEqual ( err . code , 'TEST_ERROR_1' ) ;
3734}
3835
3936{
40- const err = new errors . Error ( ' TEST_ERROR_2' , 'abc' , 'xyz' ) ;
37+ const err = new errors . codes . TEST_ERROR_2 ( 'abc' , 'xyz' ) ;
4138 assert ( err instanceof Error ) ;
4239 assert . strictEqual ( err . name , 'Error [TEST_ERROR_2]' ) ;
4340 assert . strictEqual ( err . message , 'abc xyz' ) ;
4441 assert . strictEqual ( err . code , 'TEST_ERROR_2' ) ;
4542}
4643
4744{
48- const err = new errors . Error ( ' TEST_ERROR_1' ) ;
45+ const err = new errors . codes . TEST_ERROR_1 ( ) ;
4946 assert ( err instanceof Error ) ;
5047 assert . strictEqual ( err . name , 'Error [TEST_ERROR_1]' ) ;
5148 assert . strictEqual ( err . message , 'Error for testing purposes: %s' ) ;
5249 assert . strictEqual ( err . code , 'TEST_ERROR_1' ) ;
5350}
5451
55- common . expectsError (
56- ( ) => new errors . Error ( 'TEST_FOO_KEY' ) ,
57- {
58- code : 'ERR_ASSERTION' ,
59- message : invalidKey ( 'TEST_FOO_KEY' )
60- } ) ;
61- // Calling it twice yields same result (using the key does not create it)
62- common . expectsError (
63- ( ) => new errors . Error ( 'TEST_FOO_KEY' ) ,
64- {
65- code : 'ERR_ASSERTION' ,
66- message : invalidKey ( 'TEST_FOO_KEY' )
67- } ) ;
68- common . expectsError (
69- ( ) => new errors . Error ( 1 ) ,
70- {
71- code : 'ERR_ASSERTION' ,
72- message : invalidKey ( 1 )
73- } ) ;
74- common . expectsError (
75- ( ) => new errors . Error ( { } ) ,
76- {
77- code : 'ERR_ASSERTION' ,
78- message : invalidKey ( '\\[object Object\\]' )
79- } ) ;
80- common . expectsError (
81- ( ) => new errors . Error ( [ ] ) ,
82- {
83- code : 'ERR_ASSERTION' ,
84- message : invalidKey ( '' )
85- } ) ;
86- common . expectsError (
87- ( ) => new errors . Error ( true ) ,
88- {
89- code : 'ERR_ASSERTION' ,
90- message : invalidKey ( 'true' )
91- } ) ;
92- common . expectsError (
93- ( ) => new errors . TypeError ( 1 ) ,
94- {
95- code : 'ERR_ASSERTION' ,
96- message : invalidKey ( 1 )
97- } ) ;
98- common . expectsError (
99- ( ) => new errors . TypeError ( { } ) ,
100- {
101- code : 'ERR_ASSERTION' ,
102- message : invalidKey ( '\\[object Object\\]' )
103- } ) ;
104- common . expectsError (
105- ( ) => new errors . TypeError ( [ ] ) ,
106- {
107- code : 'ERR_ASSERTION' ,
108- message : invalidKey ( '' )
109- } ) ;
110- common . expectsError (
111- ( ) => new errors . TypeError ( true ) ,
112- {
113- code : 'ERR_ASSERTION' ,
114- message : invalidKey ( 'true' )
115- } ) ;
116- common . expectsError (
117- ( ) => new errors . RangeError ( 1 ) ,
118- {
119- code : 'ERR_ASSERTION' ,
120- message : invalidKey ( 1 )
121- } ) ;
122- common . expectsError (
123- ( ) => new errors . RangeError ( { } ) ,
124- {
125- code : 'ERR_ASSERTION' ,
126- message : invalidKey ( '\\[object Object\\]' )
127- } ) ;
128- common . expectsError (
129- ( ) => new errors . RangeError ( [ ] ) ,
130- {
131- code : 'ERR_ASSERTION' ,
132- message : invalidKey ( '' )
133- } ) ;
134- common . expectsError (
135- ( ) => new errors . RangeError ( true ) ,
136- {
137- code : 'ERR_ASSERTION' ,
138- message : invalidKey ( 'true' )
139- } ) ;
140-
14152// Tests for common.expectsError
14253common . expectsError ( ( ) => {
143- throw new errors . TypeError ( 'TEST_ERROR_1' , 'a' ) ;
54+ throw new errors . codes . TEST_ERROR_1 . TypeError ( 'a' ) ;
14455} , { code : 'TEST_ERROR_1' } ) ;
14556common . expectsError ( ( ) => {
146- throw new errors . TypeError ( 'TEST_ERROR_1' , 'a' ) ;
57+ throw new errors . codes . TEST_ERROR_1 . TypeError ( 'a' ) ;
14758} , { code : 'TEST_ERROR_1' ,
14859 type : TypeError ,
14960 message : / ^ E r r o r f o r t e s t i n g / } ) ;
15061common . expectsError ( ( ) => {
151- throw new errors . TypeError ( 'TEST_ERROR_1' , 'a' ) ;
62+ throw new errors . codes . TEST_ERROR_1 . TypeError ( 'a' ) ;
15263} , { code : 'TEST_ERROR_1' , type : TypeError } ) ;
15364common . expectsError ( ( ) => {
154- throw new errors . TypeError ( 'TEST_ERROR_1' , 'a' ) ;
65+ throw new errors . codes . TEST_ERROR_1 . TypeError ( 'a' ) ;
15566} , {
15667 code : 'TEST_ERROR_1' ,
15768 type : TypeError ,
@@ -160,7 +71,7 @@ common.expectsError(() => {
16071
16172common . expectsError ( ( ) => {
16273 common . expectsError ( ( ) => {
163- throw new errors . TypeError ( 'TEST_ERROR_1' , 'a' ) ;
74+ throw new errors . codes . TEST_ERROR_1 . TypeError ( 'a' ) ;
16475 } , { code : 'TEST_ERROR_1' , type : RangeError } ) ;
16576} , {
16677 code : 'ERR_ASSERTION' ,
@@ -169,7 +80,7 @@ common.expectsError(() => {
16980
17081common . expectsError ( ( ) => {
17182 common . expectsError ( ( ) => {
172- throw new errors . TypeError ( 'TEST_ERROR_1' , 'a' ) ;
83+ throw new errors . codes . TEST_ERROR_1 . TypeError ( 'a' ) ;
17384 } , { code : 'TEST_ERROR_1' ,
17485 type : TypeError ,
17586 message : / ^ E r r o r f o r t e s t i n g 2 / } ) ;
@@ -318,25 +229,25 @@ assert.strictEqual(
318229
319230{
320231 const { kMaxLength } = process . binding ( 'buffer' ) ;
321- const error = new errors . Error ( ' ERR_BUFFER_TOO_LARGE' ) ;
232+ const error = new errors . codes . ERR_BUFFER_TOO_LARGE ( ) ;
322233 assert . strictEqual (
323234 error . message ,
324235 `Cannot create a Buffer larger than 0x${ kMaxLength . toString ( 16 ) } bytes`
325236 ) ;
326237}
327238
328239{
329- const error = new errors . Error ( ' ERR_INVALID_ARG_VALUE' , 'foo' , '\u0000bar' ) ;
240+ const error = new errors . codes . ERR_INVALID_ARG_VALUE ( 'foo' , '\u0000bar' ) ;
330241 assert . strictEqual (
331242 error . message ,
332243 'The argument \'foo\' is invalid. Received \'\\u0000bar\''
333244 ) ;
334245}
335246
336247{
337- const error = new errors . Error (
338- 'ERR_INVALID_ARG_VALUE' ,
339- 'foo' , { a : 1 } , 'must have property \'b\'' ) ;
248+ const error = new errors . codes . ERR_INVALID_ARG_VALUE (
249+ 'foo' , { a : 1 } , 'must have property \'b\''
250+ ) ;
340251 assert . strictEqual (
341252 error . message ,
342253 'The argument \'foo\' must have property \'b\'. Received { a: 1 }'
@@ -346,7 +257,7 @@ assert.strictEqual(
346257// Test that `code` property is mutable and that changing it does not change the
347258// name.
348259{
349- const myError = new errors . Error ( ' ERR_TLS_HANDSHAKE_TIMEOUT' ) ;
260+ const myError = new errors . codes . ERR_TLS_HANDSHAKE_TIMEOUT ( ) ;
350261 assert . strictEqual ( myError . code , 'ERR_TLS_HANDSHAKE_TIMEOUT' ) ;
351262 assert . strictEqual ( myError . hasOwnProperty ( 'code' ) , false ) ;
352263 assert . strictEqual ( myError . hasOwnProperty ( 'name' ) , false ) ;
@@ -364,7 +275,7 @@ assert.strictEqual(
364275// `console.log()` results, which is the behavior of `Error` objects in the
365276// browser. Note that `name` becomes enumerable after being assigned.
366277{
367- const myError = new errors . Error ( ' ERR_TLS_HANDSHAKE_TIMEOUT' ) ;
278+ const myError = new errors . codes . ERR_TLS_HANDSHAKE_TIMEOUT ( ) ;
368279 assert . deepStrictEqual ( Object . keys ( myError ) , [ ] ) ;
369280 const initialToString = myError . toString ( ) ;
370281
@@ -379,7 +290,7 @@ assert.strictEqual(
379290{
380291 let initialConsoleLog = '' ;
381292 common . hijackStdout ( ( data ) => { initialConsoleLog += data ; } ) ;
382- const myError = new errors . Error ( ' ERR_TLS_HANDSHAKE_TIMEOUT' ) ;
293+ const myError = new errors . codes . ERR_TLS_HANDSHAKE_TIMEOUT ( ) ;
383294 assert . deepStrictEqual ( Object . keys ( myError ) , [ ] ) ;
384295 const initialToString = myError . toString ( ) ;
385296 console . log ( myError ) ;
0 commit comments