[英]NodeJS program is not exiting after throwing an error UnhandledPromiseRejection
[英]Throwing errors in NodeJS - UnhandledPromiseRejection
我對 NodeJS 還很陌生,我仍在努力弄清楚如何處理錯誤。 我閱讀了很多問題,但我沒有意識到我做錯了什么。
這里我有一個登錄功能:
export const login = async (req, res) => {
let body = req.body;
try {
const user = await User.findOne({ username: body.username });
if (!user) {
throw new InternalError("Username or password are wrong");
}
if (!bcrypt.compareSync(body.password, user.password)) {
throw new InternalError("Username or password are wrong");
}
let token = jwt.sign({ data: user }, "secret", {
expiresIn: 60 * 60 * 24 * 30
});
return res.json({
user: user,
token: token
});
} catch (error) {
throw new GenericError(error);
}
};
這就是我得到的錯誤,例如,我輸入了錯誤的密碼:
(node:12332) UnhandledPromiseRejectionWarning: GenericError: Username or password are wrong
(node:12332) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by
throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()
我知道用.catch()
做出的承諾必須有.catch()
.then()
和.catch()
但這不是執行承諾時的錯誤,而是驗證密碼時的錯誤。 我想要的是拋出錯誤並獲得響應。 現在請求永遠不會結束並顯示上一個錯誤。
謝謝!
PS:InternalError 和 GenericError 只是我創建的從 Error 擴展而來的錯誤
您收到來自節點的警告,因為您在捕獲 InternalError 后重新拋出 GenericError。 您應該在調用login
時返回您的響應或捕獲您的 GenericError 而不是重新拋出 GenericError 。
這是您為兩者修改的代碼。
export const login = async (req, res) => {
// We get the body info
let body = req.body;
try {
// We find the user with the username from the body
const user = await User.findOne({ username: body.username });
// Let's assume no user exists, so we throw an InternalError,
// This skips straight to the catch block.
if (!user) {
throw new InternalError("Username or password are wrong");
}
// We never reach these two statements
// because of the error above being thrown.
if (!bcrypt.compareSync(body.password, user.password)) {
throw new InternalError("Username or password are wrong");
}
let token = jwt.sign({ data: user }, "secret", {
expiresIn: 60 * 60 * 24 * 30
});
res.json({
user: user,
token: token
});
} catch (err) {
// We caught our InternalError from not having a user above.
// Now we should return a response that the user is invalid.
// I am just going to return the error that we previously threw.
res.json({
error: err
});
}
};
您絕對可以從 catch 塊中拋出 GenericError 。 但是,這樣做需要您在調用登錄函數的任何地方捕獲 GenericError。
export const login = async (req, res) => {
// We get the body info
let body = req.body;
try {
// We find the user with the username from the body
const user = await User.findOne({ username: body.username });
// Let's assume no user exists, so we throw an InternalError,
// This skips straight to the catch block.
if (!user) {
throw new InternalError("Username or password are wrong");
}
// We never reach these two statements
// because of the error above being thrown.
if (!bcrypt.compareSync(body.password, user.password)) {
throw new InternalError("Username or password are wrong");
}
let token = jwt.sign({ data: user }, "secret", {
expiresIn: 60 * 60 * 24 * 30
});
res.json({
user: user,
token: token
});
} catch (err) {
// Throw the generic error
throw GenericError(err);
}
};
try {
// We call the login function that is defined above.
// It is going to throw a GenericError, so we have to
// catch it.
await login(req, res);
} catch (err) {
// Now we need to catch the generic error
res.json({
error: err
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.