Merge pull request #4963 from tschaub/xhr-failures
Handle more XHR errors in the TileJSON source
This commit is contained in:
@@ -44,23 +44,47 @@ ol.source.TileJSON = function(options) {
|
|||||||
ol.net.jsonp(options.url, this.handleTileJSONResponse.bind(this),
|
ol.net.jsonp(options.url, this.handleTileJSONResponse.bind(this),
|
||||||
this.handleTileJSONError.bind(this));
|
this.handleTileJSONError.bind(this));
|
||||||
} else {
|
} else {
|
||||||
var xhr = new XMLHttpRequest();
|
var client = new XMLHttpRequest();
|
||||||
xhr.open('GET', options.url, true);
|
client.addEventListener('load', this.onXHRLoad_.bind(this));
|
||||||
xhr.onload = function(e) {
|
client.addEventListener('error', this.onXHRError_.bind(this));
|
||||||
if (xhr.status >= 200 && xhr.status < 300) {
|
client.open('GET', options.url);
|
||||||
var response = /** @type {TileJSON} */(JSON.parse(xhr.responseText));
|
client.send();
|
||||||
this.handleTileJSONResponse(response);
|
|
||||||
} else {
|
|
||||||
this.handleTileJSONError();
|
|
||||||
}
|
|
||||||
}.bind(this);
|
|
||||||
xhr.send();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
goog.inherits(ol.source.TileJSON, ol.source.TileImage);
|
goog.inherits(ol.source.TileJSON, ol.source.TileImage);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @param {Event} event The load event.
|
||||||
|
*/
|
||||||
|
ol.source.TileJSON.prototype.onXHRLoad_ = function(event) {
|
||||||
|
var client = /** @type {XMLHttpRequest} */ (event.target);
|
||||||
|
if (client.status >= 200 && client.status < 300) {
|
||||||
|
var response;
|
||||||
|
try {
|
||||||
|
response = /** @type {TileJSON} */(JSON.parse(client.responseText));
|
||||||
|
} catch (err) {
|
||||||
|
this.handleTileJSONError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.handleTileJSONResponse(response);
|
||||||
|
} else {
|
||||||
|
this.handleTileJSONError();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @param {Event} event The error event.
|
||||||
|
*/
|
||||||
|
ol.source.TileJSON.prototype.onXHRError_ = function(event) {
|
||||||
|
this.handleTileJSONError();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @protected
|
* @protected
|
||||||
* @param {TileJSON} tileJSON Tile JSON.
|
* @param {TileJSON} tileJSON Tile JSON.
|
||||||
|
|||||||
@@ -4,14 +4,32 @@ goog.provide('ol.test.source.TileJSON');
|
|||||||
describe('ol.source.TileJSON', function() {
|
describe('ol.source.TileJSON', function() {
|
||||||
|
|
||||||
describe('#getState', function() {
|
describe('#getState', function() {
|
||||||
|
|
||||||
it('returns ol.source.State.ERROR on HTTP 404', function() {
|
it('returns ol.source.State.ERROR on HTTP 404', function() {
|
||||||
var changeSpy = sinon.spy(function(event) {
|
|
||||||
expect(event.target.getState()).to.eql('error');
|
|
||||||
});
|
|
||||||
var source = new ol.source.TileJSON({
|
var source = new ol.source.TileJSON({
|
||||||
url: 'invalid.jsonp'
|
url: 'invalid.jsonp'
|
||||||
});
|
});
|
||||||
ol.events.listen(source, 'change', changeSpy);
|
source.on('change', function() {
|
||||||
|
expect(source.getState()).to.eql('error');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns ol.source.State.ERROR on CORS issues', function() {
|
||||||
|
var source = new ol.source.TileJSON({
|
||||||
|
url: 'http://example.com'
|
||||||
|
});
|
||||||
|
source.on('change', function() {
|
||||||
|
expect(source.getState()).to.eql('error');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns ol.source.State.ERROR on JSON parsing issues', function() {
|
||||||
|
var source = new ol.source.TileJSON({
|
||||||
|
url: '/'
|
||||||
|
});
|
||||||
|
source.on('change', function() {
|
||||||
|
expect(source.getState()).to.eql('error');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user