我对承诺的理解并不完美 所以我不确定哪种代码是处理错误和异常情况的正确方法。
请帮助我正确编写代码。
第一。尝试 - 抓住续集器的promise.reject
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()。
第二。不要处理续集器的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()中捕获调用者。
以下续集使用功能将在快递功能下方使用。
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
也是承诺函数。
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);
}
});
});
}
答案 0 :(得分:0)
无需使用' async'如果您的函数返回一个promise,因为async
函数返回Promise
。
我的意思是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);
});
};
会在.catch(function(e) {
log.info('\nadminList not found :\n');
throw e;
})
前往getAdminList
。但是,如果您想处理错误并重新抛出,则可能出现错误。
async/await
添加,如果您不想更改错误,但是您想记录错误并将其重新抛出,可以重新抛出错误
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);
}
};
此外,如果你想{
field: 'CustomerRef',
value: '2083',
operator: '='
}
与try
{
timer1.Enabled = false;
//Set interval and tick-handler from designer
pauseTimer.Enabled = false;
pauseTimer.Enabled = true;
}
catch (Exception ex)
{
Logging.LogFatal("Scroll event capture error: ", ex);
}
try
{
//Assume that this is the refreshtimer. In the Tick-eventhandler
//you reset interval to default refresh intervals
timer1.Interval = 60000;
timer1.Enabled = false;
timer1.Enabled = true;
}
catch (Exception ex)
{
Logging.LogFatal("Scroll event capture error: ", ex);
}