I am using nodejs, express and node-mysql2 for my application. I want timezone for each connection to use UTC so that my time_created and time_modified (MYSQL: On update CURRENT_TIMESTAMP) column will have timestamp in UTC only. I don't have permissions to set timestamp of MYSQL server hence I need to do this in my application only.
I am using connection pool and adding an event listener for connection event which set timezone for that connection to be UTC.
pool.on('connection', (conn) => {
conn.query("SET time_zone='+00:00';", (error) => {
if (error) {
throw error;
}
});
});
My route definition is similar to this:
router.get('/route', async (req, res) => {
try {
const [rows] = await pool.execute('SELECT * FROM student', []);
return res.status(200).send('Success');
} catch (e) {
return res.status(500).send('Failure');
}
});
If this query "SET time_zone='+00:00'" in event listener fails, my node server get crashed with stacktrace on console. I want to catch this kind of errors in my route or anywhere so that I can send 500 response to client. Can you tell me what is good approach to handle this kind of exception thrown from event listeners in express route?