Skip to content

Commit b61cab2

Browse files
gunarrefack
authored andcommitted
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 <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent c5521fa commit b61cab2

7 files changed

+53
-36
lines changed

doc/api/errors.md

+5
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,11 @@ communication channel to a child process. See [`child.send()`] and
686686
Used generically to identify when an invalid or unexpected value has been
687687
passed in an options object.
688688

689+
<a id="ERR_INVALID_OPT_VALUE_ENCODING"></a>
690+
### ERR_INVALID_OPT_VALUE_ENCODING
691+
692+
Used when an invalid or unknown file encoding is passed.
693+
689694
<a id="ERR_INVALID_REPL_EVAL_CONFIG"></a>
690695
### ERR_INVALID_REPL_EVAL_CONFIG
691696

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ E('ERR_INVALID_OPT_VALUE',
142142
(name, value) => {
143143
return `The value "${String(value)}" is invalid for option "${name}"`;
144144
});
145+
E('ERR_INVALID_OPT_VALUE_ENCODING',
146+
(value) => `The value "${String(value)}" is invalid for option "encoding"`);
145147
E('ERR_INVALID_REPL_EVAL_CONFIG',
146148
'Cannot specify both "breakEvalOnSigint" and "eval" for REPL');
147149
E('ERR_INVALID_SYNC_FORK_INPUT',

lib/internal/fs.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const Buffer = require('buffer').Buffer;
44
const Writable = require('stream').Writable;
5+
const errors = require('internal/errors');
56
const fs = require('fs');
67
const util = require('util');
78

@@ -18,16 +19,16 @@ const {
1819

1920
function assertEncoding(encoding) {
2021
if (encoding && !Buffer.isEncoding(encoding)) {
21-
throw new Error(`Unknown encoding: ${encoding}`);
22+
throw new errors.TypeError('ERR_INVALID_OPT_VALUE_ENCODING', encoding);
2223
}
2324
}
2425

25-
function stringToFlags(flag) {
26-
if (typeof flag === 'number') {
27-
return flag;
26+
function stringToFlags(flags) {
27+
if (typeof flags === 'number') {
28+
return flags;
2829
}
2930

30-
switch (flag) {
31+
switch (flags) {
3132
case 'r' : return O_RDONLY;
3233
case 'rs' : // Fall through.
3334
case 'sr' : return O_RDONLY | O_SYNC;
@@ -52,7 +53,7 @@ function stringToFlags(flag) {
5253
case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
5354
}
5455

55-
throw new Error('Unknown file open flag: ' + flag);
56+
throw new errors.TypeError('ERR_INVALID_OPT_VALUE', 'flags', flags);
5657
}
5758

5859
// Temporary hack for process.stdout and process.stderr when piped to files.

test/parallel/test-fs-assert-encoding-error.js

+21-18
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,75 @@ const assert = require('assert');
44
const fs = require('fs');
55

66
const options = 'test';
7-
const unknownEncodingMessage = /^Error: Unknown encoding: test$/;
7+
const expectedError = common.expectsError({
8+
code: 'ERR_INVALID_OPT_VALUE_ENCODING',
9+
type: TypeError,
10+
}, 17);
811

912
assert.throws(() => {
1013
fs.readFile('path', options, common.mustNotCall());
11-
}, unknownEncodingMessage);
14+
}, expectedError);
1215

1316
assert.throws(() => {
1417
fs.readFileSync('path', options);
15-
}, unknownEncodingMessage);
18+
}, expectedError);
1619

1720
assert.throws(() => {
1821
fs.readdir('path', options, common.mustNotCall());
19-
}, unknownEncodingMessage);
22+
}, expectedError);
2023

2124
assert.throws(() => {
2225
fs.readdirSync('path', options);
23-
}, unknownEncodingMessage);
26+
}, expectedError);
2427

2528
assert.throws(() => {
2629
fs.readlink('path', options, common.mustNotCall());
27-
}, unknownEncodingMessage);
30+
}, expectedError);
2831

2932
assert.throws(() => {
3033
fs.readlinkSync('path', options);
31-
}, unknownEncodingMessage);
34+
}, expectedError);
3235

3336
assert.throws(() => {
3437
fs.writeFile('path', 'data', options, common.mustNotCall());
35-
}, unknownEncodingMessage);
38+
}, expectedError);
3639

3740
assert.throws(() => {
3841
fs.writeFileSync('path', 'data', options);
39-
}, unknownEncodingMessage);
42+
}, expectedError);
4043

4144
assert.throws(() => {
4245
fs.appendFile('path', 'data', options, common.mustNotCall());
43-
}, unknownEncodingMessage);
46+
}, expectedError);
4447

4548
assert.throws(() => {
4649
fs.appendFileSync('path', 'data', options);
47-
}, unknownEncodingMessage);
50+
}, expectedError);
4851

4952
assert.throws(() => {
5053
fs.watch('path', options, common.mustNotCall());
51-
}, unknownEncodingMessage);
54+
}, expectedError);
5255

5356
assert.throws(() => {
5457
fs.realpath('path', options, common.mustNotCall());
55-
}, unknownEncodingMessage);
58+
}, expectedError);
5659

5760
assert.throws(() => {
5861
fs.realpathSync('path', options);
59-
}, unknownEncodingMessage);
62+
}, expectedError);
6063

6164
assert.throws(() => {
6265
fs.mkdtemp('path', options, common.mustNotCall());
63-
}, unknownEncodingMessage);
66+
}, expectedError);
6467

6568
assert.throws(() => {
6669
fs.mkdtempSync('path', options);
67-
}, unknownEncodingMessage);
70+
}, expectedError);
6871

6972
assert.throws(() => {
7073
fs.ReadStream('path', options);
71-
}, unknownEncodingMessage);
74+
}, expectedError);
7275

7376
assert.throws(() => {
7477
fs.WriteStream('path', options);
75-
}, unknownEncodingMessage);
78+
}, expectedError);

test/parallel/test-fs-open-flags.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
// Flags: --expose_internals
2323
'use strict';
24-
require('../common');
24+
const common = require('../common');
2525
const assert = require('assert');
2626

2727
const fs = require('fs');
@@ -55,30 +55,29 @@ assert.strictEqual(stringToFlags('xa'), O_APPEND | O_CREAT | O_WRONLY | O_EXCL);
5555
assert.strictEqual(stringToFlags('ax+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
5656
assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
5757

58+
const expectedError =
59+
common.expectsError({code: 'ERR_INVALID_OPT_VALUE', type: TypeError}, 23);
60+
5861
('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
5962
.split(' ')
6063
.forEach(function(flags) {
6164
assert.throws(
6265
() => stringToFlags(flags),
63-
new RegExp(`^Error: Unknown file open flag: ${escapeRegExp(flags)}`)
66+
expectedError
6467
);
6568
});
6669

6770
assert.throws(
6871
() => stringToFlags({}),
69-
/^Error: Unknown file open flag: \[object Object\]$/
72+
expectedError
7073
);
7174

7275
assert.throws(
7376
() => stringToFlags(true),
74-
/^Error: Unknown file open flag: true$/
77+
expectedError
7578
);
7679

7780
assert.throws(
7881
() => stringToFlags(null),
79-
/^Error: Unknown file open flag: null$/
82+
expectedError
8083
);
81-
82-
function escapeRegExp(string) {
83-
return string.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
84-
}

test/parallel/test-fs-read-file-assert-encoding.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ const fs = require('fs');
66

77
const encoding = 'foo-8';
88
const filename = 'bar.txt';
9+
const expectedError = common.expectsError({
10+
code: 'ERR_INVALID_OPT_VALUE_ENCODING',
11+
type: TypeError,
12+
});
913

1014
assert.throws(
1115
fs.readFile.bind(fs, filename, { encoding }, common.mustNotCall()),
12-
new RegExp(`^Error: Unknown encoding: ${encoding}$`)
16+
expectedError
1317
);

test/parallel/test-internal-fs.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Flags: --expose-internals
22
'use strict';
33

4-
require('../common');
4+
const common = require('../common');
55
const assert = require('assert');
66
const fs = require('internal/fs');
77

88
assert.doesNotThrow(() => fs.assertEncoding());
99
assert.doesNotThrow(() => fs.assertEncoding('utf8'));
10-
assert.throws(() => fs.assertEncoding('foo'), /^Error: Unknown encoding: foo$/);
10+
common.expectsError(
11+
() => fs.assertEncoding('foo'),
12+
{code: 'ERR_INVALID_OPT_VALUE_ENCODING', type: TypeError}
13+
);

0 commit comments

Comments
 (0)