Skip to content

fix: fetch errors do not have responses #2564

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

Merged
merged 3 commits into from
May 27, 2020
Merged

fix: fetch errors do not have responses #2564

merged 3 commits into from
May 27, 2020

Conversation

rossipedia
Copy link
Contributor

Originally I was going to file an issue, but I figured it'd be more useful to just PR the fix

Package + Version

  • @sentry/browser

Version:

5.15.5

Description

fetch breadcrumbs attempts to access a non-existent response object.

fetch treats all responses as a success, regardless of the HTTP status code. When fetch throws an error, it won't have a response.

Details

To reproduce the error, you can use this small HTML file:

<!DOCTYPE html>
<script src="https://browser.sentry-cdn.com/5.15.5/bundle.min.js"></script>
<script>
Sentry.init({
    dsn: "YOUR_PUBLIC_DSN",
    debug: true,
});
fetch("https://some-domain-that.doesnt-exist.com");
</script>

Explanation

The handlers for fetch do not add a response object when an error occurs (correctly, as one does not exist in this case):

(error: Error) => {
triggerHandlers('fetch', {
...commonHandlerData,
endTimestamp: Date.now(),
error,
});
throw error;
},

However, in the code for adding the fetch breadcrumbs, there is an unconditional attempt to access the response.status property in the error case:

if (handlerData.error) {
getCurrentHub().addBreadcrumb(
{
category: 'fetch',
data: {
...handlerData.fetchData,
status_code: handlerData.response.status,
},
level: Severity.Error,

Which leads to the error shown:

image

@rossipedia rossipedia requested a review from kamilogorek as a code owner April 29, 2020 21:25
Copy link
Member

@HazAT HazAT left a comment

Choose a reason for hiding this comment

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

Thank you! Solid change and good description!
Sorry for the delay.

@HazAT HazAT merged commit 031b020 into getsentry:master May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants