From b410a2ecf7d46c1a7c16fbc1c2dc79bdc0d79641 Mon Sep 17 00:00:00 2001 From: Molly Lloyd Date: Mon, 5 Mar 2018 12:40:58 -0800 Subject: [PATCH 1/2] provide helpful message if access token triggers 401: Unauthorized --- src/util/ajax.js | 6 +++++- test/unit/util/ajax.test.js | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/util/ajax.js b/src/util/ajax.js index f39cb83dc40..c999525cc2f 100644 --- a/src/util/ajax.js +++ b/src/util/ajax.js @@ -85,7 +85,11 @@ exports.getJSON = function(requestParameters: RequestParameters, callback: Callb } callback(null, data); } else { - callback(new AJAXError(xhr.statusText, xhr.status, requestParameters.url)); + if (xhr.status === 401) { + callback(new AJAXError(`${xhr.statusText}: you may have provided an invalid Mapbox access token. See https://www.mapbox.com/api-documentation/#access-tokens`, xhr.status, requestParameters.url)); + } else { + callback(new AJAXError(xhr.statusText, xhr.status, requestParameters.url)); + } } }; xhr.send(); diff --git a/test/unit/util/ajax.test.js b/test/unit/util/ajax.test.js index 1a322a329fa..1ea9a5b6b4e 100644 --- a/test/unit/util/ajax.test.js +++ b/test/unit/util/ajax.test.js @@ -72,5 +72,17 @@ test('ajax', (t) => { window.server.respond(); }); + t.test('getJSON, 401', (t) => { + window.server.respondWith(request => { + request.respond(401); + }); + ajax.getJSON({ url:'' }, (error) => { + t.equal(error.status, 401); + t.equal(error.message, "Unauthorized: you may have provided an invalid Mapbox access token. See https://www.mapbox.com/api-documentation/#access-tokens"); + t.end(); + }); + window.server.respond(); + }); + t.end(); }); From 2a6aa37c3c274012fc952af20b1e6ba2833e2469 Mon Sep 17 00:00:00 2001 From: Molly Lloyd Date: Mon, 5 Mar 2018 13:06:07 -0800 Subject: [PATCH 2/2] make message apply only to mapbox.com requests --- src/util/ajax.js | 2 +- test/unit/util/ajax.test.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/util/ajax.js b/src/util/ajax.js index c999525cc2f..eb52d025e00 100644 --- a/src/util/ajax.js +++ b/src/util/ajax.js @@ -85,7 +85,7 @@ exports.getJSON = function(requestParameters: RequestParameters, callback: Callb } callback(null, data); } else { - if (xhr.status === 401) { + if (xhr.status === 401 && requestParameters.url.match(/mapbox.com/)) { callback(new AJAXError(`${xhr.statusText}: you may have provided an invalid Mapbox access token. See https://www.mapbox.com/api-documentation/#access-tokens`, xhr.status, requestParameters.url)); } else { callback(new AJAXError(xhr.statusText, xhr.status, requestParameters.url)); diff --git a/test/unit/util/ajax.test.js b/test/unit/util/ajax.test.js index 1ea9a5b6b4e..3e649a210f1 100644 --- a/test/unit/util/ajax.test.js +++ b/test/unit/util/ajax.test.js @@ -72,11 +72,23 @@ test('ajax', (t) => { window.server.respond(); }); - t.test('getJSON, 401', (t) => { + t.test('getJSON, 401: non-Mapbox domain', (t) => { window.server.respondWith(request => { request.respond(401); }); ajax.getJSON({ url:'' }, (error) => { + t.equal(error.status, 401); + t.equal(error.message, "Unauthorized"); + t.end(); + }); + window.server.respond(); + }); + + t.test('getJSON, 401: Mapbox domain', (t) => { + window.server.respondWith(request => { + request.respond(401); + }); + ajax.getJSON({ url:'api.mapbox.com' }, (error) => { t.equal(error.status, 401); t.equal(error.message, "Unauthorized: you may have provided an invalid Mapbox access token. See https://www.mapbox.com/api-documentation/#access-tokens"); t.end();