1

I am using a service that will send back XML with data in response. As a rule, everything works fine but sometimes the server incorrectly sends empty XML, without data.

var isokLayer = $http({
        method: 'GET',
        url: URLs.GEOSERVER_ISOK
    }).then(function(response) {
        if (response.status == 200) {
            do {
                var formatter = new ol.format.WMTSCapabilities();
            } while (response.data == null);

            var datas = (formatter.read(response.data));

            var options = ol.source.WMTS.optionsFromCapabilities(datas, {
                layer: 'CIEN',
                matrixSet: 'EPSG:2180',
            });
            var source = new ol.source.WMTS(options);

            for (var z = 0; z < source.getTileGrid().getMatrixIds().length; ++z) {
                source.getTileGrid().getOrigin(z).reverse(); 
            }

            var result = new ol.layer.Tile({
                source: source,
                visible: false,
                xarisLayerType: 'baseLayer',
                xarisLayerName: 'NMT LPIS',
                xarisLayerSampleIcon: 'assets/styles/img/baseLayerSample/nmt.png',
                zIndex: ConfigService.getBaseLayerZIndex()
            });
            map.addLayer(result);
            layerList.push(result);
        } else {
            console.log(response.status);
        }
    }, function(err) {
        console.log(err);
    });

How i can repeat http call inside my successCallback if response.data are null? I tried repeat this in errorCallback but response.status always is 200 and the error function is never execute.

0

1 Answer 1

1

How about this code?

$q.reject method is allow you to reject current promise. If promise is rejected, the callback of the 'then' method is stopped and a catch callback is executed.

var isokLayer = $http({
   // ...
}).then(function(response) {
    if (response.status == 200 && response.data) {
        // ...
    } else if (response.data == null) {
        return $q.reject(response);
    } else {
       console.log(response.status)
    }
}).catch(function(errResponse) {
    console.log(errResponse);
});
Sign up to request clarification or add additional context in comments.

4 Comments

i think you are missing a "return" before "$q.reject"
Thanks @AhmadAboSaa, I fixed that.
I try use reject, but after "return $q.reject(response)" function doesn't go into errorCallback.
Sorry, i fixed again. Use catch method instead of error callback in then method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.