user3082488
user3082488

Reputation: 71

how to catch an error and console log it? Angular typescript

private async getData(): Promise<Array<DataSet>> {
// the next line returns an error
        this.portalConf = await GetProcedure.exe(this.portalId ? this.portalId : PortalStore.getPortalId());
        this.portal = new Portal(this.portalConf.name, this.portalConf.id, this.portalConf.content, '');
        DataSet.resetStore();
        return GetData.getDatasets(this.portal);
    }

the "this.conf" line returns an error , how can I catch that error and act on it ?

Upvotes: 1

Views: 1482

Answers (1)

Anirudh Kanukanti
Anirudh Kanukanti

Reputation: 308

You can use the below code: simply use try and catch blocks

private async getData(): Promise < Array < DataSet >> {
  try {
    // the next line returns an error
    this.portalConf = await GetProcedure.exe(this.portalId ? this.portalId : PortalStore.getPortalId());
    this.portal = new Portal(this.portalConf.name, this.portalConf.id, this.portalConf.content, '');
    DataSet.resetStore();
    return GetData.getDatasets(this.portal);
  } catch (error) {
    console.log('error', error);
  }
}

Upvotes: 2

Related Questions