2017-10-27 23:25:14 +00:00
|
|
|
// Flags: --expose-internals
|
|
|
|
'use strict';
|
|
|
|
|
2018-04-11 01:10:22 +00:00
|
|
|
require('../common');
|
2017-10-27 23:25:14 +00:00
|
|
|
const assert = require('assert');
|
2018-04-26 17:12:47 +00:00
|
|
|
const { E, SystemError, codes } = require('internal/errors');
|
2017-10-27 23:25:14 +00:00
|
|
|
|
2018-04-11 01:10:22 +00:00
|
|
|
assert.throws(
|
2019-03-16 11:09:14 +00:00
|
|
|
() => { new SystemError(); },
|
2017-10-27 23:25:14 +00:00
|
|
|
{
|
2018-04-11 01:10:22 +00:00
|
|
|
name: 'TypeError',
|
2022-06-27 15:16:06 +00:00
|
|
|
message: "Cannot read properties of undefined (reading 'syscall')",
|
2017-10-27 23:25:14 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-03-20 16:46:30 +00:00
|
|
|
E('ERR_TEST', 'custom message', SystemError);
|
2018-04-26 17:12:47 +00:00
|
|
|
const { ERR_TEST } = codes;
|
2017-10-27 23:25:14 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
2018-03-20 16:46:30 +00:00
|
|
|
code: 'ETEST',
|
|
|
|
message: 'code message',
|
|
|
|
syscall: 'syscall_test',
|
|
|
|
path: '/str',
|
|
|
|
dest: '/str2'
|
2017-10-27 23:25:14 +00:00
|
|
|
};
|
|
|
|
|
2018-04-11 01:10:22 +00:00
|
|
|
assert.throws(
|
2018-03-20 16:46:30 +00:00
|
|
|
() => { throw new ERR_TEST(ctx); },
|
2017-10-27 23:25:14 +00:00
|
|
|
{
|
2018-03-20 16:46:30 +00:00
|
|
|
code: 'ERR_TEST',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'SystemError',
|
2018-03-20 16:46:30 +00:00
|
|
|
message: 'custom message: syscall_test returned ETEST (code message)' +
|
|
|
|
' /str => /str2',
|
|
|
|
info: ctx
|
2017-10-27 23:25:14 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
2018-03-20 16:46:30 +00:00
|
|
|
code: 'ETEST',
|
|
|
|
message: 'code message',
|
|
|
|
syscall: 'syscall_test',
|
|
|
|
path: Buffer.from('/buf'),
|
|
|
|
dest: '/str2'
|
2017-10-27 23:25:14 +00:00
|
|
|
};
|
2018-04-11 01:10:22 +00:00
|
|
|
assert.throws(
|
2018-03-20 16:46:30 +00:00
|
|
|
() => { throw new ERR_TEST(ctx); },
|
2017-10-27 23:25:14 +00:00
|
|
|
{
|
2018-03-20 16:46:30 +00:00
|
|
|
code: 'ERR_TEST',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'SystemError',
|
2018-03-20 16:46:30 +00:00
|
|
|
message: 'custom message: syscall_test returned ETEST (code message)' +
|
|
|
|
' /buf => /str2',
|
|
|
|
info: ctx
|
2017-10-27 23:25:14 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
2018-03-20 16:46:30 +00:00
|
|
|
code: 'ETEST',
|
|
|
|
message: 'code message',
|
|
|
|
syscall: 'syscall_test',
|
|
|
|
path: Buffer.from('/buf'),
|
|
|
|
dest: Buffer.from('/buf2')
|
2017-10-27 23:25:14 +00:00
|
|
|
};
|
2018-04-11 01:10:22 +00:00
|
|
|
assert.throws(
|
2018-03-20 16:46:30 +00:00
|
|
|
() => { throw new ERR_TEST(ctx); },
|
2017-10-27 23:25:14 +00:00
|
|
|
{
|
2018-03-20 16:46:30 +00:00
|
|
|
code: 'ERR_TEST',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'SystemError',
|
2018-03-20 16:46:30 +00:00
|
|
|
message: 'custom message: syscall_test returned ETEST (code message)' +
|
|
|
|
' /buf => /buf2',
|
|
|
|
info: ctx
|
2017-10-27 23:25:14 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
|
|
|
code: 'ERR',
|
|
|
|
errno: 123,
|
|
|
|
message: 'something happened',
|
2018-03-20 16:46:30 +00:00
|
|
|
syscall: 'syscall_test',
|
2017-10-27 23:25:14 +00:00
|
|
|
path: Buffer.from('a'),
|
|
|
|
dest: Buffer.from('b')
|
|
|
|
};
|
2018-03-20 16:46:30 +00:00
|
|
|
const err = new ERR_TEST(ctx);
|
2017-10-27 23:25:14 +00:00
|
|
|
assert.strictEqual(err.info, ctx);
|
2018-03-20 16:46:30 +00:00
|
|
|
assert.strictEqual(err.code, 'ERR_TEST');
|
2017-10-27 23:25:14 +00:00
|
|
|
err.code = 'test';
|
|
|
|
assert.strictEqual(err.code, 'test');
|
|
|
|
|
|
|
|
// Test legacy properties. These shouldn't be used anymore
|
|
|
|
// but let us make sure they still work
|
|
|
|
assert.strictEqual(err.errno, 123);
|
2018-03-20 16:46:30 +00:00
|
|
|
assert.strictEqual(err.syscall, 'syscall_test');
|
2017-10-27 23:25:14 +00:00
|
|
|
assert.strictEqual(err.path, 'a');
|
|
|
|
assert.strictEqual(err.dest, 'b');
|
|
|
|
|
|
|
|
// Make sure it's mutable
|
|
|
|
err.code = 'test';
|
|
|
|
err.errno = 321;
|
|
|
|
err.syscall = 'test';
|
|
|
|
err.path = 'path';
|
|
|
|
err.dest = 'path';
|
|
|
|
|
|
|
|
assert.strictEqual(err.errno, 321);
|
|
|
|
assert.strictEqual(err.syscall, 'test');
|
|
|
|
assert.strictEqual(err.path, 'path');
|
|
|
|
assert.strictEqual(err.dest, 'path');
|
|
|
|
}
|
2018-11-06 17:46:28 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
|
|
|
code: 'ERR_TEST',
|
|
|
|
message: 'Error occurred',
|
|
|
|
syscall: 'syscall_test'
|
|
|
|
};
|
|
|
|
assert.throws(
|
|
|
|
() => {
|
|
|
|
const err = new ERR_TEST(ctx);
|
2019-03-16 11:09:14 +00:00
|
|
|
err.name = 'Foobar';
|
2018-11-06 17:46:28 +00:00
|
|
|
throw err;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'ERR_TEST',
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'Foobar',
|
2018-11-06 17:46:28 +00:00
|
|
|
message: 'custom message: syscall_test returned ERR_TEST ' +
|
|
|
|
'(Error occurred)',
|
|
|
|
info: ctx
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|