Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
How to handle Express errors without relying on the function's length property?

From Express's error handling docs:

Define error-handling middleware functions in the same way as other middleware functions, except error-handling functions have four arguments instead of three: (err, req, res, next). For example:

app.use(function (err, req, res, next) {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})

It seems that Express's .use(middlware) inspects the middleware function's length to see how many arguments it takes, and if four, then it treats it differently to other middleware functions, passing it an error as the first argument.

This is incompatible with modern linting setups like XO or Airbnb, as it forces you to define an extra trailing parameter tha doesn't get consumed (i.e. next in the above example). Also, unused trailing parameters might get removed by some automated code transformations, which is just something I don't want to worry about. In my opinion, Function.prototype.length is best kept for metaprogramming/introspection as opposed to affecting real production behaviour.

Is there any other, more explicit way to define an error handler in Express, that doesn't force you to imply intended functionality through the number of parameters your function takes?

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!