Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda