Skip to content

Commit ba5b659

Browse files
joyeecheungrichardlau
authored andcommitted
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 <yagiz@nizipli.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 903ebd3 commit ba5b659

5 files changed

Lines changed: 36 additions & 22 deletions

File tree

‎doc/api/errors.md‎

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

1995+
The thrown error object includes an `input` property that contains the URL object
1996+
of the invalid `file:` URL.
1997+
19951998
<aid="ERR_INVALID_HANDLE_TYPE"></a>
19961999

19972000
### `ERR_INVALID_HANDLE_TYPE`

‎lib/internal/errors.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,10 @@ E('ERR_INVALID_FD',
14761476
E('ERR_INVALID_FD_TYPE','Unsupported fd type: %s',TypeError);
14771477
E('ERR_INVALID_FILE_URL_HOST',
14781478
'File URL host must be "localhost" or empty on %s',TypeError);
1479-
E('ERR_INVALID_FILE_URL_PATH','File URL path %s',TypeError);
1479+
E('ERR_INVALID_FILE_URL_PATH',function(reason,input){
1480+
this.input=input;
1481+
return`File URL path ${reason}`;
1482+
},TypeError);
14801483
E('ERR_INVALID_HANDLE_TYPE','This handle type cannot be sent',TypeError);
14811484
E('ERR_INVALID_HTTP_TOKEN','%s must be a valid HTTP token ["%s"]',TypeError,HideStackFramesError);
14821485
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
@@ -1462,7 +1462,7 @@ function getPathFromURLWin32(url) {
14621462
if((pathname[n+1]==='2'&&third===102)||// 2f 2F /
14631463
(pathname[n+1]==='5'&&third===99)){// 5c 5C \
14641464
thrownewERR_INVALID_FILE_URL_PATH(
1465-
'must not include encoded \\ or / characters',
1465+
'must not include encoded \\ or / characters',url,
14661466
);
14671467
}
14681468
}
@@ -1483,7 +1483,7 @@ function getPathFromURLWin32(url) {
14831483
constsep=StringPrototypeCharAt(pathname,2);
14841484
if(letter<CHAR_LOWERCASE_A||letter>CHAR_LOWERCASE_Z||// a..z A..Z
14851485
(sep!==':')){
1486-
thrownewERR_INVALID_FILE_URL_PATH('must be absolute');
1486+
thrownewERR_INVALID_FILE_URL_PATH('must be absolute',url);
14871487
}
14881488
returnStringPrototypeSlice(pathname,1);
14891489
}
@@ -1550,7 +1550,7 @@ function getPathBufferFromURLWin32(url) {
15501550

15511551
if(letter<CHAR_LOWERCASE_A||letter>CHAR_LOWERCASE_Z||// a..z A..Z
15521552
(sep!==CHAR_COLON)){
1553-
thrownewERR_INVALID_FILE_URL_PATH('must be absolute');
1553+
thrownewERR_INVALID_FILE_URL_PATH('must be absolute',url);
15541554
}
15551555

15561556
// Now, we'll just return everything except the first byte of
@@ -1569,6 +1569,7 @@ function getPathFromURLPosix(url) {
15691569
if(pathname[n+1]==='2'&&third===102){
15701570
thrownewERR_INVALID_FILE_URL_PATH(
15711571
'must not include encoded / characters',
1572+
url,
15721573
);
15731574
}
15741575
}

‎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
constwindowsTestCases=[
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+
constassert=require('assert');
8+
consturl=require('url');
9+
10+
constinputs=[];
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(constinputofinputs){
19+
assert.throws(()=>url.fileURLToPath(input),(err)=>{
20+
assert.strictEqual(err.code,'ERR_INVALID_FILE_URL_PATH');
21+
assert(err.inputinstanceofURL);
22+
assert.strictEqual(err.input.href,input);
23+
returntrue;
24+
});
25+
}

0 commit comments

Comments
 (0)