-1

I am learning node from time to time. Have this question.

Middleware 1:

app.use(function(req, res, next) {
   db.load(function(err, session) {
    if (err) {
      return next(err);
    }
    ...
  });
 });

Middleware 2:

app.use(function(req, res, next) {
       next();
});

Route handler

app.get('/', function(req, res, next) {
});

Imagine middleware 1 calls next(err). Now my question is:

  • how do I understand in middleware 2 that error was issued from 1st middleware?
  • Also refer [__"passing variables to the next middleware using next() in expressjs"__](http://stackoverflow.com/questions/18875292/passing-variables-to-the-next-middleware-using-next-in-expressjs) – Rayon Mar 24 '17 at 05:30
  • This is not a duplicate... He's asking about errors. Not passing variables into the next function. – meyer9 Mar 24 '17 at 05:31
  • @meyer9 — Is there any way one could access passed value in next handler ? – Rayon Mar 24 '17 at 05:34
  • 1
    If you throw an error, or next(err), you can access it by creating a middleware with the signature, `function(err, req, res, next) { console.log(err) }` and then using that in your app. – meyer9 Mar 24 '17 at 05:35
  • And if you wanted to continue addressing the error later down the middleware chain, you can add a next(err) onto that one. – meyer9 Mar 24 '17 at 05:36
  • @Rayon Doesn't seem like a dupe, I would reconsider the close vote if I were you. –  Mar 24 '17 at 05:36
  • @meyer9 How does it distinguish function(err, req, res, next) from function(req, res, next)? –  Mar 24 '17 at 05:37
  • @meyer9 Thanks I don't know what's wrong with people closing all questions as dupes –  Mar 24 '17 at 05:38
  • `(function(a){}).length = 1` and `(function(a, b){}).length = 2`. – meyer9 Mar 24 '17 at 05:39
  • I am curious about to be accepted answer of this question.. I have rolled back my close vote though.... – Rayon Mar 24 '17 at 05:41
  • @GiorgiMoniava I posted this in answer form now here: http://stackoverflow.com/a/42992187/2537322 – meyer9 Mar 24 '17 at 05:42
  • @Bergi: We have a second incorrect duplicate vote cast here. –  Mar 24 '17 at 05:45
  • @GiorgiMoniava How so ? "Error handling principles for Node.js + Express.js applications?" is perfect dupe! – Rayon Mar 24 '17 at 07:33
  • @Rayon That link was not posted when I put this comment. –  Mar 24 '17 at 07:40

2 Answers2

0

First of all, middleware 2 will not be called until it has a function signature like :-

app.use(function(err,req, res, next){
  // your code
})

You can pass middleware name in error object you pass in middleware 1

next({err:err,name:'middleware1'})

And access name in middleware 2

console.log(err.name)
Anmol Mittal
  • 843
  • 5
  • 12
  • "First of all, middleware 2 will not be called until it has a function signature like" --> Why? How does it differentiate between two function signatures? –  Mar 24 '17 at 05:36
  • 1
    https://github.com/expressjs/express/blob/master/lib/router/layer.js#L65 , looping through the middleware stack, it checks for no. of the arguments, and above method is called from :- https://github.com/expressjs/express/blob/master/lib/router/route.js#L135 – Anmol Mittal Mar 24 '17 at 05:39
  • Also , check out docs :- https://expressjs.com/en/guide/error-handling.html – Anmol Mittal Mar 24 '17 at 05:42
  • So using call next(error) --> it will jump to error handler middleware and skip other middlewares? –  Mar 24 '17 at 05:44
  • yes, you can handle the error inside that, kind of generic error handler for all middlewares in your app – Anmol Mittal Mar 24 '17 at 05:46
0

Error handling middleware can be accomplished by using 4 arguments in your middleware instead of 3. function(err, req, res, next)

For example, if you wanted to just print out the error, you could create a new middleware like this:

function logErrors (err, req, res, next) {
  console.error(err.stack)
  next(err)
}
app.use(logErrors)

They check the signature of a function by using the length property indicating the number of parameters. For example:

function func1(a) {}
function func2(a, b) {}

console.log(func1.length) # 1
console.log(func2.length) # 2

Once an express request errors out, it will skip any normal middleware to the next middleware that handles errors.

Source: https://expressjs.com/en/guide/error-handling.html

meyer9
  • 1,120
  • 9
  • 26
  • Yes, but why will it jump to error handler middleware instead of running my second middleware? (check my code) –  Mar 24 '17 at 05:44
  • Once an express request errors out, it will skip any normal middleware to the next middleware that handles errors. – meyer9 Mar 24 '17 at 05:45