From edca90223c28a1cd77c6c6c6a17395a61e93843e Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sat, 16 Aug 2025 16:55:15 +0200 Subject: [PATCH] feat: expose miss utility --- packages/router/src/experimental/index.ts | 2 ++ .../route-resolver/matchers/errors.ts | 33 +++++++------------ .../route-resolver/matchers/test-utils.ts | 5 ++- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/packages/router/src/experimental/index.ts b/packages/router/src/experimental/index.ts index ed848772..7e1a0ff6 100644 --- a/packages/router/src/experimental/index.ts +++ b/packages/router/src/experimental/index.ts @@ -36,6 +36,8 @@ export type { ParamParser, } from './route-resolver/matchers/matcher-pattern' +export { miss, MatchMiss } from './route-resolver/matchers/errors' + // in the new experimental router, there are only parents // this should create type errors if someone is realying on children declare module 'vue-router' { diff --git a/packages/router/src/experimental/route-resolver/matchers/errors.ts b/packages/router/src/experimental/route-resolver/matchers/errors.ts index 142b37ff..7e60091f 100644 --- a/packages/router/src/experimental/route-resolver/matchers/errors.ts +++ b/packages/router/src/experimental/route-resolver/matchers/errors.ts @@ -1,29 +1,20 @@ /** - * NOTE: for these classes to keep the same code we need to tell TS with `"useDefineForClassFields": true` in the `tsconfig.json` - */ - -// 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 - -/** - * Error throw when a matcher miss + * Error throw when a matcher matches by regex but validation fails. */ export class MatchMiss extends Error { name = 'MatchMiss' } -// NOTE: not sure about having a helper. Using `new MatchMiss(description?)` is good enough -export const miss = () => new MatchMiss() -// TODO: which one?, the return type of never makes types work anyway -// export const throwMiss = () => { throw new MatchMiss() } -// export const throwMiss = (...args: ConstructorParameters) => { throw new MatchMiss(...args) } - /** - * Error throw when a param is invalid when parsing params from path, query, or hash. + * Helper to create a {@link MatchMiss} error. + * @param args - Arguments to pass to the `MatchMiss` constructor. + * + * @example + * ```ts + * throw miss() + * // in a number param matcher + * throw miss('Number must be finite') + * ``` */ -export class ParamInvalid extends Error { - name = 'ParamInvalid' - constructor(public param: string) { - super() - } -} -export const invalid = (param: string) => new ParamInvalid(param) +export const miss = (...args: ConstructorParameters) => + new MatchMiss(...args) diff --git a/packages/router/src/experimental/route-resolver/matchers/test-utils.ts b/packages/router/src/experimental/route-resolver/matchers/test-utils.ts index 2da8adde..b4336e95 100644 --- a/packages/router/src/experimental/route-resolver/matchers/test-utils.ts +++ b/packages/router/src/experimental/route-resolver/matchers/test-utils.ts @@ -5,7 +5,7 @@ import { MatcherPatternHash, } from './matcher-pattern' import { NEW_MatcherRecord } from '../old/resolver-dynamic' -import { invalid, miss } from './errors' +import { miss } from './errors' export const ANY_PATH_PATTERN_MATCHER: MatcherPatternPath<{ pathMatch: string @@ -37,8 +37,7 @@ export const USER_ID_PATH_PATTERN_MATCHER: MatcherPatternPath<{ id: number }> = } const id = Number(match[1]) if (Number.isNaN(id)) { - throw invalid('id') - // throw miss() + throw miss(`Invalid number: ${String(match[1])}`) } return { id } }, -- 2.47.3