errors: port internal/fs errors to internal/errors
* Assign codes to errors reported by internal/fs.js PR-URL: https://github.com/nodejs/node/pull/11317 Refs: https://github.com/nodejs/node/issues/11273 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
This commit is contained in:
parent
c5521fa617
commit
b61cab2234
@ -686,6 +686,11 @@ communication channel to a child process. See [`child.send()`] and
|
|||||||
Used generically to identify when an invalid or unexpected value has been
|
Used generically to identify when an invalid or unexpected value has been
|
||||||
passed in an options object.
|
passed in an options object.
|
||||||
|
|
||||||
|
<a id="ERR_INVALID_OPT_VALUE_ENCODING"></a>
|
||||||
|
### ERR_INVALID_OPT_VALUE_ENCODING
|
||||||
|
|
||||||
|
Used when an invalid or unknown file encoding is passed.
|
||||||
|
|
||||||
<a id="ERR_INVALID_REPL_EVAL_CONFIG"></a>
|
<a id="ERR_INVALID_REPL_EVAL_CONFIG"></a>
|
||||||
### ERR_INVALID_REPL_EVAL_CONFIG
|
### ERR_INVALID_REPL_EVAL_CONFIG
|
||||||
|
|
||||||
|
@ -142,6 +142,8 @@ E('ERR_INVALID_OPT_VALUE',
|
|||||||
(name, value) => {
|
(name, value) => {
|
||||||
return `The value "${String(value)}" is invalid for option "${name}"`;
|
return `The value "${String(value)}" is invalid for option "${name}"`;
|
||||||
});
|
});
|
||||||
|
E('ERR_INVALID_OPT_VALUE_ENCODING',
|
||||||
|
(value) => `The value "${String(value)}" is invalid for option "encoding"`);
|
||||||
E('ERR_INVALID_REPL_EVAL_CONFIG',
|
E('ERR_INVALID_REPL_EVAL_CONFIG',
|
||||||
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL');
|
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL');
|
||||||
E('ERR_INVALID_SYNC_FORK_INPUT',
|
E('ERR_INVALID_SYNC_FORK_INPUT',
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const Buffer = require('buffer').Buffer;
|
const Buffer = require('buffer').Buffer;
|
||||||
const Writable = require('stream').Writable;
|
const Writable = require('stream').Writable;
|
||||||
|
const errors = require('internal/errors');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
|
|
||||||
@ -18,16 +19,16 @@ const {
|
|||||||
|
|
||||||
function assertEncoding(encoding) {
|
function assertEncoding(encoding) {
|
||||||
if (encoding && !Buffer.isEncoding(encoding)) {
|
if (encoding && !Buffer.isEncoding(encoding)) {
|
||||||
throw new Error(`Unknown encoding: ${encoding}`);
|
throw new errors.TypeError('ERR_INVALID_OPT_VALUE_ENCODING', encoding);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function stringToFlags(flag) {
|
function stringToFlags(flags) {
|
||||||
if (typeof flag === 'number') {
|
if (typeof flags === 'number') {
|
||||||
return flag;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (flag) {
|
switch (flags) {
|
||||||
case 'r' : return O_RDONLY;
|
case 'r' : return O_RDONLY;
|
||||||
case 'rs' : // Fall through.
|
case 'rs' : // Fall through.
|
||||||
case 'sr' : return O_RDONLY | O_SYNC;
|
case 'sr' : return O_RDONLY | O_SYNC;
|
||||||
@ -52,7 +53,7 @@ function stringToFlags(flag) {
|
|||||||
case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
|
case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error('Unknown file open flag: ' + flag);
|
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'flags', flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Temporary hack for process.stdout and process.stderr when piped to files.
|
// Temporary hack for process.stdout and process.stderr when piped to files.
|
||||||
|
@ -4,72 +4,75 @@ const assert = require('assert');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const options = 'test';
|
const options = 'test';
|
||||||
const unknownEncodingMessage = /^Error: Unknown encoding: test$/;
|
const expectedError = common.expectsError({
|
||||||
|
code: 'ERR_INVALID_OPT_VALUE_ENCODING',
|
||||||
|
type: TypeError,
|
||||||
|
}, 17);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.readFile('path', options, common.mustNotCall());
|
fs.readFile('path', options, common.mustNotCall());
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.readFileSync('path', options);
|
fs.readFileSync('path', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.readdir('path', options, common.mustNotCall());
|
fs.readdir('path', options, common.mustNotCall());
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.readdirSync('path', options);
|
fs.readdirSync('path', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.readlink('path', options, common.mustNotCall());
|
fs.readlink('path', options, common.mustNotCall());
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.readlinkSync('path', options);
|
fs.readlinkSync('path', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.writeFile('path', 'data', options, common.mustNotCall());
|
fs.writeFile('path', 'data', options, common.mustNotCall());
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.writeFileSync('path', 'data', options);
|
fs.writeFileSync('path', 'data', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.appendFile('path', 'data', options, common.mustNotCall());
|
fs.appendFile('path', 'data', options, common.mustNotCall());
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.appendFileSync('path', 'data', options);
|
fs.appendFileSync('path', 'data', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.watch('path', options, common.mustNotCall());
|
fs.watch('path', options, common.mustNotCall());
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.realpath('path', options, common.mustNotCall());
|
fs.realpath('path', options, common.mustNotCall());
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.realpathSync('path', options);
|
fs.realpathSync('path', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.mkdtemp('path', options, common.mustNotCall());
|
fs.mkdtemp('path', options, common.mustNotCall());
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.mkdtempSync('path', options);
|
fs.mkdtempSync('path', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.ReadStream('path', options);
|
fs.ReadStream('path', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
fs.WriteStream('path', options);
|
fs.WriteStream('path', options);
|
||||||
}, unknownEncodingMessage);
|
}, expectedError);
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
// Flags: --expose_internals
|
// Flags: --expose_internals
|
||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
@ -55,30 +55,29 @@ assert.strictEqual(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
|
|||||||
assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
|
assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
|
||||||
assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
|
assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
|
||||||
|
|
||||||
|
const expectedError =
|
||||||
|
common.expectsError({code: 'ERR_INVALID_OPT_VALUE', type: TypeError}, 23);
|
||||||
|
|
||||||
('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
|
('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
|
||||||
.split(' ')
|
.split(' ')
|
||||||
.forEach(function(flags) {
|
.forEach(function(flags) {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => stringToFlags(flags),
|
() => stringToFlags(flags),
|
||||||
new RegExp(`^Error: Unknown file open flag: ${escapeRegExp(flags)}`)
|
expectedError
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => stringToFlags({}),
|
() => stringToFlags({}),
|
||||||
/^Error: Unknown file open flag: \[object Object\]$/
|
expectedError
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => stringToFlags(true),
|
() => stringToFlags(true),
|
||||||
/^Error: Unknown file open flag: true$/
|
expectedError
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => stringToFlags(null),
|
() => stringToFlags(null),
|
||||||
/^Error: Unknown file open flag: null$/
|
expectedError
|
||||||
);
|
);
|
||||||
|
|
||||||
function escapeRegExp(string) {
|
|
||||||
return string.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
||||||
}
|
|
||||||
|
@ -6,8 +6,12 @@ const fs = require('fs');
|
|||||||
|
|
||||||
const encoding = 'foo-8';
|
const encoding = 'foo-8';
|
||||||
const filename = 'bar.txt';
|
const filename = 'bar.txt';
|
||||||
|
const expectedError = common.expectsError({
|
||||||
|
code: 'ERR_INVALID_OPT_VALUE_ENCODING',
|
||||||
|
type: TypeError,
|
||||||
|
});
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
fs.readFile.bind(fs, filename, { encoding }, common.mustNotCall()),
|
fs.readFile.bind(fs, filename, { encoding }, common.mustNotCall()),
|
||||||
new RegExp(`^Error: Unknown encoding: ${encoding}$`)
|
expectedError
|
||||||
);
|
);
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
// Flags: --expose-internals
|
// Flags: --expose-internals
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const fs = require('internal/fs');
|
const fs = require('internal/fs');
|
||||||
|
|
||||||
assert.doesNotThrow(() => fs.assertEncoding());
|
assert.doesNotThrow(() => fs.assertEncoding());
|
||||||
assert.doesNotThrow(() => fs.assertEncoding('utf8'));
|
assert.doesNotThrow(() => fs.assertEncoding('utf8'));
|
||||||
assert.throws(() => fs.assertEncoding('foo'), /^Error: Unknown encoding: foo$/);
|
common.expectsError(
|
||||||
|
() => fs.assertEncoding('foo'),
|
||||||
|
{code: 'ERR_INVALID_OPT_VALUE_ENCODING', type: TypeError}
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user