Skip to content

Commit 70d2d6d

Browse files
joyeecheungtargos
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 <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 41245ad commit 70d2d6d

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
<aid="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
@@ -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
@@ -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
thrownewERR_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
constsep=StringPrototypeCharAt(pathname,2);
14851485
if(letter<CHAR_LOWERCASE_A||letter>CHAR_LOWERCASE_Z||// a..z A..Z
14861486
(sep!==':')){
1487-
thrownewERR_INVALID_FILE_URL_PATH('must be absolute');
1487+
thrownewERR_INVALID_FILE_URL_PATH('must be absolute',url);
14881488
}
14891489
returnStringPrototypeSlice(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-
thrownewERR_INVALID_FILE_URL_PATH('must be absolute');
1554+
thrownewERR_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
thrownewERR_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
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)