1// Flags: --expose-internals 2'use strict'; 3 4require('../common'); 5const assert = require('assert'); 6const { E, SystemError, codes } = require('internal/errors'); 7 8assert.throws( 9 () => { new SystemError(); }, 10 { 11 name: 'TypeError', 12 message: 'String.prototype.match called on null or undefined' 13 } 14); 15 16E('ERR_TEST', 'custom message', SystemError); 17const { ERR_TEST } = codes; 18 19{ 20 const ctx = { 21 code: 'ETEST', 22 message: 'code message', 23 syscall: 'syscall_test', 24 path: '/str', 25 dest: '/str2' 26 }; 27 28 assert.throws( 29 () => { throw new ERR_TEST(ctx); }, 30 { 31 code: 'ERR_TEST', 32 name: 'SystemError', 33 message: 'custom message: syscall_test returned ETEST (code message)' + 34 ' /str => /str2', 35 info: ctx 36 } 37 ); 38} 39 40{ 41 const ctx = { 42 code: 'ETEST', 43 message: 'code message', 44 syscall: 'syscall_test', 45 path: Buffer.from('/buf'), 46 dest: '/str2' 47 }; 48 assert.throws( 49 () => { throw new ERR_TEST(ctx); }, 50 { 51 code: 'ERR_TEST', 52 name: 'SystemError', 53 message: 'custom message: syscall_test returned ETEST (code message)' + 54 ' /buf => /str2', 55 info: ctx 56 } 57 ); 58} 59 60{ 61 const ctx = { 62 code: 'ETEST', 63 message: 'code message', 64 syscall: 'syscall_test', 65 path: Buffer.from('/buf'), 66 dest: Buffer.from('/buf2') 67 }; 68 assert.throws( 69 () => { throw new ERR_TEST(ctx); }, 70 { 71 code: 'ERR_TEST', 72 name: 'SystemError', 73 message: 'custom message: syscall_test returned ETEST (code message)' + 74 ' /buf => /buf2', 75 info: ctx 76 } 77 ); 78} 79 80{ 81 const ctx = { 82 code: 'ERR', 83 errno: 123, 84 message: 'something happened', 85 syscall: 'syscall_test', 86 path: Buffer.from('a'), 87 dest: Buffer.from('b') 88 }; 89 const err = new ERR_TEST(ctx); 90 assert.strictEqual(err.info, ctx); 91 assert.strictEqual(err.code, 'ERR_TEST'); 92 err.code = 'test'; 93 assert.strictEqual(err.code, 'test'); 94 95 // Test legacy properties. These shouldn't be used anymore 96 // but let us make sure they still work 97 assert.strictEqual(err.errno, 123); 98 assert.strictEqual(err.syscall, 'syscall_test'); 99 assert.strictEqual(err.path, 'a'); 100 assert.strictEqual(err.dest, 'b'); 101 102 // Make sure it's mutable 103 err.code = 'test'; 104 err.errno = 321; 105 err.syscall = 'test'; 106 err.path = 'path'; 107 err.dest = 'path'; 108 109 assert.strictEqual(err.errno, 321); 110 assert.strictEqual(err.syscall, 'test'); 111 assert.strictEqual(err.path, 'path'); 112 assert.strictEqual(err.dest, 'path'); 113} 114 115{ 116 const ctx = { 117 code: 'ERR_TEST', 118 message: 'Error occurred', 119 syscall: 'syscall_test' 120 }; 121 assert.throws( 122 () => { 123 const err = new ERR_TEST(ctx); 124 err.name = 'Foobar'; 125 throw err; 126 }, 127 { 128 code: 'ERR_TEST', 129 name: 'Foobar', 130 message: 'custom message: syscall_test returned ERR_TEST ' + 131 '(Error occurred)', 132 info: ctx 133 } 134 ); 135} 136