Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: port internal/fs errors to internal/errors #11317

Merged
merged 1 commit into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 7 additions & 6 deletions lib/internal/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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;
Expand All @@ -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.
Expand Down
39 changes: 21 additions & 18 deletions test/parallel/test-fs-assert-encoding-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
17 changes: 8 additions & 9 deletions test/parallel/test-fs-open-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

// Flags: --expose_internals
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

const fs = require('fs');
Expand Down Expand Up @@ -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 changes: 5 additions & 1 deletion test/parallel/test-fs-read-file-assert-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
7 changes: 5 additions & 2 deletions test/parallel/test-internal-fs.js
Original file line number Diff line number Diff line change
@@ -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}
);