我来到一个连接不可靠的地方,发现我的try catch块在mariadb,nodejs中不起作用,所以应用程序残酷地死亡。我想捕获错误并优雅地返回错误消息。
try{
pool = mariadb.createPool(dbParams);
pool.on("error", err => {
console.log('Mysql error dbParams', dbParams.host)
console.log(err);
})
}catch(e){
console.log('dbParams', dbParams.host)
console.log(e)
}
字符串
并且app在try块中的这个函数中死亡:
async function dbQuery(...args){
let result = false;
try{
result = pool.query(...args)
}catch(e){
console.log('query icinde yakalandi', e)
}
return result;
}
型
错误是这样的:
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^
SqlError: (conn=-1, no: 45028, SQLState: HY000) retrieve connection from pool timeout after 10001ms
(pool connections: active=0 idle=0 limit=5)
at module.exports.createError (/app/node_modules/.pnpm/mariadb@3.1.1/node_modules/mariadb/lib/misc/errors.js:57:10)
at Pool._requestTimeoutHandler (/app/node_modules/.pnpm/mariadb@3.1.1/node_modules/mariadb/lib/pool.js:344:26)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7)
From event:
at ConnectionPromise._PARAM (/app/node_modules/.pnpm/mariadb@3.1.1/node_modules/mariadb/lib/connection-promise.js:104:30)
at PoolPromise.query (/app/node_modules/.pnpm/mariadb@3.1.1/node_modules/mariadb/lib/pool-promise.js:100:40)
at IncomingMessage.dbQuery (/app/config.js:40:19)
at /app/index.js:166:29
at Layer.handle [as handle_request] (/app/node_modules/.pnpm/express@4.18.2/node_modules/express/lib/router/layer.js:95:5)
at next (/app/node_modules/.pnpm/express@4.18.2/node_modules/express/lib/router/route.js:144:13)
at Route.dispatch (/app/node_modules/.pnpm/express@4.18.2/node_modules/express/lib/router/route.js:114:3)
at Layer.handle [as handle_request] (/app/node_modules/.pnpm/express@4.18.2/node_modules/express/lib/router/layer.js:95:5)
at /app/node_modules/.pnpm/express@4.18.2/node_modules/express/lib/router/index.js:284:15
at param (/app/node_modules/.pnpm/express@4.18.2/node_modules/express/lib/router/index.js:365:14) {
text: 'retrieve connection from pool timeout after 10001ms\n' +
' (pool connections: active=0 idle=0 limit=5)',
sql: null,
fatal: false,
errno: 45028,
sqlState: 'HY000',
code: 'ER_GET_CONNECTION_TIMEOUT'
}
型
1条答案
按热度按时间tcbh2hod1#
我不使用mariadb,但你的代码可能有潜在的问题,可能会导致这个问题。我相信这个错误是在pool.query()函数内部抛出的,而且由于这个函数是异步的(我假设),所以不能直接使用dbQuery中的try-catch块来捕获它的错误。要优雅地处理错误,您需要等待promise,然后能够捕获和处理错误。
字符串