Skip to content

Commit

Permalink
errors: port internal/fs errors to internal/errors
Browse files Browse the repository at this point in the history
* 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 Jul 19, 2017
1 parent c5521fa commit b61cab2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
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}
);

0 comments on commit b61cab2

Please sign in to comment.