question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handling "Cannot match any routes " in express engine

See original GitHub issue

🐞 Bug report

What modules are related to this issue?

  • aspnetcore-engine
  • builders
  • common
  • express-engine
  • hapi-engine

Is this a regression?

No

Description

When an error occurs in the express engine, there seems to be no way to catch it and allow express to respond with a 500 error. Instead, the browser keeps waiting for a response that never comes.

A good example is provided by ticket #1630. If you navigate to a url that doesn’t exist, the browser keeps waiting for a response that never comes. The server console shows an error, ie.:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'CCC'
Error: Cannot match any routes. URL Segment: 'CCC'
    at ApplyRedirects.noMatchError (/Users/mathieu/Projects/zimmo/v3-universal/dist/v3-universal/server/main.js:136824:16)
    at CatchSubscriber.selector (/Users/mathieu/Projects/zimmo/v3-universal/dist/v3-universal/server/main.js:136808:28)

While there is a specific solution for the given error in #1630, I’m looking for a more generic solution in case “something breaks” inside Angular. The end game is to not let the express app crash and/or return a 500 response to the browser.

I tried a few things to catch the given error in server.ts.

Example 1 - Generic Express error middleware:

  server.use(function (err, req, res, next) {
    // this is never called
    res.status(404).send("Sorry can't find that!")
  })

Example 2 - res.render callback

  server.get('*', (req, res) => {
    res.render('index.html', { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] }, function (err, html) {
        // this is never called
        if (err) { throw err; };
        res.send(html);
     });
  });

Example 3 - try/catch for res.render

  server.get('*', (req, res) => {
    try {
    res.render('index.html', { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
    } catch (err) {
       // this is never called
       throw err;
    }
  });

🔬 Minimal Reproduction

#1630 covers a straight-forward scenario.

🌍 Your Environment


Angular CLI: 10.2.3
Node: 12.16.2
OS: darwin x64

Angular: 10.2.5
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1002.3
@angular-devkit/build-angular   0.1002.3
@angular-devkit/core            10.2.3
@angular-devkit/schematics      10.2.3
@angular/cli                    10.2.3
@nguniversal/builders           10.1.0
@nguniversal/express-engine     10.1.0
@schematics/angular             10.2.3
@schematics/update              0.1002.3
rxjs                            6.6.7
typescript                      4.0.7

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

2reactions
webberigcommented, Jan 15, 2022

I’ve come to realise this is not really a Universal bug/problem.

My mistake was to approach these errors from the Universal point-of-view, not realising they would prove problematic even without Universal. Even without Universal engine, Angular will behave odd when given examples in my previous posts occur.

Angular offers different options to handle errors, but does not implement them by default when you create a new application:

Developers should address given scenarios and add error handling to the Angular application. After that, Universal should land on its feet.

1reaction
lindsaylevinecommented, Jul 31, 2021

hello! i’m experiencing the exact same issue. like @webberig, i have tried error middleware, try catch, promise catching, res.render callback. no matter what, this error never gets caught, and causes the serverless function that wraps it to time out.

Screen Shot 2021-07-30 at 11 10 49 PM

i challenge this comment in the issue @webberig linked. this has been consistently reported across several angular repos. and as we’ve pointed out, try catch does not handle a res.render that never resolves or rejects because it is infinitely stuck. cc @CaerusKaru

related issues ive found:

  1. https://github.com/angular/angular/issues/17073#issuecomment-316864447 – this is an older one but seems very relevant to the underlying cause here. says fixed but the issue definitely still persists.
  2. https://github.com/angular/universal/issues/1810
  3. https://github.com/angular/universal/issues/1425
Read more comments on GitHub >

github_iconTop Results From Across the Web

two angular apps in a express app (Error: Cannot match any ...
The strange is that checking the network console, I can check that the file does get sent. It seems like if the navigation...
Read more >
Using Express.js Routes for Promise-based Error Handling
An Express.js route is a handler function corresponding to a given type of HTTP event matching a specified URI pattern. It's sent to...
Read more >
Routing - Express.js
Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Route ......
Read more >
New Express 5 Features to Try - Fusebit
Express 5 will enforce RegExp for matching group expressions in route paths. This facilitates better text matching in paths using patterns.
Read more >
Getting Started with Express - Reflectoring
We define a route by associating it with one or more callback functions called handler functions, which are executed when the application ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found