NESTJS从控制器中的空结果中抛出异常

发布于 2025-02-09 03:21:46 字数 1089 浏览 1 评论 0原文

我有一个调用服务的控件。 如果服务从DB返回空的有效载荷,我想提出异常。

目前,我在服务中这样做: 这是我目前提供的服务。

async getPreferences(eUserId: string): Promise<UserPreferences> {

    const userPreferences = await this.userPreferencesModel.findOne({
      eUserId,
    });

    if (!userPreferences) {
      throw new NotFoundException("We couldn't find your user preferences");
    }

    return userPreferences;
  }

我希望控制器处理异常,问题是控制器响应是一个承诺。 我该如何处理?

这就是我剃须的操作:

@Get()
  async getPreferences(
    @Headers('x-e-user-id') eUserId: string,
  ): Promise<UserPreferences> {
  
    const userPreferences = this.userPreferencesService.getPreferences(eUserId);

    console.log('userPreferences: ', userPreferences);
    // Here is what I am trying to monitor...
    if (userPreferences) {
      throw new NotFoundException("We couldn't find your user preferences");
    }

    return userPreferences;
  }

控制器返回中的ther console.log:

userPreferences:  Promise { <pending> }

现在,如果服务响应为空,则毫无例外。

我如何监视服务结果以进行异常

I have a control that calls a service.
If the service returns an empty payload from the db I want to throw an exception.

at the moment I am doing that in the service:
this is the service I have at the moment with the exception.

async getPreferences(eUserId: string): Promise<UserPreferences> {

    const userPreferences = await this.userPreferencesModel.findOne({
      eUserId,
    });

    if (!userPreferences) {
      throw new NotFoundException("We couldn't find your user preferences");
    }

    return userPreferences;
  }

I want the controller to handle the exception, The issue is that the controller response is a Promise.
How can I handle that?

This is what I shave done:

@Get()
  async getPreferences(
    @Headers('x-e-user-id') eUserId: string,
  ): Promise<UserPreferences> {
  
    const userPreferences = this.userPreferencesService.getPreferences(eUserId);

    console.log('userPreferences: ', userPreferences);
    // Here is what I am trying to monitor...
    if (userPreferences) {
      throw new NotFoundException("We couldn't find your user preferences");
    }

    return userPreferences;
  }

Ther console.log in the controller returns:

userPreferences:  Promise { <pending> }

Now, if the service response is empty no exception is thrown.

How can I monitor the service result in order to throw an exception

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2025-02-16 03:21:46

您可以解决此问题的多种方法。这是一个。

不要在服务中丢失错误,只需返回结果或null

async getPreferences(eUserId: string): Promise<UserPreferences | null> {
  return this.userPreferencesModel.findOne({
    eUserId,
  });
}

然后,在您的控制器等待结果中,您忘记了此。这就是为什么您看到待决的承诺的原因。结果解决后,请检查是否返回任何用户首选项,然后扔notFoundException(如果不是)。

@Get()
async getPreferences(@Headers('x-e-user-id') eUserId: string): Promise<UserPreferences> {
  const userPreferences = await this.userPreferencesService.getPreferences(eUserId);
  if (!userPreferences) {
    throw new NotFoundException("We couldn't find your user preferences");
  }

  return userPreferences;
}

我不会扔notfoundException或您服务中的任何其他HTTP相关错误。将该责任留给您的控制器,不要将您的服务(逻辑)与HTTP错误代码联系起来。在这里抛出不知道它们正在使用的上下文(HTTP)的错误。PS

:您还可以考虑通过查询字符串或路由参数传递用户ID,而不是通过标头来传递用户ID。

Multiple ways you can solve this. Here's one.

Don't throw an error in your service, just return the result or null.

async getPreferences(eUserId: string): Promise<UserPreferences | null> {
  return this.userPreferencesModel.findOne({
    eUserId,
  });
}

Then in your controller await for the result, you forgot this. That's why you are seeing a pending promise. After the result has been resolved, check if any user preferences were returned and throw the NotFoundException if not.

@Get()
async getPreferences(@Headers('x-e-user-id') eUserId: string): Promise<UserPreferences> {
  const userPreferences = await this.userPreferencesService.getPreferences(eUserId);
  if (!userPreferences) {
    throw new NotFoundException("We couldn't find your user preferences");
  }

  return userPreferences;
}

I would not throw NotFoundException or any other HTTP related error from your service. Leave that responsibility up to your controller, don't tie your service (logic) to HTTP error codes. Throw errors here that are not aware of the context (HTTP) they are being used in.

PS: You might also consider passing the user ID via the query string or as a route parameter instead of via the headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文