2014-09-03 30 views
4

到目前为止,我有大约4呼叫使用Restangular我的API,并在他们,我已经在当时的第二个参数检查他们中的每一个,如下图所示:通用错误处理器为Restangular

Restangular.all("accounts").getList().then(function() { 
    console.log("All ok"); 
}, function(response) { 
    console.log("Error with status code", response.status); 
}); 

正如你所看到的,这不是一个可维护的方法,因为这会在整个应用程序中创建大量重复的错误处理代码。

据我所知,errorInterceptor存在,但我无法想象用它来创建一个通用的错误处理程序。

我希望有一些新的想法可以帮助我解决这个问题。

谢谢:)

回答

2

我有类似的问题。 errorInterceptor的问题在于它在任何回调之前被调用,所以我们有一个选择,不仅在我们需要的时候做一些事情。我用黑客的方式解决了这个问题,所以无论你是否在代码中使用它,都取决于你。做工精细的:

"angular": "1.3.0", 
"restangular": "1.4.0", 

第一,我用errorInterceptor:

RestangularConfigurer.setErrorInterceptor(function (response, deferred, responseHandler) { 
    var hasSomeErrback = deferred.promise.$$state.pending.map(function (thenAttachment) { 
     if (thenAttachment[2]) { 
      return true 
     } 
     else { 
      return false 
     } 
    }).reduce(function (accumulator, value) { 
     return accumulator || value 
    }, false) 

    if (!hasSomeErrback) { 
     myErrorService.globalError() 
    } 
}) 

但不是调用全局错误处理程序(myErrorService.globalError())每次我这样做只有当没有错误回调时间注册。这是相当哈克,因为它使用的承诺目标的INTERAL数据,但似乎工作,看测试:

describe('when server responded with error', function() { 
    beforeEach(function() { 
     spyOn(myErrorService, 'globalError') 
     $httpBackend.whenGET('/api/evil').respond(500, 'pure evil') 
    }) 

    it('should call global error handler if error callback was NOT attached', function() { 
     myApiClient.all('evil').getList().then(function() { 

     }) 

     $httpBackend.flush() 

     expect(myErrorService.globalError).toHaveBeenCalled() 

    }) 

    it('shouldnt call global error handler if error callback was attached', function() { 
     myApiClient.all('evil').getList().then(function() { 

     }, function() { 
      console.log('Inside local catch') 
     }) 

     $httpBackend.flush() 

     expect(myErrorService.globalError).not.toHaveBeenCalled() 
    }) 

    it('shouldnt call global error handler if error callback was attached using catch', function() { 
     myApiClient.all('evil').getList().catch(function() { 

     }) 

     $httpBackend.flush() 

     expect(myErrorService.globalError).not.toHaveBeenCalled() 
    }) 
}) 
+0

从http复制://www.ngroutes.com/questions/AUuADiQ_a5vEqxqlK7FY/general-error-handler-for-restangular.html – 2015-08-27 13:53:55

4

而不是检查我们有承诺链中一个特定的错误处理或没有,我说我的一般错误处理程序要撤销的承诺链的更高版本:

Restangular.setErrorInterceptor(
     function(response, deferred, responseHandler) { 
      var generalHandlerTimer = $timeout(function(){ 
       generalErrorHanding(response); 
      },1); 
      response.cancelGeneralHandler = function(){ 
       $timeout.cancel(generalHandlerTimer); 
      }; 
      return true; // continue the promise chain 
     } 
); 

和呼叫,其中可能发生错误:

restangularizedUser.patch({ 
      user: { 
      email:newValue 
      } 
     }).then(function(res) { 
       //Success 
      }, function(response) { 
       //Error 
       //Cancel the general error handler due to I want to handle it on my way 
       response.cancelGeneralHandler(); 
       $log.debug('Handle the ERROR on my specific way'); 
     }); 

缺点:

  • 如果需要使用特定的错误处理程序,那么你需要调用response.cancelGeneralHandler();

优势:

  • 没有破解,需要:)