Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide helpful message if access token triggers 401: Unauthorized #6283

Merged
merged 2 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about also testing the URL is on the mapbox.com domain? So long as we promise compatibility with third party vector tile sources we must be careful with logic that assumes we're talking to Mapbox servers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea!

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();
Expand Down
12 changes: 12 additions & 0 deletions test/unit/util/ajax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});