Permalink
Browse files

errors: port internal/fs errors to internal/errors

* Assign codes to errors reported by internal/fs.js

PR-URL: #11317
Refs: #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>
  • Loading branch information...
gunar authored and refack committed Feb 12, 2017
1 parent c5521fa commit b61cab2234b3d9bf7265980f9286a136af20365b
View
@@ -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
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>
### ERR_INVALID_REPL_EVAL_CONFIG
View
@@ -142,6 +142,8 @@ E('ERR_INVALID_OPT_VALUE',
(name, value) => {
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',
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL');
E('ERR_INVALID_SYNC_FORK_INPUT',
View
@@ -2,6 +2,7 @@
const Buffer = require('buffer').Buffer;
const Writable = require('stream').Writable;
const errors = require('internal/errors');
const fs = require('fs');
const util = require('util');
@@ -18,16 +19,16 @@ const {
function assertEncoding(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) {
if (typeof flag === 'number') {
return flag;
function stringToFlags(flags) {
if (typeof flags === 'number') {
return flags;
}
switch (flag) {
switch (flags) {
case 'r' : return O_RDONLY;
case 'rs' : // Fall through.
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;
}
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.
@@ -4,72 +4,75 @@ const assert = require('assert');
const fs = require('fs');
const options = 'test';
const unknownEncodingMessage = /^Error: Unknown encoding: test$/;
const expectedError = common.expectsError({
code: 'ERR_INVALID_OPT_VALUE_ENCODING',
type: TypeError,
}, 17);
assert.throws(() => {
fs.readFile('path', options, common.mustNotCall());
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.readFileSync('path', options);
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.readdir('path', options, common.mustNotCall());
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.readdirSync('path', options);
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.readlink('path', options, common.mustNotCall());
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.readlinkSync('path', options);
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.writeFile('path', 'data', options, common.mustNotCall());
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.writeFileSync('path', 'data', options);
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.appendFile('path', 'data', options, common.mustNotCall());
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.appendFileSync('path', 'data', options);
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.watch('path', options, common.mustNotCall());
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.realpath('path', options, common.mustNotCall());
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.realpathSync('path', options);
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.mkdtemp('path', options, common.mustNotCall());
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.mkdtempSync('path', options);
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.ReadStream('path', options);
}, unknownEncodingMessage);
}, expectedError);
assert.throws(() => {
fs.WriteStream('path', options);
}, unknownEncodingMessage);
}, expectedError);
@@ -21,7 +21,7 @@
// Flags: --expose_internals
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
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('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')
.split(' ')
.forEach(function(flags) {
assert.throws(
() => stringToFlags(flags),
new RegExp(`^Error: Unknown file open flag: ${escapeRegExp(flags)}`)
expectedError
);
});
assert.throws(
() => stringToFlags({}),
/^Error: Unknown file open flag: \[object Object\]$/
expectedError
);
assert.throws(
() => stringToFlags(true),
/^Error: Unknown file open flag: true$/
expectedError
);
assert.throws(
() => 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 filename = 'bar.txt';
const expectedError = common.expectsError({
code: 'ERR_INVALID_OPT_VALUE_ENCODING',
type: TypeError,
});
assert.throws(
fs.readFile.bind(fs, filename, { encoding }, common.mustNotCall()),
new RegExp(`^Error: Unknown encoding: ${encoding}$`)
expectedError
);
@@ -1,10 +1,13 @@
// Flags: --expose-internals
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const fs = require('internal/fs');
assert.doesNotThrow(() => fs.assertEncoding());
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}
);

0 comments on commit b61cab2

Please sign in to comment.