]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: expose miss utility
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 16 Aug 2025 14:55:15 +0000 (16:55 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 16 Aug 2025 14:55:15 +0000 (16:55 +0200)
packages/router/src/experimental/index.ts
packages/router/src/experimental/route-resolver/matchers/errors.ts
packages/router/src/experimental/route-resolver/matchers/test-utils.ts

index ed84877224af6425fe8ee44a69457c8ea5d91b0c..7e1a0ff69d3027ac4613a768373805ad70d311a8 100644 (file)
@@ -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' {
index 142b37ff8fffdf31d0254a3adf47aed394c4196c..7e60091fb3cce3ec03b7e786d0d669819c3a1fd7 100644 (file)
@@ -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<typeof MatchMiss>) => { 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<typeof MatchMiss>) =>
+  new MatchMiss(...args)
index 2da8adde75c28c1d82c9c5a4a6c0975dc21467e3..b4336e95f56ea262c597c3195fdab3b0eb22e686 100644 (file)
@@ -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 }
     },