角如何处理所要求无法使用的网址$ http.get或$ http.jsonp,这是由$ q.all执行() [英] Angular how to deal with unavailable URLs requested by $http.get or $http.jsonp, which are executed by $q.all()

查看:138
本文介绍了角如何处理所要求无法使用的网址$ http.get或$ http.jsonp,这是由$ q.all执行()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code:

eventResourcesCall = $http.jsonp('https://apicall/to/serverA');
eventsDetailsCall = $http.get('https://apicall/to/serverB');

$q.all([eventResourcesCall, eventsDetailsCall]).then(function(values){
    //process data manipulation and merging
});

的问题是,服务器A和服务器B可能不会提供有时,当其中的一个是不可用的,该数据处理code停止,我得到类似于下面描述的一个错误:

The problem is that serverA and ServerB might not be available sometimes, and when one of those are unavailable, the data processing code stops and I get an error similar to the one described below:

GET https://apicall/to/serverA?jsonp=angular.callbacks._0 404 (Not Found)

任何一个都可以点我到一个文档或描述在回答如何妥善处理由$ HTTP请求和$ q.all执行不可用URL()?

Can any one point me to a documentation or describe on the answer how to properly deal with unavailable URL requested by $http and executed by $q.all()?

我想能够做的是让一个指示该URL无法访问,然后避免了数据处理code流产。

What I would like to be able to do is to get an indication that the URL is not accessible and then avoid the data processing code abortion.

谢谢!

推荐答案

我会用间接承​​诺:

var d1 = $q.defer(), d2 = $q.defer();

function NetworkError(reason) { this.reason = reason; }

eventResourcesCall = $http.jsonp('https://apicall/to/serverA').then(
    function(response) {
        d1.resolve(response);
    },
    function(err) {
        d1.resolve(new NetworkError(err));
    }
);
eventsDetailsCall = $http.get('https://apicall/to/serverB').then(
    function(response) {
        d2.resolve(response);
    },
    function(err) {
        d2.resolve(new NetworkError(err));
    }
);

$q.all([d1, d2]).then(function(values){
    var eventResources = values[0], eventsDetails = values[1];

    if( eventResources instanceof NetworkError ) {
        // handle error
    }
    else {
        // eventResources is good, use it
    }

    // and so on...
});

和因此间接的应许八方通解决了所有()成功。但分辨率值可能是这标志着实际的错误在该请求的特殊 NetworkError 类。

So the indirect promises are allways resolved and the all() succeeds. But the resolution value may be of the special NetworkError class which signals the actual error in this request.

这绝对是庞大的,但也与一些实用方法来改善,例如:

This is definitely bulky, but could be improved with some utility methods, e.g.:

function makeIndirectPromise(httpPromise) {
    var ret = $q.defer();
    httpPromise.then(
        function(response) {
            ret.resolve(response);
        },
        function(err) {
            ret.resolve(new NetworkError(err));
        }
    );
    return ret.promise;
}

而code上述变化:

And the code above changes to:

function NetworkError(reason) { this.reason = reason; }

function makeIndirectPromise(httpPromise) { /* see above */ }

eventResourcesCall = makeIndirectPromise($http.jsonp('https://apicall/to/serverA'));
eventsDetailsCall = makeIndirectPromise($http.get('https://apicall/to/serverB'));

$q.all([eventResourcesCall, eventsDetailsCall]).then(function(values){
    var eventResources = values[0], eventsDetails = values[1];

    if( eventResources instanceof NetworkError ) {
        // handle error
    }
    else {
        // eventResources is good, use it
    }

    // and so on...
});

这篇关于角如何处理所要求无法使用的网址$ http.get或$ http.jsonp,这是由$ q.all执行()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆