-
-
Notifications
You must be signed in to change notification settings - Fork 754
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
Conversation
Codecov ReportAttention: Patch coverage is
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. |
Thanks for taking the time to report this issue! |
This is quite challenging to test since the fake server ( |
src/util/ajax.ts
Outdated
try { | ||
response = await fetch(request); | ||
} catch (e) { | ||
if (isNetworkError(e)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Yes, all the tests set |
* 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
For some weird reason, this PR is now coming with other things from the main branch. Closing it and reopening it clean. |
Clean version here #4931 , sorry for that! |
Launch Checklist
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:
Here is the console output,
Test
I tried adding another test to
src/util/ajax.test.ts
but it is impossible to simulate a CORS with theNise
package'sFakeServer
, since a CORS error is not part of the server response but is due to the browser blocking it.Other info