处理promise.reject尝试catch或promise.catch
我对承诺的理解并不完美。
所以我不确定哪个代码是正确的方式来处理错误和exception情况。
请帮我写正确的代码。
1。 尝试 – 赶上续集者的诺言
async function doGetAdminList(adminName) { let adminList; try { adminList = await sequelize.query( sqls.GET_ADMIN_LIST, { replacements: { adminName: adminName }, type: sequelize.QueryTypes.SELECT } ); } catch (e) { return Promise.reject({status:500, message: "SQL Error" }); } if (!adminList || !Object.keys(adminList).length) { log.info('\nadminList not found :\n'); return Promise.reject({status:400, message: 'adminList not found.' }) } return adminList; }
为此,我想知道try-catch是否能够捕获续集器的promise.catch()。
第2位。 不要处理续集器的promise.reject
async function doGetAdminList(adminName) { let adminList; adminList = await sequelize.query( sqls.GET_ADMIN_LIST, { replacements: { adminName: adminName }, type: sequelize.QueryTypes.SELECT } ); if (!adminList || !Object.keys(adminList).length) { log.info('\nadminList not found :\n'); return Promise.reject({status:400, message: 'adminList not found.' }) } return adminList; }
为此,我不知道sequelizer的promise.reject()是否可以通过调用者函数并在promise.catch()函数中捕获调用者。
以上使用函数将在以下expression函数中使用。
adminController.js
const jwtAuth = require('../common/jwtAuth.js'); exports.getAdminList = function (req, res) { res.setHeader("Content-Type", "application/json; charset=utf-8"); if (!req.body.adminName) { return res.status(400).json({ message: 'adminName is empty.' }); } jwtAuth(req.headers.accesstoken) .then((decoded) => { worker = decoded.loginName; return doGetAdminList(adminName); }) .then((adminList) => { log.info("getAdminList() finish"); res.status(200).json(adminList); }) .catch(e => { log.error(e); return res.status(e.status).json(e); }); };
jwtAuth.js
也是promise函数。
const jwt = require('jsonwebtoken'); module.exports = async function verifyJwt(token) { return await new Promise((resolve, reject) => { if (!token) { reject({status:401, message:'Empty token'}); return; } jwt.verify(token,"dipa",function(err, decoded){ if(err) { reject({status:401, message:'TokenExpiredError'}); } else { resolve(decoded); } }); }); }
如果你的函数返回一个promise,就不需要使用“async”,因为async
函数返回一个Promise
。
我的意思是, var somethink = await doSomethink()
的结果是var somethink = await doSomethink()
它不是承诺它的对象,因为你从一个async
函数返回它作为Promise.resolve(somethink )
返回。
所以你的'jwtAuth.js'更好没有
const jwt = require('jsonwebtoken'); module.exports = function verifyJwt(token) { return new Promise((resolve, reject) => { if (!token) { reject({status:401, message:'Empty token'}); return; } jwt.verify(token,"dipa",function(err, decoded){ if(err) { reject({status:401, message:'TokenExpiredError'}); } else { resolve(decoded); } }); }); }
同样如此
function doGetAdminList(adminName) { let adminList; return sequelize.query( sqls.GET_ADMIN_LIST, { replacements: { adminName: adminName }, type: sequelize.QueryTypes.SELECT } ).catch((e)=> { //here you catch you sequelize error which can be anything //you can either catch and throw a new Error log.info('\nadminList not found :\n'); throw Error({ status: 500, message: "SQL Error" }) }) }
关于getAdminList
和最后的catch
。
如果jwtAuth
或doGetAdminList
引发错误.catch
将收到错误。
如果在doGetAdminList
您不会在.catch
上sequelize.query
那么sequelize error
将会传播到您的catch
中。 但是,如果你想处理错误,并重新抛出错误是可能的。
const jwtAuth = require('../common/jwtAuth.js'); exports.getAdminList = function (req, res) { res.setHeader("Content-Type", "application/json; charset=utf-8"); if (!req.body.adminName) { res.status(400).json({ message: 'adminName is empty.' }); } jwtAuth(req.headers.accesstoken) .then((decoded) => { worker = decoded.loginName; return doGetAdminList(adminName); }) .then((adminList) => { log.info("getAdminList() finish"); res.status(200).json(adminList); }) .catch(e => { //the message with mess "SQL Error" will travel here. log.error(e); res.status(e.status).json(e); }); };
添加,如果你不想改变错误,但是你想logging和处理错误,你可以重新抛出它
.catch(function(e) { log.info('\nadminList not found :\n'); throw e; })
另外,如果你想做async/await
getAdminList
exports.getAdminList = async function(req, res) { res.setHeader("Content-Type", "application/json; charset=utf-8"); if (!req.body.adminName) res.status(400).json({message: 'adminName is empty.'}); try { let decoded = await jwtAuth(req.headers.accesstoken) worker = decoded.loginName; let adminList = await doGetAdminList(req.body.adminName); log.info("getAdminList() finish"); res.status(200).json(adminList); } catch (e) { //the message with mess "SQL Error" will travel here. log.error(e); res.status(e.status).json(e); } };