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

Add CORS error detection and convert the TypeError to AJAXError #4822

Closed
wants to merge 10 commits into from

Conversation

jonathanlurie
Copy link
Contributor

@jonathanlurie jonathanlurie commented Oct 11, 2024

Launch Checklist

  • Confirm your changes do not include backports from Mapbox projects (unless with a compliant license) - if you are not sure about this, please ask!
  • Briefly describe the changes in this PR.
  • Link to related issues.
  • Include before/after visuals or gifs if this PR includes visual changes.
  • Write tests for all new functionality. Read bellow
  • Document any changes to public APIs.
  • Post benchmark scores.
  • Add an entry to CHANGELOG.md under the ## main section.

Description

This PR addresses how failed HTTP requests due to CORS policy are handled. So far, if a request fails because of CORS, it does not return a response, instead, it makes the browser throw a generic TypeError ( see here ). As a result, a map instance subscribing to the "error" event would catch a generic error that does not include the faulty URL. It is better to handle a CORS error with an AJAXError, that contains information about the URL and the cause of the actual problem.

This is especially useful in the context of loading a style.json so that if a given style is not reachable because of CORS, it is possible to catch this error with the same efficiency as if fetching a style.json would yield an HTTP 5xx status code, since in both cases the request would fails due to server configuration.

Before / After

With this minimalist snippet:

const map = new maplibregl.Map({
  container: 'map',
  style: "https://example.com/style.json",
});

map.on("error", (err) => {
  console.log(">>> ", err);
}) 

Here is the console output,

  • BEFORE:
Screenshot 2024-10-11 at 09 59 01
  • AFTER:
Screenshot 2024-10-11 at 10 03 41

Test

I tried adding another test to src/util/ajax.test.ts but it is impossible to simulate a CORS with the Nise package's FakeServer, since a CORS error is not part of the server response but is due to the browser blocking it.

Other info

  • This makes no change to public API.
  • this does not affect performance

@codecov-commenter
Copy link

codecov-commenter commented Oct 11, 2024

Codecov Report

Attention: Patch coverage is 25.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 90.69%. Comparing base (9ba25c6) to head (3fc4ca9).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/util/ajax.ts 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4822      +/-   ##
==========================================
- Coverage   90.86%   90.69%   -0.17%     
==========================================
  Files         265      265              
  Lines       38098    38103       +5     
  Branches     3163     3166       +3     
==========================================
- Hits        34616    34559      -57     
- Misses       2538     2610      +72     
+ Partials      944      934      -10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@HarelM
Copy link
Collaborator

HarelM commented Oct 11, 2024

Thanks for taking the time to report this issue!
Can this be tested using integration tests in browser or unit tests?

src/util/ajax.ts Outdated Show resolved Hide resolved
@jonathanlurie
Copy link
Contributor Author

Thanks for taking the time to report this issue! Can this be tested using integration tests in browser or unit tests?

This is quite challenging to test since the fake server (nise) cannot simulate CORS issue or DNS resolution failures. I haven't find a way to test that yet.

src/util/ajax.ts Outdated
try {
response = await fetch(request);
} catch (e) {
if (isNetworkError(e)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we care if this is a network error then? If the missing information is that we want to get the url for the relevant fetch behavior, why not simply wrap any exception here with AJAXError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's a good point, we could do that. I am not aware of all the possible scenarios in which fetch would throw, but if it does, that's fair to call it an AJAXError regardless of the reason and to make the faulty URL part of it.

@HarelM
Copy link
Collaborator

HarelM commented Oct 29, 2024

Yes, all the tests set fetch to null, so this can't be really tested.

HarelM and others added 3 commits October 29, 2024 14:14
* Fix map-pitch-aligned line-placed texts on globe

* Add render test

* Only use globe text line render tests with collision circles

* Add another symbol-visibility/visible expected image

* Add changelog entry
@jonathanlurie
Copy link
Contributor Author

For some weird reason, this PR is now coming with other things from the main branch. Closing it and reopening it clean.

@jonathanlurie jonathanlurie mentioned this pull request Oct 30, 2024
8 tasks
@jonathanlurie
Copy link
Contributor Author

Clean version here #4931 , sorry for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants