[APM] Improve error message for route matching (#126455)

This commit is contained in:
Dario Gieselaar 2022-03-01 14:49:27 +01:00 committed by GitHub
parent e0e14c4b47
commit c8a206545a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -196,7 +196,15 @@ describe('createRouter', () => {
it('throws an error if the given path does not match any routes', () => {
expect(() => {
router.getParams('/service-map', history.location);
}).toThrowError('No matching route found for /service-map');
}).toThrowError('/service-map does not match current path /');
expect(() => {
router.getParams('/services/{serviceName}', history.location);
}).toThrowError('/services/{serviceName} does not match current path /');
expect(() => {
router.getParams('/service-map', '/services/{serviceName}', history.location);
}).toThrowError('None of /service-map, /services/{serviceName} match current path /');
});
it('does not throw an error if the given path does not match any routes but is marked as optional', () => {
@ -248,7 +256,7 @@ describe('createRouter', () => {
expect(() => {
router.matchRoutes('/traces', history.location);
}).toThrowError('No matching route found for /traces');
}).toThrowError('/traces does not match current path /service-map');
});
it('applies defaults', () => {

View file

@ -99,7 +99,15 @@ export function createRouter<TRoutes extends RouteMap>(routes: TRoutes): Router<
if (optional) {
return [];
}
throw new Error(`No matching route found for ${paths}`);
let errorMessage: string;
if (paths.length === 1) {
errorMessage = `${paths[0]} does not match current path ${location.pathname}`;
} else {
errorMessage = `None of ${paths.join(', ')} match current path ${location.pathname}`;
}
throw new Error(errorMessage);
}
return matches.slice(0, matchIndex + 1).map((matchedRoute) => {