]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(errors): improve message for NoMatch
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 27 Oct 2019 16:43:38 +0000 (17:43 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 27 Oct 2019 16:43:38 +0000 (17:43 +0100)
__tests__/matcher/__snapshots__/resolve.spec.ts.snap
__tests__/matcher/resolve.spec.ts
src/errors.ts
src/matcher/index.ts

index 044ee4aba136442c8e69574fda561a8df0b45fd0..187ce4327dc82723e7ca4c2ebf2478131569c360 100644 (file)
@@ -1,8 +1,19 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
-exports[`Router Matcher resolve LocationAsName throws if the named route does not exists 1`] = `[NoRouteMatchError: No match for {"path":"/","name":"Home","params":{},"query":{},"hash":"","fullPath":"/"}]`;
+exports[`Router Matcher resolve LocationAsName throws if the named route does not exists 1`] = `
+[NoRouteMatchError: No match for
+{"name":"Home"}]
+`;
 
-exports[`Router Matcher resolve LocationAsPath throws if the path does not exists 1`] = `[NoRouteMatchError: No match for {"path":"/foo","params":{},"query":{},"hash":"","fullPath":"/"}]`;
+exports[`Router Matcher resolve LocationAsPath disallows multiple trailing slashes 1`] = `
+[NoRouteMatchError: No match for
+{"path":"/home//"}]
+`;
+
+exports[`Router Matcher resolve LocationAsPath throws if the path does not exists 1`] = `
+[NoRouteMatchError: No match for
+{"path":"/foo"}]
+`;
 
 exports[`Router Matcher resolve LocationAsRelative redirects throws if relative location when redirecting 1`] = `
 [InvalidRouteMatch: Cannot redirect using a relative location:
@@ -12,4 +23,9 @@ exports[`Router Matcher resolve LocationAsRelative redirects throws if relative
 Use the function redirect and explicitely provide a name]
 `;
 
-exports[`Router Matcher resolve LocationAsRelative throws if the current named route does not exists 1`] = `[NoRouteMatchError: No match for {"name":"home","params":{},"path":"/"}]`;
+exports[`Router Matcher resolve LocationAsRelative throws if the current named route does not exists 1`] = `
+[NoRouteMatchError: No match for
+{"params":{"a":"foo"}}
+while being at
+{"name":"home","params":{},"path":"/","meta":{}}]
+`;
index d62846f6c0a82d5aecf94758fbc4ee63c8b969b0..2984dcf1657c094bf2c29e62f8b8c11a70ea6877 100644 (file)
@@ -524,7 +524,7 @@ describe('Router Matcher', () => {
         expect(
           assertErrorMatch(
             record,
-            {},
+            { params: { a: 'foo' } },
             {
               ...start,
               matched: start.matched.map(normalizeRouteRecord),
index b63d2add5ea6ce111bdaa7c8a0ab2a912ce1945e..52502fadaa2e4107c2da792b9a5af13a76508b31 100644 (file)
@@ -57,13 +57,15 @@ export class NoRouteMatchError extends RouterError {
   private [isNoRouteMatchError] = true
 
   constructor(
-    currentLocation: MatcherLocationNormalized,
-    location: MatcherLocation
+    location: MatcherLocation,
+    currentLocation?: MatcherLocationNormalized
   ) {
-    // TODO: change the merge to provide information that is useful only
     super(
-      'No match for ' +
-        JSON.stringify(mergeMatcherLocations(currentLocation, location))
+      'No match for\n' +
+        JSON.stringify(location) +
+        (currentLocation
+          ? '\nwhile being at\n' + JSON.stringify(currentLocation)
+          : '')
     )
   }
 
@@ -224,12 +226,3 @@ function stringifyRoute(to: RouteLocation): string {
   }
   return JSON.stringify(location, null, 2)
 }
-
-function mergeMatcherLocations(
-  currentLocation: MatcherLocationNormalized,
-  location: MatcherLocation
-) {
-  const merged = { ...currentLocation, ...location }
-  delete merged.meta
-  return merged
-}
index f0ca9daf6460a7056c652c1f41bddbf390b2a705..973c441627d649b5ed3ec346388ea23c655e66b3 100644 (file)
@@ -103,7 +103,7 @@ export function createRouterMatcher(routes: RouteRecord[]): RouterMatcher {
     if ('name' in location && location.name) {
       matcher = matchers.find(m => m.record.name === location.name)
 
-      if (!matcher) throw new NoRouteMatchError(currentLocation, location)
+      if (!matcher) throw new NoRouteMatchError(location)
 
       name = matcher.record.name
       // TODO: merge params
@@ -132,7 +132,7 @@ export function createRouterMatcher(routes: RouteRecord[]): RouterMatcher {
       // TODO: if no matcher, return the location with an empty matched array
       // to allow non existent matches
       // TODO: warning of unused params if provided
-      if (!matcher) throw new NoRouteMatchError(currentLocation, location)
+      if (!matcher) throw new NoRouteMatchError(location)
 
       // no need to resolve the path with the matcher as it was provided
       // this also allows the user to control the encoding
@@ -189,7 +189,7 @@ export function createRouterMatcher(routes: RouteRecord[]): RouterMatcher {
       matcher = currentLocation.name
         ? matchers.find(m => m.record.name === currentLocation.name)
         : matchers.find(m => m.re.test(currentLocation.path))
-      if (!matcher) throw new NoRouteMatchError(currentLocation, location)
+      if (!matcher) throw new NoRouteMatchError(location, currentLocation)
       name = matcher.record.name
       params = location.params || currentLocation.params
       path = matcher.resolve(params)