Skip to content

Commit

Permalink
Prevent 404 HTTP code from throwing an exception (#4446)
Browse files Browse the repository at this point in the history
* Prevent 404 HTTP statuses from triggering an exception

* Revert debug/index.html

* Revert src/source/vector_tile_source.js
  • Loading branch information
lucaswoj authored Mar 20, 2017
1 parent 6945214 commit 9e3397a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/source/source_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class SourceCache extends Evented {
_tileLoaded(tile, id, previousState, err) {
if (err) {
tile.state = 'errored';
this._source.fire('error', {tile: tile, error: err});
if (err.status !== 404) this._source.fire('error', {tile: tile, error: err});
return;
}

Expand Down
11 changes: 9 additions & 2 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

const window = require('./window');

class AJAXError extends Error {
constructor(message, status) {
super(message);
this.status = status;
}
}

exports.getJSON = function(url, callback) {
const xhr = new window.XMLHttpRequest();
xhr.open('GET', url, true);
Expand All @@ -19,7 +26,7 @@ exports.getJSON = function(url, callback) {
}
callback(null, data);
} else {
callback(new Error(xhr.statusText));
callback(new AJAXError(xhr.statusText, xhr.status));
}
};
xhr.send();
Expand All @@ -44,7 +51,7 @@ exports.getArrayBuffer = function(url, callback) {
expires: xhr.getResponseHeader('Expires')
});
} else {
callback(new Error(xhr.statusText));
callback(new AJAXError(xhr.statusText, xhr.status));
}
};
xhr.send();
Expand Down
7 changes: 7 additions & 0 deletions test/unit/source/source_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ test('SourceCache / Source lifecycle', (t) => {
sourceCache.onAdd();
});

t.test('suppress 404 errors', (t) => {
const sourceCache = createSourceCache({status: 404, message: 'Not found'})
.on('error', t.fail);
sourceCache.onAdd();
t.end();
});

t.test('loaded() true after source error', (t) => {
const sourceCache = createSourceCache({ error: 'Error loading source' })
.on('error', () => {
Expand Down
52 changes: 49 additions & 3 deletions test/unit/util/ajax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,63 @@ test('ajax', (t) => {
window.restore();
callback();
});
t.test('getArrayBuffer', (t) => {
const url = '/buffer-request';

t.test('getArrayBuffer, no content error', (t) => {
window.server.respondWith(request => {
request.respond(200, {'Content-Type': 'image/png'}, '');
});
ajax.getArrayBuffer(url, (error) => {
ajax.getArrayBuffer('', (error) => {
t.pass('called getArrayBuffer');
t.ok(error, 'should error when the status is 200 without content.');
t.end();
});
window.server.respond();
});

t.test('getArrayBuffer, 404', (t) => {
window.server.respondWith(request => {
request.respond(404);
});
ajax.getArrayBuffer('', (error) => {
t.equal(error.status, 404);
t.end();
});
window.server.respond();
});

t.test('getJSON', (t) => {
window.server.respondWith(request => {
request.respond(200, {'Content-Type': 'application/json'}, '{"foo": "bar"}');
});
ajax.getJSON('', (error, body) => {
t.error(error);
t.deepEqual(body, {foo: 'bar'});
t.end();
});
window.server.respond();
});

t.test('getJSON, invalid syntax', (t) => {
window.server.respondWith(request => {
request.respond(200, {'Content-Type': 'application/json'}, 'how do i even');
});
ajax.getJSON('', (error) => {
t.ok(error);
t.end();
});
window.server.respond();
});

t.test('getJSON, 404', (t) => {
window.server.respondWith(request => {
request.respond(404);
});
ajax.getJSON('', (error) => {
t.equal(error.status, 404);
t.end();
});
window.server.respond();
});

t.end();
});

0 comments on commit 9e3397a

Please sign in to comment.