node/test/parallel/test-errors-systemerror.js
Ruben Bridgewater dca7fb2225
errors: validate input arguments
This makes sure the input arguments get validated so implementation
errors will be caught early. It also improves a couple of error
messages by providing more detailed information and fixes errors
detected by the new functionality. Besides that a error type got
simplified and tests got refactored.

PR-URL: https://github.com/nodejs/node/pull/19924
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2018-04-13 19:59:44 +02:00

115 lines
2.5 KiB
JavaScript

// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const errors = require('internal/errors');
const { E, SystemError } = errors;
assert.throws(
() => { throw new errors.SystemError(); },
{
name: 'TypeError',
message: 'Cannot read property \'match\' of undefined'
}
);
E('ERR_TEST', 'custom message', SystemError);
const { ERR_TEST } = errors.codes;
{
const ctx = {
code: 'ETEST',
message: 'code message',
syscall: 'syscall_test',
path: '/str',
dest: '/str2'
};
assert.throws(
() => { throw new ERR_TEST(ctx); },
{
code: 'ERR_TEST',
name: 'SystemError [ERR_TEST]',
message: 'custom message: syscall_test returned ETEST (code message)' +
' /str => /str2',
info: ctx
}
);
}
{
const ctx = {
code: 'ETEST',
message: 'code message',
syscall: 'syscall_test',
path: Buffer.from('/buf'),
dest: '/str2'
};
assert.throws(
() => { throw new ERR_TEST(ctx); },
{
code: 'ERR_TEST',
name: 'SystemError [ERR_TEST]',
message: 'custom message: syscall_test returned ETEST (code message)' +
' /buf => /str2',
info: ctx
}
);
}
{
const ctx = {
code: 'ETEST',
message: 'code message',
syscall: 'syscall_test',
path: Buffer.from('/buf'),
dest: Buffer.from('/buf2')
};
assert.throws(
() => { throw new ERR_TEST(ctx); },
{
code: 'ERR_TEST',
name: 'SystemError [ERR_TEST]',
message: 'custom message: syscall_test returned ETEST (code message)' +
' /buf => /buf2',
info: ctx
}
);
}
{
const ctx = {
code: 'ERR',
errno: 123,
message: 'something happened',
syscall: 'syscall_test',
path: Buffer.from('a'),
dest: Buffer.from('b')
};
const err = new ERR_TEST(ctx);
assert.strictEqual(err.info, ctx);
assert.strictEqual(err.code, 'ERR_TEST');
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);
assert.strictEqual(err.syscall, 'syscall_test');
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');
}