Skip to content

Commit edca902

Browse files
committed
feat: expose miss utility
1 parent 2a50349 commit edca902

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

packages/router/src/experimental/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export type {
3636
ParamParser,
3737
} from './route-resolver/matchers/matcher-pattern'
3838

39+
export { miss, MatchMiss } from './route-resolver/matchers/errors'
40+
3941
// in the new experimental router, there are only parents
4042
// this should create type errors if someone is realying on children
4143
declare module 'vue-router' {
Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
/**
2-
* NOTE: for these classes to keep the same code we need to tell TS with `"useDefineForClassFields": true` in the `tsconfig.json`
3-
*/
4-
5-
// TODO: document helpers if kept. The helpers could also be moved to the generated code to reduce bundle size. After all, user is unlikely to write these manually
6-
7-
/**
8-
* Error throw when a matcher miss
2+
* Error throw when a matcher matches by regex but validation fails.
93
*/
104
export class MatchMiss extends Error {
115
name = 'MatchMiss'
126
}
137

14-
// NOTE: not sure about having a helper. Using `new MatchMiss(description?)` is good enough
15-
export const miss = () => new MatchMiss()
16-
// TODO: which one?, the return type of never makes types work anyway
17-
// export const throwMiss = () => { throw new MatchMiss() }
18-
// export const throwMiss = (...args: ConstructorParameters<typeof MatchMiss>) => { throw new MatchMiss(...args) }
19-
208
/**
21-
* Error throw when a param is invalid when parsing params from path, query, or hash.
9+
* Helper to create a {@link MatchMiss} error.
10+
* @param args - Arguments to pass to the `MatchMiss` constructor.
11+
*
12+
* @example
13+
* ```ts
14+
* throw miss()
15+
* // in a number param matcher
16+
* throw miss('Number must be finite')
17+
* ```
2218
*/
23-
export class ParamInvalid extends Error {
24-
name = 'ParamInvalid'
25-
constructor(public param: string) {
26-
super()
27-
}
28-
}
29-
export const invalid = (param: string) => new ParamInvalid(param)
19+
export const miss = (...args: ConstructorParameters<typeof MatchMiss>) =>
20+
new MatchMiss(...args)

packages/router/src/experimental/route-resolver/matchers/test-utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
MatcherPatternHash,
66
} from './matcher-pattern'
77
import { NEW_MatcherRecord } from '../old/resolver-dynamic'
8-
import { invalid, miss } from './errors'
8+
import { miss } from './errors'
99

1010
export const ANY_PATH_PATTERN_MATCHER: MatcherPatternPath<{
1111
pathMatch: string
@@ -37,8 +37,7 @@ export const USER_ID_PATH_PATTERN_MATCHER: MatcherPatternPath<{ id: number }> =
3737
}
3838
const id = Number(match[1])
3939
if (Number.isNaN(id)) {
40-
throw invalid('id')
41-
// throw miss()
40+
throw miss(`Invalid number: ${String(match[1])}`)
4241
}
4342
return { id }
4443
},

0 commit comments

Comments
 (0)