【发布时间】: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