Commit 4386bf7
fix(ext/web): friendlier URLPattern construction errors (#35102)
## Problem
When `URLPattern` construction fails, the underlying
[`urlpattern`](https://crates.io/crates/urlpattern) crate surfaces an
opaque message with no indication of which input value was bad or how to
fix it. The common trigger (reported in #25965) is a file-router route
like Fresh's `[:slug].tsx`, which expands to a `::slug` pathname:
```
error: Uncaught (in promise) TypeError: tokenizer error: invalid name; must be at least length 1 (at char 1)
path: new URLPattern({ pathname }),
```
You can't tell from that which component (`pathname`) was at fault, what
its value was, or what to do about it.
## Change
Enrich the error thrown from `op_urlpattern_parse`
(`ext/web/urlpattern.rs`):
- Echo the offending component value.
- Point a caret at the failing character — only for init objects with a
single component set, since the crate's reported position is relative to
a single parsed component string, not the whole constructor string.
- Add a hint for the most common mistake: a `:` that does not start a
valid named group.
For `new URLPattern({ pathname: "/::slug" })` the message is now:
```
Failed to parse pathname from "/::slug": tokenizer error: invalid name; must be at least length 1 (at char 1)
/::slug
^
hint: ":" starts a named group and must be followed by a name (a letter or "_", then letters, digits or "_"). To match a literal ":", escape it as "\:".
```
Valid patterns are unaffected, and the existing crate message is
preserved as the leading line so nothing that parsed it loses
information.
## Tests
Added unit tests in `tests/unit/urlpattern_test.ts` covering both the
init-object (with caret) and constructor-string (no caret) paths. WPT
`urlpattern` tests assert only on the thrown error *type*, not its
message, so they are unaffected.
Closes #25965
Closes denoland/divybot#542
Co-authored-by: divybot <divybot@users.noreply.github.com>
Co-authored-by: Divy Srivastava <me@littledivy.com>1 parent 05a686c commit 4386bf7
2 files changed
Lines changed: 135 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
9 | 100 | | |
10 | 101 | | |
11 | 102 | | |
| |||
49 | 140 | | |
50 | 141 | | |
51 | 142 | | |
52 | | - | |
| 143 | + | |
| 144 | + | |
53 | 145 | | |
54 | | - | |
| 146 | + | |
| 147 | + | |
55 | 148 | | |
56 | 149 | | |
57 | 150 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
0 commit comments