How do you pass nice error messages to clients in express?
app.use(errorHandler);
function errorHandler(err, req, res, next) {
res.send(err);
}
app.post('/login', usernameIsValid, pwdIsValid, createToken);
In usernameIsValid
I create a new Error
with a message and status and call next(err)
. Now this bypasses pwdIsValid
and createToken
like it should BUT the weird thing is my errorHandler logs the error without a console.log
. Am I using the default error handler in express somewhere? How do I turn it off? I have tried both production and development for NODE_ENV
.
On the client xhr.responseText
is my error.message + what looks like a stack trace? I've even tried just to send err.message but it looks the same.
One way to handle this mess is to make each middleware (if there is an error) create an error string like req.data.err = "invalid username"
and then make sure each following middleware checks req.data.err
if the string is present. But this is tedious as hell >_<