Using ExpressJS 4.16.0
, and NodeJS 10.15.0
, and KnexJS/Bookshelf, I'm trying to figure out error handling.
Errors are being caught as expected in my application, but the error is not being returned in the response from the server call: return res.status(400).send(error);
, instead I'm getting a {}
.
How can I fix this?
user route:
router.post('/users/register',
async function(req, res, next) {
let user;
try {
user = await UserService.register(req.body);
} catch (error) {
console.log('Route: Catch: ', error); // Will display error from UserService Error here: "column users.emailll does not exist"
return res.status(400).send(error); // Sends `{}` back
}
...
UsersService:
...
try {
user = await new User.User({
emailll: userInput.email // Will throw error in "catch" below because "emailll" is invalid field
}).fetch();
} catch (error) {
// Throws error "column users.emailll does not exist"
console.log('Service: Catch: ', error);
throw new Error(error);
}
Response from server: {}
I'm assuming the error is being swallowed somewhere due to async await
calls? Do I need to use something like this express-async-errors lib?