Saturday 3 June 2017

Node Express: how to only send custom error messages back to client

How can i ensure that only error messages that i create are sent to the client, in the context of a promise rejection?

I'm throwing a custom error, which is working fine.

return db.query(`SELECT * FROM events WHERE hostId = ? AND status = ?;`,[userId, ACTIVE_EVENT])
.then((results)=>{
    if(results.length != 0)
        throw new Error('You already have an active event')
})

It's later being caught

.catch(function (err) {
    console.log(err)
    res.status(400).send(err.message)
});

And a nice error msg goes to the client.

But sometimes i get a nasty error from a malformed database query, or whatever, and the client would see a msg like "ER_TRUNCATED_WRONG_VALUE: Incorrect time value:"



via Eric Guan

No comments:

Post a Comment