Skip to content

Commit 8e2b093

Browse files
authored
url: add err.input to ERR_INVALID_FILE_URL_PATH
Otherwise there's no information from the error about what exactly is the invalid URL. PR-URL: #59730 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent fdef072 commit 8e2b093

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

doc/api/errors.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,9 @@ A Node.js API that consumes `file:` URLs (such as certain functions in the
19961996
[`fs`][] module) encountered a file URL with an incompatible path. The exact
19971997
semantics for determining whether a path can be used is platform-dependent.
19981998

1999+
The thrown error object includes an `input` property that contains the URL object
2000+
of the invalid `file:` URL.
2001+
19992002
<a id="ERR_INVALID_HANDLE_TYPE"></a>
20002003

20012004
### `ERR_INVALID_HANDLE_TYPE`

lib/internal/errors.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,10 @@ E('ERR_INVALID_FD',
14891489
E('ERR_INVALID_FD_TYPE', 'Unsupported fd type: %s', TypeError);
14901490
E('ERR_INVALID_FILE_URL_HOST',
14911491
'File URL host must be "localhost" or empty on %s', TypeError);
1492-
E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s', TypeError);
1492+
E('ERR_INVALID_FILE_URL_PATH', function(reason, input) {
1493+
this.input = input;
1494+
return `File URL path ${reason}`;
1495+
}, TypeError);
14931496
E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent', TypeError);
14941497
E('ERR_INVALID_HTTP_TOKEN', '%s must be a valid HTTP token ["%s"]', TypeError, HideStackFramesError);
14951498
E('ERR_INVALID_IP_ADDRESS', 'Invalid IP address: %s', TypeError);

lib/internal/url.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ function getPathFromURLWin32(url) {
14631463
if ((pathname[n + 1] === '2' && third === 102) || // 2f 2F /
14641464
(pathname[n + 1] === '5' && third === 99)) { // 5c 5C \
14651465
throw new ERR_INVALID_FILE_URL_PATH(
1466-
'must not include encoded \\ or / characters',
1466+
'must not include encoded \\ or / characters', url,
14671467
);
14681468
}
14691469
}
@@ -1484,7 +1484,7 @@ function getPathFromURLWin32(url) {
14841484
const sep = StringPrototypeCharAt(pathname, 2);
14851485
if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || // a..z A..Z
14861486
(sep !== ':')) {
1487-
throw new ERR_INVALID_FILE_URL_PATH('must be absolute');
1487+
throw new ERR_INVALID_FILE_URL_PATH('must be absolute', url);
14881488
}
14891489
return StringPrototypeSlice(pathname, 1);
14901490
}
@@ -1551,7 +1551,7 @@ function getPathBufferFromURLWin32(url) {
15511551

15521552
if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || // a..z A..Z
15531553
(sep !== CHAR_COLON)) {
1554-
throw new ERR_INVALID_FILE_URL_PATH('must be absolute');
1554+
throw new ERR_INVALID_FILE_URL_PATH('must be absolute', url);
15551555
}
15561556

15571557
// Now, we'll just return everything except the first byte of
@@ -1570,6 +1570,7 @@ function getPathFromURLPosix(url) {
15701570
if (pathname[n + 1] === '2' && third === 102) {
15711571
throw new ERR_INVALID_FILE_URL_PATH(
15721572
'must not include encoded / characters',
1573+
url,
15731574
);
15741575
}
15751576
}

test/parallel/test-url-fileurltopath.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,6 @@ test('fileURLToPath with host', () => {
3131
}
3232
});
3333

34-
test('fileURLToPath with invalid path', () => {
35-
if (isWindows) {
36-
assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), {
37-
code: 'ERR_INVALID_FILE_URL_PATH'
38-
});
39-
assert.throws(() => url.fileURLToPath('file:///C:/a%5C/'), {
40-
code: 'ERR_INVALID_FILE_URL_PATH'
41-
});
42-
assert.throws(() => url.fileURLToPath('file:///?:/'), {
43-
code: 'ERR_INVALID_FILE_URL_PATH'
44-
});
45-
} else {
46-
assert.throws(() => url.fileURLToPath('file:///a%2F/'), {
47-
code: 'ERR_INVALID_FILE_URL_PATH'
48-
});
49-
}
50-
});
51-
5234
const windowsTestCases = [
5335
// Lowercase ascii alpha
5436
{ path: 'C:\\foo', fileURL: 'file:///C:/foo' },
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
// This tests that url.fileURLToPath() throws ERR_INVALID_FILE_URL_PATH
4+
// for invalid file URL paths along with the input property.
5+
6+
const { isWindows } = require('../common');
7+
const assert = require('assert');
8+
const url = require('url');
9+
10+
const inputs = [];
11+
12+
if (isWindows) {
13+
inputs.push('file:///C:/a%2F/', 'file:///C:/a%5C/', 'file:///?:/');
14+
} else {
15+
inputs.push('file:///a%2F/');
16+
}
17+
18+
for (const input of inputs) {
19+
assert.throws(() => url.fileURLToPath(input), (err) => {
20+
assert.strictEqual(err.code, 'ERR_INVALID_FILE_URL_PATH');
21+
assert(err.input instanceof URL);
22+
assert.strictEqual(err.input.href, input);
23+
return true;
24+
});
25+
}

0 commit comments

Comments
 (0)