如何捕获 AuthenticationError?
How to catch AuthenticationError?
我有以下注册服务器功能。当我显然传递错误的登录名和密码时,我得到了错误。在我的 React 应用程序中,我在控制台中观察到以下 message:Uncaught(承诺)Error:Authentication 错误。
我当然会向用户显示消息,但我应该避免在控制台中出现“未处理”的情况吗?如何避免,因为老实说,我不知道如何在这里使用 catch?
signIn: async (parent, {username,email,password},{models}) => {
if (email){
email = email.trim().toLowerCase()
}
const user = await models.User.findOne({
$or: [{email},{username}]
});
//user not found
if(!user){
throw new AuthenticationError('Authentication Error')
}
const valid = await bcrypt.compare(password,user.password)
//console.log(valid)
if(!valid){
throw new AuthenticationError('Authentication Error')
}
//Creation token and return
return jwt.sign({id: user._id}, process.env.JWT_SECRET);
},
你在登录函数的调用中尝试过简单的 Try-Catch 吗?
try{
...
signIn(....)
...
}catch(Exception e) {
//console.log(e) ->should be 'Authentication error'
//Block of code to handle errors
}
我有以下注册服务器功能。当我显然传递错误的登录名和密码时,我得到了错误。在我的 React 应用程序中,我在控制台中观察到以下 message:Uncaught(承诺)Error:Authentication 错误。
我当然会向用户显示消息,但我应该避免在控制台中出现“未处理”的情况吗?如何避免,因为老实说,我不知道如何在这里使用 catch?
signIn: async (parent, {username,email,password},{models}) => {
if (email){
email = email.trim().toLowerCase()
}
const user = await models.User.findOne({
$or: [{email},{username}]
});
//user not found
if(!user){
throw new AuthenticationError('Authentication Error')
}
const valid = await bcrypt.compare(password,user.password)
//console.log(valid)
if(!valid){
throw new AuthenticationError('Authentication Error')
}
//Creation token and return
return jwt.sign({id: user._id}, process.env.JWT_SECRET);
},
你在登录函数的调用中尝试过简单的 Try-Catch 吗?
try{
...
signIn(....)
...
}catch(Exception e) {
//console.log(e) ->should be 'Authentication error'
//Block of code to handle errors
}