expressjs / express

Fast, unopinionated, minimalist web framework for node.

Home Page:https://expressjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The default error handler is not catching URIError

abdallaoui-dev opened this issue · comments

app.use((error, req, res, next) => {
   if (error instanceof URIError) {
      return res.send("uri error")
   }

  if (error) {
      return res.send("any error")
   }   
   next()

  //  this is not catching URIError  
})


app.use((req, res, next)) => {
   try {
      decodeURIComponent(req.path)
    } catche(error) {
       return res.send("uri error")
   }
  next()

  // this works but if I add error arg it's not going to work
}

Hi @abdallaoui-dev sorry you are having trouble. I'm not clear on what exactly you mean, though. Can you show the example does reproduces the issue that I can run and debug through it?

if someone put % in url like http://localhost:3000/see/%
express throws URIError: Failed to decode pram /see/% with response 400
now if add the fourth argument to app.use(err, req, res, next) err does't catch it, if I remove err arg and trycatch decodeURI(req.path) it works and I can catch the error.
I solve this by having two functions one for all errors and one for this which should be with all in one function

Hello, thank you, that makes sense. Unfortunately I am not able to replicate what you are saying. Given your statement, I constructed the following simple app to demonstrate the error handler catching an error:

const express = require("./index")
const app = express()

app.get('/see/:param', (req, res) => {
  res.send('see!')
})

app.use((err, req, res, next) => {
  res.send('got an error in the error handler: ' + err.toString())
})

app.listen(3000)

Running and then calling that with the URL you provided, http://localhost:3000/see/% demonstrates that the error handler does indeed get that URIError:

$ curl -s 'http://localhost:3000/see/%'
got an error in the error handler: URIError: Failed to decode param '%'

If you can provide a concrete demonstration like I have above that shows it not being caught, we can help further.