kriasoft / universal-router

A simple middleware-style router for isomorphic JavaScript web apps
https://www.kriasoft.com/universal-router/
MIT License
1.7k stars 105 forks source link

middleware TypeError for not found routes #168

Closed asyncLiz closed 5 years ago

asyncLiz commented 5 years ago

I'm submitting a ...

When using an action and calling context.next() with child routes, a TypeError is thrown if the route does not exist. I expected a route not found error to be thrown.

https://jsbin.com/zicukiyoxu/1/edit?html,output

    const router = new UniversalRouter({
      path: '/',
      async action({ next }) {
        console.log('middleware: start');
        const child = await next();
        console.log('middleware: end');
        return child;
      },
      children: [
        {
          path: '/hello',
          action() {
            console.log('route: return a result');
            return 'Hello, world!';
          }
        }
      ]
    });

    router.resolve('/hello/404');
universal-router.js:439 Uncaught (in promise) TypeError: Cannot read property 'route' of undefined
    at next (VM173 universal-router.js:439)
    at VM173 universal-router.js:462

If you comment out the action middleware, you get the expected error message

Uncaught (in promise) Error: Route not found
    at next (VM173 universal-router.js:451)
    at VM173 universal-router.js:462

I still get the TypeError if I add a fallback route using '*'. Without the action middleware and with a fallback, the fallback route is correctly returned and no error is thrown.

frenzzy commented 5 years ago

Thank you for the bug report! The fix is released: v8.2.1 🎉