捕获异常后继续程序

时间:2019-12-16 20:10:15

标签: node.js loops try-catch

在我的nodejs应用程序中,我通过遍历端点来使用http服务。我已经使用try catch块来处理服务是否关闭或无法正常工作。如果服务无法正常工作,我想在不破坏代码的情况下继续执行下一个服务。在catch块中使用continue时会出现Jump target cannot cross function boundary错误。我如何才能继续执行程序,以使它遍历所有端点而不中断执行?

@Injectable()
export class MessageService {
  publishMessages(productEvent: ProductEvent): Observable<any> {
    let json = '';

    const url = this.configService.get('SITEMAP_ENDPOINT');
    const urls = [url + '/sitemap/retrieve', url + '/sitemap/retrieve/abz'];
    try {
      for (let i = 0; i < urls.length; i++) {
        return https.get(urls[i], res => {
          res.on('data', function(chunk) {
            json += chunk;
          });
          res.on('end', function() {
            const result = JSON.parse(json);
            var splitCodes = productEvent.productCodes.split(',');
            for (let i = 0; i < result.length; i++) {
              for (let j = 0; j < result[i].products.length; j++) {
                if (
                  productEvent.productCodes.length !== 0
                ) {
                  for (let k = 0; k < splitCodes.length; k++) {
                    if (result[i].products[j].productCode === splitCodes[k]) {
                      console.log(
                       result[i].products[j].productCode
                       );
                    }
                  }
                } else if (
                  productEvent.vehicleType.length !== 0 &&
                  productEvent.vehicleType !== undefined
                ) {
                  for (let k = 0; k < productEvent.vehicleType.length; k++) {
                  ///some work here
                  }
                } else {
                  console.log('No data found');
                  return;
                }
              }
            }
          });
        });
      }
    } catch (err) {
      console.error(err);
      continue;     -----------------> HERE
    } 
  }
}

0 个答案:

没有答案