2017-10-27 16:25:14 -07:00
|
|
|
// Flags: --expose-internals
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const errors = require('internal/errors');
|
2018-03-21 00:46:30 +08:00
|
|
|
const { AssertionError } = require('assert');
|
2017-10-27 16:25:14 -07:00
|
|
|
|
2018-03-21 00:46:30 +08:00
|
|
|
const { E, SystemError } = errors;
|
2017-10-27 16:25:14 -07:00
|
|
|
|
|
|
|
common.expectsError(
|
2018-03-21 00:46:30 +08:00
|
|
|
() => { throw new errors.SystemError(); },
|
2017-10-27 16:25:14 -07:00
|
|
|
{
|
2018-03-21 00:46:30 +08:00
|
|
|
code: 'ERR_ASSERTION',
|
|
|
|
type: AssertionError,
|
|
|
|
message: 'An invalid error message key was used: undefined.'
|
2017-10-27 16:25:14 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-03-21 00:46:30 +08:00
|
|
|
E('ERR_TEST', 'custom message', SystemError);
|
|
|
|
const { ERR_TEST } = errors.codes;
|
2017-10-27 16:25:14 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
2018-03-21 00:46:30 +08:00
|
|
|
code: 'ETEST',
|
|
|
|
message: 'code message',
|
|
|
|
syscall: 'syscall_test',
|
|
|
|
path: '/str',
|
|
|
|
dest: '/str2'
|
2017-10-27 16:25:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
common.expectsError(
|
2018-03-21 00:46:30 +08:00
|
|
|
() => { throw new ERR_TEST(ctx); },
|
2017-10-27 16:25:14 -07:00
|
|
|
{
|
2018-03-21 00:46:30 +08:00
|
|
|
code: 'ERR_TEST',
|
|
|
|
type: SystemError,
|
|
|
|
message: 'custom message: syscall_test returned ETEST (code message)' +
|
|
|
|
' /str => /str2',
|
|
|
|
info: ctx
|
2017-10-27 16:25:14 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
2018-03-21 00:46:30 +08:00
|
|
|
code: 'ETEST',
|
|
|
|
message: 'code message',
|
|
|
|
syscall: 'syscall_test',
|
|
|
|
path: Buffer.from('/buf'),
|
|
|
|
dest: '/str2'
|
2017-10-27 16:25:14 -07:00
|
|
|
};
|
|
|
|
common.expectsError(
|
2018-03-21 00:46:30 +08:00
|
|
|
() => { throw new ERR_TEST(ctx); },
|
2017-10-27 16:25:14 -07:00
|
|
|
{
|
2018-03-21 00:46:30 +08:00
|
|
|
code: 'ERR_TEST',
|
|
|
|
type: SystemError,
|
|
|
|
message: 'custom message: syscall_test returned ETEST (code message)' +
|
|
|
|
' /buf => /str2',
|
|
|
|
info: ctx
|
2017-10-27 16:25:14 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
2018-03-21 00:46:30 +08:00
|
|
|
code: 'ETEST',
|
|
|
|
message: 'code message',
|
|
|
|
syscall: 'syscall_test',
|
|
|
|
path: Buffer.from('/buf'),
|
|
|
|
dest: Buffer.from('/buf2')
|
2017-10-27 16:25:14 -07:00
|
|
|
};
|
|
|
|
common.expectsError(
|
2018-03-21 00:46:30 +08:00
|
|
|
() => { throw new ERR_TEST(ctx); },
|
2017-10-27 16:25:14 -07:00
|
|
|
{
|
2018-03-21 00:46:30 +08:00
|
|
|
code: 'ERR_TEST',
|
|
|
|
type: SystemError,
|
|
|
|
message: 'custom message: syscall_test returned ETEST (code message)' +
|
|
|
|
' /buf => /buf2',
|
|
|
|
info: ctx
|
2017-10-27 16:25:14 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const ctx = {
|
|
|
|
code: 'ERR',
|
|
|
|
errno: 123,
|
|
|
|
message: 'something happened',
|
2018-03-21 00:46:30 +08:00
|
|
|
syscall: 'syscall_test',
|
2017-10-27 16:25:14 -07:00
|
|
|
path: Buffer.from('a'),
|
|
|
|
dest: Buffer.from('b')
|
|
|
|
};
|
2018-03-21 00:46:30 +08:00
|
|
|
const err = new ERR_TEST(ctx);
|
2017-10-27 16:25:14 -07:00
|
|
|
assert.strictEqual(err.info, ctx);
|
2018-03-21 00:46:30 +08:00
|
|
|
assert.strictEqual(err.code, 'ERR_TEST');
|
2017-10-27 16:25:14 -07: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-21 00:46:30 +08:00
|
|
|
assert.strictEqual(err.syscall, 'syscall_test');
|
2017-10-27 16:25:14 -07: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');
|
|
|
|
}
|