Error handling dont work #1098

Vavooon opened this Issue Apr 21, 2012 · 15 comments


None yet
5 participants

Vavooon commented Apr 21, 2012

Today i updated express.js to 2.5.9 and now code
app.use(function(err, req, res, next){...})
dont work, only with three parameters.
I`m using node 0.6.15.
Sorry for my poor english.


tj commented May 9, 2012

make sure you have that middleware .use() call low (or very last) below the others

Vavooon commented May 9, 2012

I has inserted it in the end of app.configure block, but there is no changes.
Before one of last updates i used this function for processing express.limit('512kb'), but it not work now.

pyrat commented May 30, 2012

Same situation for me, it works if I raise an error in the action but for random errors within model code, it dies.


tj commented May 30, 2012

@pyrat if the model (or anything else) is throwing you'll need to try/catch those and pass them with next(). that's the unfortunate code-smell that is callbacks

pyrat commented May 30, 2012

@visionmedia thanks for the reply, can you expand on what you mean by passing caught exceptions through with next()

On another note, is 2.5.9 the recommended stable version at the moment?


tj commented May 30, 2012

For example if you have this in a route:

User.find(id, function(){})

and User.find() might throw (just pretend), then you need to wrap in a trycatch:

try {
} catch (err) {

typically most APIs simply dont throw, you get the error in the callback, but if the code is written poorly and has bugs, or things like JSON.parse() which throw, then you unfortunately have to try/catch them and delegate the error. If higher level node stuff used coros this wouldn't be the case

pyrat commented Jun 1, 2012

@visionmedia thanks for the response, makes sense.

What is the recommended stable version of express at the moment? 2.5.9?


tj commented Jun 1, 2012

yeah 2.x is stable, 3x is in alpha so things are still changing a bit but not much


tj commented Jun 1, 2012

mostly bug fixes

with Node 0.8.0 I think the problem occurs. I place app.use(error) at the very end just before I define the function: function error(err, req, res) {}

But err is the req, req is the res, and res is the next. only 3 parameters intead of 4.


tj commented Jun 29, 2012

@murvinlai to define it as an error-handler it has to have 4 args: (err, req, res, next)

ok.. seems like it works now. :) thanks.

I can not seam to get the middleware working in Express. This is my app and my custom error-handler is never called.

"use strict"

http    = require 'http'
express = require 'express'
app     = express()

app.use express.bodyParser()
app.get '/', (req, res, next) ->
  res.send 'Hello World'
    throw new Error 'Broke!'
  catch err
    next err

app.use (err, req, res, next) ->
  console.log 'this is never called'

app.listen 3000
console.log 'Server is running on port 3000'

Node.JS: v0.8.19
Express.JS: v3.1.0


tj commented Feb 15, 2013

wouldn't be surprised if the coffeescript is compiling to something you dont want, cant say, but it does work, we have tons of tests and no issues in any of our apps

tj closed this Feb 15, 2013

Apparently calling res.send('Hello World'); before throwing the error was the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment