我有类似的问题。 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()
})
})
从http复制://www.ngroutes.com/questions/AUuADiQ_a5vEqxqlK7FY/general-error-handler-for-restangular.html – 2015-08-27 13:53:55