Based on the express framework, node-mysql module development project needs to execute sql statements in the project. If the sql statement is abnormal, the program will crash directly.
conn.query(sql,function (err, result) {}
如果這里的事情了因?yàn)榍岸颂峤环绞降脑颍ɑ蛘呤莿e人的非法提交),sql語(yǔ)句變?yōu)榱?SELECT * FROM prod WHERE prodName LIKE'%undefined%' LIMIT NaN,10;
這樣的話(huà),node控制臺(tái)會(huì)拋出:throw err; // Rethrow non-MySQL errors
node就崩潰了,請(qǐng)問(wèn)有什么好的方式處理sql異常?
Try adding this processing to the end of the startup file
app.js
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
Then you need to check the API of mysql connector. Generally, it must have an exception handling mechanism.
I use mongoose. Add exception handling when declaring the connection.
mongoose.connection.on('error', function (err) {
console.error('Mongoose connection error: ' + err);
});