Nestjs-如何构造代码以优雅处理错误?
我正在努力处理Nestjs应用程序中的错误,因为我在任何地方都在写尝试/捕捉。我也不确定在服务或解决方案/控制器中丢弃错误,还是在某些拦截器或异常过滤器中捕获并重新捕获它是清洁/最佳实践。还在用fn()。然后编写代码。
这是我的代码的一个示例,其中有许多样板代码处理错误的错误处理。
您将如何更干净和干燥处理错误?
async validateUser(
email: string,
plainTextPassword: string,
): Promise<User | null> {
try {
const user = await this.usersRepository.findOne({ email });
if (!user) {
throw new HttpException(
"Wrong credentials provided",
HttpStatus.BAD_REQUEST,
);
}
const isMatch = await this.verifyPassword(
plainTextPassword,
user?.password,
);
if (isMatch) {
await this.usersRepository.filter(user);
return user;
}
} catch (err) {
this.logger.error(err);
throw new HttpException(
"Something went wrong",
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
/** This function is used by the validateUser function
* Note: This could be written much much shorter in 1-2 lines...
*/
async verifyPassword(plainTextPassword: string, hashedPassword: string) {
try {
const isMatch = await bcrypt.compare(plainTextPassword, hashedPassword);
if (!isMatch) {
throw new HttpException(
"Wrong credentials provided",
HttpStatus.BAD_REQUEST,
);
}
return isMatch;
} catch (err) {
this.logger.error(err);
throw new HttpException(
"Something went wrong",
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
I'm struggling to handle errors in my NestJS app because I'm writing try/catch everywhere. I'm also not sure whether it's cleaner/best practice to throw errors in a service or in the resolver/controller, or catch it and rethrow it in some interceptor or exception filter. Also is writing code with fn().then().catch()
cleaner since I seem to be rewriting the same errors a lot when using try/catch.
Here is an example of my code with poor error handling with lots of boilerplate code.
How would you handle the errors more cleanly and DRY?
async validateUser(
email: string,
plainTextPassword: string,
): Promise<User | null> {
try {
const user = await this.usersRepository.findOne({ email });
if (!user) {
throw new HttpException(
"Wrong credentials provided",
HttpStatus.BAD_REQUEST,
);
}
const isMatch = await this.verifyPassword(
plainTextPassword,
user?.password,
);
if (isMatch) {
await this.usersRepository.filter(user);
return user;
}
} catch (err) {
this.logger.error(err);
throw new HttpException(
"Something went wrong",
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
/** This function is used by the validateUser function
* Note: This could be written much much shorter in 1-2 lines...
*/
async verifyPassword(plainTextPassword: string, hashedPassword: string) {
try {
const isMatch = await bcrypt.compare(plainTextPassword, hashedPassword);
if (!isMatch) {
throw new HttpException(
"Wrong credentials provided",
HttpStatus.BAD_REQUEST,
);
}
return isMatch;
} catch (err) {
this.logger.error(err);
throw new HttpException(
"Something went wrong",
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是一些建议:
httpexception
,例如notfoundexception
,forbidDenexception
和internalSeralSeralSeralSeralSeralSeralSeralSeralSeralSeralSeralSeralRorexception
。参见 this 。internalServererRorexception
s,因为Nestjs的默认过滤器将处理任何未透露的错误并亲自抛出500个错误。Here are some suggestions:
HttpException
, such asNotFoundException
,ForbiddenException
andInternalServerErrorException
. See this.InternalServerErrorException
s, since NestJS's default filter will handle any uncaught error and throw the 500 himself.