无法捕捉到错误

时间:2017-10-31 23:54:53

标签: angular typescript ionic-framework error-handling try-catch

我有一个功能:

buy() {
   return new Promise((resolve, reject) => {
     this.http.request('http://192.168.1.131:8888/generatetoken.php')
    .subscribe(res => { 
      resolve(res.text());    
    });
   }).then((key) => {
     braintree.dropin.create({
      authorization: key,
      container: '#dropin-container'
    }, (createErr, instance) => {
      if (createErr) {
        // An error in the create call is likely due to
        // incorrect configuration values or network issues.
        // An appropriate error will be shown in the UI.
        console.error(createErr + "      createErrrrrrrrrrrrrrrrrr");
        return;
      }

        instance.requestPaymentMethod((requestPaymentMethodErr, payload) => {
          if (requestPaymentMethodErr) {
            // No payment method is available.
            // An appropriate error will be shown in the UI.
            console.error(requestPaymentMethodErr + "      reqqe");
            return;
          }

          // Submit payload.nonce to your server
        });

    });
   }).catch(error => console.log(error + "    eoe"));



 }

运行时输出:

console.error: ERROR [object Object]

我在哪里可以发现错误?我追赶then(),但我没有看到任何其他我能抓到的地方。请帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

此代码中唯一可以抛出错误的地方是初始http请求。

如果该请求出错,请确保您有办法调用reject。根据我所知的可观察量(不多),也许这会起作用:

this.http.request('http://192.168.1.131:8888/generatetoken.php')
    .subscribe(
        res => { resolve(res.text()); },
        err => { reject(err); }
    );