Skip to content

Commit 3a95d54

Browse files
committed
Properly handle failures
As discussed in web-platform-tests/wpt#2830 (comment). This makes the setter tests pass (by having most of them ignore invalid input instead of throwing). It should also make parsing failures during construction throw correctly; previously they were checking a nonexistant urlRecord.failure property. This changes the public API by making the URL-record-returning functions return either a URL record or the string "failure", instead of returning either a URL record or throwing.
1 parent a9f1f00 commit 3a95d54

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ The URL record type has the following API:
6262
- [`cannotBeABaseURL`](https://url.spec.whatwg.org/#url-cannot-be-a-base-url-flag) (as a boolean)
6363

6464
These properties should be treated with care, as in general changing them will cause the URL record to be in an inconsistent state until the appropriate invocation of `basicURLParse` is used to fix it up. You can see examples of this in the URL Standard, where there are many step sequences like "4. Set context object’s url’s fragment to the empty string. 5. Basic URL parse _input_ with context object’s url as _url_ and fragment state as _state override_." In between those two steps, a URL record is in an unusable state.
65+
66+
The return value of "failure" in the spec is represented by the string `"failure"`. That is, functions like `parseURL` and `basicURLParse` can return _either_ a URL record _or_ the string `"failure"`.

lib/URL-impl.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ exports.implementation = class URLImpl {
99
let parsedBase = null;
1010
if (base !== undefined) {
1111
parsedBase = usm.basicURLParse(base);
12-
if (parsedBase.failure) {
12+
if (parsedBase === "failure") {
1313
throw new TypeError("Invalid base URL");
1414
}
1515
}
1616

1717
const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
18-
if (parsedURL.failure) {
18+
if (parsedURL === "failure") {
1919
throw new TypeError("Invalid URL");
2020
}
2121

@@ -45,7 +45,12 @@ exports.implementation = class URLImpl {
4545
}
4646

4747
set href(v) {
48-
this._url = usm.basicURLParse(v);
48+
const parsedURL = usm.basicURLParse(v);
49+
if (parsedURL === "failure") {
50+
throw new TypeError("Invalid URL");
51+
}
52+
53+
this._url = parsedURL;
4954
}
5055

5156
get origin() {

src/url-state-machine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ module.exports.basicURLParse = function (input, options) {
11291129

11301130
const usm = new URLStateMachine(input, options.baseURL, options.encodingOverride, options.url, options.stateOverride);
11311131
if (usm.failure) {
1132-
throw new TypeError("Invalid URL");
1132+
return "failure";
11331133
}
11341134

11351135
return usm.url;

0 commit comments

Comments
 (0)