与异步/等待中的REST API处理错误处理的最佳实践?
我正在为消息传递应用程序的快递后端进行REST API。现在,我正在使用尝试/捕获来处理服务器端错误:
router.get("/:id", async ({ params }, res) => {
try {
const user = await User.findById(params.id);
if (!user) {
res.status(400).json({ message: "Invalid User ID" });
return;
}
res.json(user);
} catch (e) {
res.status(500).json(e);
}
});
这是否可以,还是我应该使用更好的练习?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您应该知道所有类型的错误:
https://nodejs.dev/learln/learln/learl handlling-in-nodejs
https://wwwww.honeybadger.io/blog/blog/errors-nodejs/errors-nodejs/ BR>
您是专门询问
asych/等待
try/catch
,我建议此片段代码来自在这里:
/complete-node-bootcamp/blob/master/4-natours/after-section-14/utils/catchasync.js 需要写
尝试{} catch(){}
到处都是。First you should get to know all types of errors:
https://nodejs.dev/learn/error-handling-in-nodejs
https://www.honeybadger.io/blog/errors-nodejs/
You're asking specifically for
asych/await
withtry/catch
and I suggest this snippet code from here:and then use it like this:
so you don't need to write
try{} catch (){}
everywhere.虽然这里的所有答案都是正确的 - 捕获错误并在nodejs中记录它们的好习惯,但实际上您正在编写Nodejs环境下运行的ExpressJS应用程序,因此您应该遵循端点的Expressjs指南,其中端点的端点是图书馆。
这是一个链接 https://expressjs.com/en/guide/eror andling。 html
这样,您不需要以前的答案之一中的一个
catchasync
,您甚至不需要处理端点代码中可能发生的大多数错误...(除非当然,您想将它们用于休闲(例如未找到错误的错误和适当的错误代码)),因为expressjs定义的错误处理程序应在发生时为您处理错误。While all of the answers here are right - its good practice to catch errors and log them in nodejs, you are in fact writing expressjs application running under nodejs environment, and as such you should follow the guide of expressjs of handling errors in endpoints in that library.
Here is a link https://expressjs.com/en/guide/error-handling.html
With this, you dont need any
catchAsync
from one of the previous answers, you dont even to need to handle most of the errors that might happen in the endpoints code... (Unless you of course want to use them to your leisure (like NotFound errors and proper error code for it)) as the error handler defined for expressjs should handle the errors for you whenever they occure.您的代码是基本的,但已经很好。
当您遇到错误时,您可以在某个地方记录它们,这是一个很好的做法,因此您可以在系统的生活中跟踪和修复它们。您在那里的方式,如果错误不会发生,则可能会在不知道的情况下默默地影响用户。
您可以在任何本地日志中记录错误,也可以使用一些在线平台(有几个)以SaaS形式提供此服务。
Your code is basic, but already nice.
It's good practice, when you catch errors, that you log them somewhere so you can track and fix them over the life of your system. The way you did there, if the error doesn't happen to you, it could be silently affecting users without you knowing.
You can record the errors in any local log or you can also use some online platform (there are several) that offer this service in the form of SaaS.
您的代码适合小型项目。
如果您有很多检查或尝试捕获块,则可能需要考虑一个错误处理程序。您可以使用NEXT()函数将错误转发。
有关更多信息: https://expressjs.com/en/guide/eror andling。 html
Your code would be fine for small projects.
If you have alot of checks or try catch block you might want tot consider an error handler. You can forward the error with the next() function.
For more info: https://expressjs.com/en/guide/error-handling.html