TypeScript:未捕获Promise内部错误

问题描述

我正在使用TypeScript开发SPFx WebPart。

我有一个名称获得团队的功能(get()也返回了一个承诺):

  public getTeamChannelByName(teamId: string,channelName: string) {
    return new Promise<MicrosoftGraph.Channel>(async (resolve,reject) => {
      this.context.msGraphClientFactory
        .getClient()
        .then((client: MSGraphClient) =>{
          client
            .api(`/teams/${teamId}/channels`)
            .filter(`displayName eq '${channelName}'`)
            .version("beta")
            .get((error,response: any) => {
              if ( response.value.length == 1) {
                const channels: MicrosoftGraph.Channel[] = response.value;
                resolve(channels[0]);
              } else if (response.value.length < 1) {
                reject(new Error("No team found with the configured name"));
              } else {
                reject(new Error("Error XY"));
              }
            });
          })
        .catch(error => {
          console.log(error);
          reject(error);
        });
    });
  }

我这样称呼这个函数

  public getConversations(teamName: string,channelName: string,messageLimitTopics: number = 0,messageLimitResponses: number = 0) {
    return new Promise<any>(async (resolve,reject) => {
      try {
        this.getTeamGroupByName(teamName)
          .then((teamGroup: MicrosoftGraph.Group) => {
            const teamId: string = teamGroup.id;
            this.getTeamChannelByName(teamId,channelName)
              .then((teamChannel: MicrosoftGraph.Channel) => {
                const channelId: string = teamChannel.id;
                this.getChannelTopicmessages(teamId,channelId,messageLimitTopics)
                  .then((messages: MicrosoftGraph.Message[]) => {
                    const numberOfMessages = messages.length;
                    ... // omitted
                  });
              });
          });
      } catch(error) {
        reject(error);
      }
    });
  }

这个getConversations()函数本身是从我的Webpart代码调用的:

  public getConversations() {
    if (this.props.teamName && this.props.teamName.length > 0 &&
        this.props.channelName && this.props.channelName.length > 0) {
      GraphService.getConversations(this.props.teamName,this.props.channelName,this.props.messageLimitTopics,this.props.messageLimitResponses)
          .then((conversations) => {
              .. // omitted
          })
          .catch((err: Error) => {
            console.log(err);
            this.setState({errorMessage: err.message});
          });
    } else {
      // Mandatory settings are missing
      this.setState({errorMessage: strings.MandatorySettingsMissing});
    }
  }

因此,如您所见,在上面,我想写出从reject函数内部的getConversations()收到的错误(消息)。问题是,我没有收到此错误的拒绝,但是在控制台中,我看到以下内容

Uncaught Error: No team found with the configured name

我在.catch()添加了您在上方看到的getTeamChannelByName()块,但这在调试过程中没有被击中。

在兑现承诺方面工作不多,我仍然对它们有些困惑,所以我想我可能是错误地构造了承诺链,也许是将捕获块放在了错误的位置? / p>

解决方法

我查看了与Promises一起犯的常见错误,当然也有所谓的“ Promise Hell”。

所以,我还不能100%确定,但是我想,这样做的结果是我的.then("Promise")没有追赶阻拦,所以拒绝无济于事。

我现在已将调用方法更改为此,并且还删除了try / catch块,因为这是不必要的:

public getConversations(teamName: string,channelName: string,messageLimitTopics: number = 0,messageLimitResponses: number = 0) {
    return new Promise<any>(async (resolve,reject) => {
      let teamId: string = null;
      let channelId: string = null;
      this.getTeamGroupIdByName(teamName)
        .then(tId => {
          teamId = tId;
          return this.getTeamChannelIdByName(teamId,channelName);
        })
        .then(cId => {
          channelId = cId;
          return this.getChannelTopicMessages(teamId,channelId,messageLimitTopics);
        })
        .catch(error => {
          reject(error);
        });
    });
  }