【问题标题】:Error handling in nested async functions in express.jsexpress.js 中嵌套异步函数中的错误处理
【发布时间】:2020-05-11 20:44:38
【问题描述】:

我正在尝试编写一个微服务来向用户发送消息以验证他的电话号码。我正在研究微服务的一部分,其中向端点发送带有正确验证码的消息将触发将他的电话号码添加到 Firebase 的代码。但是,我想为用户满意度和调试目的提供良好的错误处理。我已经阅读了快速文档。起初,我只是在异步函数中抛出了一个错误,但我收到一个警告,即未处理的承诺异常将在未来以非零退出状态终止服务器。如果我使用 express 内置的 next 或 end 函数处理 catch 块中的错误,则代码不会完全终止(即使文档说它确实如此)。这会导致代码运行我的代码,向用户发送成功消息(覆盖我之前的错误消息)。即使我检查是否已发送标头,它仍然不会阻止成功响应覆盖我的消息。

如果您提供正确的信息,该代码将正常工作。重要的是在函数 validateVerificationCode 中抛出一个错误以记录并将正确的错误发送给客户端。当我触发导致以下错误的端点时,我会收到以下信息。

编辑: 以下是人们可能认为的可能解决方案。但是,在 express 中,每个抛出错误的异步函数也必须捕获错误。因此,firebase 查询必须捕获错误。

   public async verifyVerification(
    req: Request,
    res: Response,
    next: NextFunction
  ) {
    const { uid, verificationCode } = req.params;
    await this.validateVerificationCode(uid, verificationCode, res, next)
      .then(() => {
        console.log("Verifying user sms verification code complete");
      })
      .catch(error => {
        res.status(500);
        res.json({ error: error });
      });

    res.json({ success: "success" });

  }

  public async validateVerificationCode(
    uid: string,
    verificationCode: string,
    res: Response,
    next: NextFunction
  ) {
    const query = firestore.collectionGroup("PublicUser");

    const publicUserQuery = query.where("uid", "==", uid);
    publicUserQuery
      .limit(1)
      .get()
      .then((querySnapshot: any) => {
        querySnapshot.forEach((documentSnapshot: any) => {
          const smsVerificationInfo = documentSnapshot.get(
            "smsVerificationInfo"
          );

          if (
            !smsVerificationInfo ||
            smsVerificationInfo.validUntil.seconds <
              Firestore.Timestamp.now().seconds
          ) {
            throw Error(
              "SMS has expired or does not exist. Please send new request"
            );
          } else if (smsVerificationInfo.verificationCode != verificationCode) {
            throw Error("Incorrect verification code");
          }

          documentSnapshot.ref.update({
            smsVerificationInfo: FieldValue.delete()
          });
          documentSnapshot.ref.set(
            {
              verifiedPhoneNumber: smsVerificationInfo.phoneNumber
            },
            { merge: true }
          );
        });
      })
  }





[start:dev] Server is listening on port 4000
[start:dev] Verifying user sms verification code complete
[start:dev] (node:77536) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[start:dev]     at ServerResponse.setHeader (_http_outgoing.js:535:11)
[start:dev]     at ServerResponse.header (/Users/rahmijamalpruitt/Documents/SideProjects/SonderPhoneMicroService/node_modules/express/lib/response.js:771:10)
[start:dev]     at ServerResponse.send (/Users/rahmijamalpruitt/Documents/SideProjects/SonderPhoneMicroService/node_modules/express/lib/response.js:170:12)
[start:dev]     at ServerResponse.json (/Users/rahmijamalpruitt/Documents/SideProjects/SonderPhoneMicroService/node_modules/express/lib/response.js:267:15)
[start:dev]     at ServerResponse.send (/Users/rahmijamalpruitt/Documents/SideProjects/SonderPhoneMicroService/node_modules/express/lib/response.js:158:21)
[start:dev]     at /Users/rahmijamalpruitt/Documents/SideProjects/SonderPhoneMicroService/dist/controllers/smsVerificationController.js:149:25
[start:dev]     at processTicksAndRejections (internal/process/task_queues.js:97:5)
[start:dev] (node:77536) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
[start:dev] (node:77536) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Response {
   "status": 200,
   "success": "success"
}

【问题讨论】:

  • 在您的res 中,不要忘记添加return。它看起来像这样:return res.send(...).
  • @TitusSutioFanpula 不会改变任何东西。
  • 我不知道您在catch() 中做什么,因为在那里,您向客户端发送请求,但您在其中添加了next 函数。
  • 您没有对 Firestore API 调用返回的两个承诺做任何事情。而且由于您在 validateVerificationCode 中根本没有返回承诺或使用 await,因此该函数调用上的 await 没有任何帮助。而且我有点困惑,为什么您没有在代码中统一使用 async/await 语法。
  • 如果你对下一步做什么感到困惑,它是一个内置函数来处理异步函数中的错误处理。在文档中它说“如果您将任何内容传递给 next() 函数(字符串 'route' 除外),Express 会将当前请求视为错误,并将跳过任何剩余的非错误处理路由和中间件功能”如果您对为什么我在下次调用之前发送一些东西感到困惑,我希望能够在我告诉 express 停止执行代码之前告诉客户端发生了哪个错误。 @TitusSutioFanpula

标签: node.js typescript firebase express google-cloud-firestore


【解决方案1】:

为了解决发送冲突响应的问题。我将响应处理完全排除在 verifyVerification 函数之外。我在 response.locals 对象中传递响应。

router.get(
  "/verify/:uid/:verificationCode",
  async (req: Request, res: Response, next: NextFunction) => {
    await smsVerificationController.verifyVerification(req, res, next);

    if (res.locals.error) {
      res.status(404).send({ error: res.locals.error.toString() });
    } else {
      res.status(200).json({
        success: "success"
      });
    }
  }
);

此外,我结合了该功能以获得更好的可读性。但是,此方法也适用于深度嵌套的异步函数。

public async verifyVerification(
    req: Request,
    res: Response,
    next: NextFunction
  ) {
    const { uid, verificationCode } = req.params;

    const query = firestore
      .collectionGroup("PublicUser")
      .where("uid", "==", uid)
      .limit(1)
      .get();

    await query
      .then((snapshot: any) => {
        snapshot.forEach((documentSnapshot: any) => {
          const smsVerificationInfo = documentSnapshot.get(
            "smsVerificationInfo"
          );

          if (
            !smsVerificationInfo ||
            smsVerificationInfo.validUntil.seconds <
              Firestore.Timestamp.now().seconds
          ) {
            throw Error(
              "SMS has expired or does not exist. Please send new request"
            );
          } else if (smsVerificationInfo.verificationCode != verificationCode) {
            throw Error("Incorrect verification code");
          }

          documentSnapshot.ref.update({
            smsVerificationInfo: FieldValue.delete()
          });

          documentSnapshot.ref.set(
            {
              verifiedPhoneNumber: smsVerificationInfo.phoneNumber
            },
            { merge: true }
          );
        });
      })
      .catch((error: Error) => {
        res.locals.error = error;
        next(error);
      });
  }

【讨论】:

    【解决方案2】:

    它会抛出错误,因为您发送了两次响应。 第一个在validateVerificationCode,第二个在verifyVerification

    你不应该把 async 和 promise 结合起来。

    public async verifyVerification(req, res, next) {
        const {
            uid,
            verificationCode
        } = req.params;
    
        try {
            await this.validateVerificationCode(uid, req, res, next);
            res.status(200).json({
                success: 'success'
            });
        } catch(error => {
            res.status(404).send({ error });
        });
    }
    
    
    public async validateVerificationCode(uid, verificationCode, next) {
        const querySnapShot = await firestore.collectionGroup("PublicUser").where("uid", "==", uid).limit(1).get();
    
        querySnapShot.forEach(documentSnapshot => {
            const smsVerificationInfo = documentSnapshot.get('smsVerificationInfo');
            const invalidTime = smsVerificationInfo.validUntil.seconds < Firestore.Timestamp.now().seconds;
    
            if(!smsVerificationInfo || invalidTime) {
                throw new Error('SMS has expired or does not exists. Please send new request');
            }
    
            if(smsVerificationInfo.verificationCode !== verificationCode) {
                throw new Error('Incorrect verification code');
            }
    
    
            documentSnapshot.ref.update({
                smsVerificationInfo: FieldValue.delete()
            });
    
            documentSnapshot.ref.set({
                verifiedPhoneNumber: smsVerificationInfo.phoneNumber
            }, { 
                merge: true 
            });
        });
    }
    

    【讨论】:

    • 感谢您的意见。如前所述,如果我没有在 validateVerificationCode 中捕获查询快照中的错误,我将收到 unhandle promise 警告。这就是为什么,我正在使用下一个。感谢@TitusSutioFanpula,我同意使用 next 两次是矫枉过正。不过,如果我在 validateVerificationCode 中捕捉到承诺,它不会触发在 verifyVerification 中的捕捉。导致它发送 response.send({}|)
    猜你喜欢
    • 2018-10-05
    • 2020-01-10
    • 2021-08-03
    • 1970-01-01
    • 2014-05-02
    • 2018-04-02
    • 2019-09-17
    • 2019-02-10
    相关资源
    最近更新 更多