繁体   English   中英

在 NodeJS 中抛出错误 - 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM