node/test/parallel/test-errors-systemerror.js
James M Snell c3dc0e0d75 src: add CollectExceptionInfo & errors.SystemError
Preparing for the migration of existing UVException and ErrnoExceptions
from the native layer, add new `errors.SystemError` to internal/errors
and new `env->CollectExceptionInfo()` / `env->CollectUVExceptionInfo()`
methods.

PR-URL: https://github.com/nodejs/node/pull/16567
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-11-02 11:58:38 -07:00

188 lines
3.8 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const errors = require('internal/errors');
common.expectsError(
() => { throw new errors.SystemError(); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred'
}
);
common.expectsError(
() => { throw new errors.SystemError({}); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred'
}
);
common.expectsError(
() => { throw new errors.SystemError(null); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred'
}
);
common.expectsError(
() => { throw new errors.SystemError({ code: 'ERR' }); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred: ERR'
}
);
{
const ctx = {
code: 'ERR',
syscall: 'foo'
};
common.expectsError(
() => { throw new errors.SystemError(ctx); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred: ERR [foo]'
}
);
}
{
const ctx = {
code: 'ERR',
syscall: 'foo',
path: Buffer.from('a')
};
common.expectsError(
() => { throw new errors.SystemError(ctx); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred: ERR [foo]: a'
}
);
}
{
const ctx = {
code: 'ERR',
syscall: 'foo',
path: Buffer.from('a'),
dest: Buffer.from('b')
};
common.expectsError(
() => { throw new errors.SystemError(ctx); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred: ERR [foo]: a => b'
}
);
}
{
const ctx = {
syscall: 'foo',
path: Buffer.from('a'),
dest: Buffer.from('b')
};
common.expectsError(
() => { throw new errors.SystemError(ctx); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred: [foo]: a => b'
}
);
}
{
const ctx = {
path: Buffer.from('a'),
dest: Buffer.from('b')
};
common.expectsError(
() => { throw new errors.SystemError(ctx); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'A system error occurred: a => b'
}
);
}
{
const ctx = {
code: 'ERR',
message: 'something happened',
syscall: 'foo',
path: Buffer.from('a'),
dest: Buffer.from('b')
};
common.expectsError(
() => { throw new errors.SystemError(ctx); },
{
code: 'ERR_SYSTEM_ERROR',
type: errors.SystemError,
message: 'something happened: ERR [foo]: a => b'
}
);
}
{
const ctx = {
code: 'ERR_ASSERTION'
};
common.expectsError(
() => { throw new errors.SystemError(ctx); },
{
code: 'ERR_ASSERTION',
type: errors.SystemError
}
);
}
{
const ctx = {
code: 'ERR',
errno: 123,
message: 'something happened',
syscall: 'foo',
path: Buffer.from('a'),
dest: Buffer.from('b')
};
const err = new errors.SystemError(ctx);
assert.strictEqual(err.info, ctx);
assert.strictEqual(err.code, 'ERR_SYSTEM_ERROR');
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, 'foo');
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');
}