From: Eduardo San Martin Morote Date: Sun, 27 Oct 2019 16:43:38 +0000 (+0100) Subject: feat(errors): improve message for NoMatch X-Git-Tag: v4.0.0-alpha.0~175 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d909d8fbd7768da5c7c349e53bc45f36a393be27;p=thirdparty%2Fvuejs%2Frouter.git feat(errors): improve message for NoMatch --- diff --git a/__tests__/matcher/__snapshots__/resolve.spec.ts.snap b/__tests__/matcher/__snapshots__/resolve.spec.ts.snap index 044ee4ab..187ce432 100644 --- a/__tests__/matcher/__snapshots__/resolve.spec.ts.snap +++ b/__tests__/matcher/__snapshots__/resolve.spec.ts.snap @@ -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":{}}] +`; diff --git a/__tests__/matcher/resolve.spec.ts b/__tests__/matcher/resolve.spec.ts index d62846f6..2984dcf1 100644 --- a/__tests__/matcher/resolve.spec.ts +++ b/__tests__/matcher/resolve.spec.ts @@ -524,7 +524,7 @@ describe('Router Matcher', () => { expect( assertErrorMatch( record, - {}, + { params: { a: 'foo' } }, { ...start, matched: start.matched.map(normalizeRouteRecord), diff --git a/src/errors.ts b/src/errors.ts index b63d2add..52502fad 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -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 -} diff --git a/src/matcher/index.ts b/src/matcher/index.ts index f0ca9daf..973c4416 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -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)