title: Does a middleware have access to middleware error handler?
body: <p>I am currently using Express and trying to handle a JsonWebToken error gracefully.</p>
  <p>I have two middleware functions I have created, one that extracts a user from a jwt token, and one that handles any route errors for specific usecases.</p>
  <pre><code>const userExtractor = async (req, res, next) =&gt; {
      const token = req.token
      if (token) {
          const decodedToken = jwt.verify(token, process.env.SECRET)
          const user = await User.findById(decodedToken.id)
          req.user = user
      }
  
      next()
  }
  </code></pre>
  <p>If token is included and verified, and finds a user from my database with the id inside the token, a user object is attached to any incoming requests.</p>
  <p>I am trying to error handle when someone sends a faulty token. The error occurs with jsonwebtoken <code>.verify</code> method.</p>
  <pre><code>Desktop\backend\node_modules\jsonwebtoken\verify.js:75
      return done(new JsonWebTokenError('invalid token'))
  </code></pre>
  <p>My error handle should be handling this specific error, however I am not sure if how middlewares work this is possible.</p>
  <pre><code>const errorHandler = (error, req, res, next) =&gt; {
      if (error.name === &quot;JsonWebTokenError&quot;) {
          return res.status(400).json({error: 'invalid token has been sent'})
      }
      next(error)
  }
  </code></pre>
  <p>And yes, I have initiated both middlewares, with the error handler specifically being the last middleware used at the end.</p>
  <pre><code>// Util middleware
  app.use(middleware.tokenExtractor)
  app.use(middleware.userExtractor)
  
  // Routes
  app.use('/auth', authRouter);
  app.use('/api/me', meRouter)
  
  app.get(&quot;/&quot;, (req, res) =&gt; {
      res.send(&quot;Hello World!&quot;);
  });
  
  app.use(middleware.errorHandler)
  </code></pre>