nodejs/test/parallel/test-fs-close-errors.js
Joyee Cheung 9f122e3b55
fs: throw fs.close errors in JS
* Collect the error context in both JS and C++, then throw
  the error in JS
* Test that the errors thrown from fs.close and fs.closeSync
  includes the correct error code, error number and syscall
  properties

PR-URL: https://github.com/nodejs/node/pull/17338
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2017-12-28 03:08:25 +08:00

67 lines
1.6 KiB
JavaScript

'use strict';
// This tests that the errors thrown from fs.close and fs.closeSync
// include the desired properties
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const uv = process.binding('uv');
['', false, null, undefined, {}, []].forEach((i) => {
common.expectsError(
() => fs.close(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
}
);
common.expectsError(
() => fs.closeSync(i),
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "fd" argument must be of type integer'
}
);
});
{
assert.throws(
() => {
const fd = fs.openSync(__filename, 'r');
fs.closeSync(fd);
fs.closeSync(fd);
},
(err) => {
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(
err.message,
'EBADF: bad file descriptor, close'
);
assert.strictEqual(err.constructor, Error);
assert.strictEqual(err.syscall, 'close');
assert.strictEqual(err.errno, uv.UV_EBADF);
return true;
}
);
}
{
const fd = fs.openSync(__filename, 'r');
fs.close(fd, common.mustCall((err) => {
assert.ifError(err);
fs.close(fd, common.mustCall((err) => {
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(
err.message,
'EBADF: bad file descriptor, close'
);
assert.strictEqual(err.constructor, Error);
assert.strictEqual(err.syscall, 'close');
assert.strictEqual(err.errno, uv.UV_EBADF);
}));
}));
}