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

cannot catch error from fetch call when used a malformed URL #18087

Closed
Trenrod opened this issue Feb 25, 2018 · 9 comments
Closed

cannot catch error from fetch call when used a malformed URL #18087

Trenrod opened this issue Feb 25, 2018 · 9 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@Trenrod
Copy link

Trenrod commented Feb 25, 2018

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Environment:
OS: Windows 10
Node: 6.12.3
Yarn: Not Found
npm: 3.10.10
Watchman: Not Found
Xcode: N/A
Android Studio: Version 3.0.0.0 AI-171.4443003

Packages: (wanted => installed)
react: 16.2.0 => 16.2.0
react-native: 0.53.3 => 0.53.3

Target Platform: Android 6 Emulator (From the Getting Started howto)

Steps to Reproduce

Cannot catch the fetch call when used a malformed url.

  1. Call fetch() with a malformed url like 'asd' or 'https:://facebook.github.io'
  2. try to catch with '.catch' when called as promise or try/catch within async/await

Expected Behavior

catch should be executed

Actual Behavior

red screen shows up with "unexpected url: [URL USED WITHIN THE CALL]"

grafik

Reproducible Demo

  async asyncFetch(url) {
    try {
      console.log('DEBUG - START FETCHING');
      const response = await fetch(url);
      console.log('DEBUG - END FETCHING');
      const xml = await response.text();
      const json = xml2json.parse(xml);
      return { data: json };
    } catch (error) {
      console.log('DEBUG - START ERROR: ' + error.message);
      throw error;
    }
  }

  const url = 'httasd:afdasdfads';
  this.asyncFetch(url)
  .catch(err => {
    console.log('DEBUG - Error: ' + err.message);
  })

Actual console output is:
DEBUG - START FETCHING

Note: Same issue like #17843

@ksegla
Copy link

ksegla commented Feb 26, 2018

fetch has been having this very bad problem for some time now, crashing apps in production whenever something it doesn't like happen. At this point, I'm looking for good alternatives :-(

@Dorentin1997
Copy link

Check if url is good and returns something. Send an request at that link with postman check what it returns.

@Trenrod
Copy link
Author

Trenrod commented Feb 26, 2018

@Dorentin1997 thank you for your comment.
Yes, a workaround can be using 'url-parse' to validate the url.
But my assumption is that if someone enters an invalid url, fetch should throw a handleble exception and not crashing the whole application.

@Dorentin1997
Copy link

Use url-parse do the logic there, if it`s wrong throw the exception there.

@Trenrod
Copy link
Author

Trenrod commented Feb 26, 2018

@Dorentin1997 again. Yes as a workaround.
Still this bug exists in the fetch implementation, which should be fixed.

@Dorentin1997
Copy link

@Trenrod Yes it`s clearly a bug, i had problems too with this. Hopefully it will be fixed.

jcurtis added a commit to jcurtis/react-native that referenced this issue Feb 26, 2018
…lid URL on Android

Currently if you invoke `fetch()` with an invalid URL ("aaa" for
example) you cannot catch the error in javascript since it's not
reported. Instead the entire app crashes.

Fixes facebook#7436 and facebook#18087

Hopefully.
facebook-github-bot pushed a commit that referenced this issue Feb 27, 2018
…lid URL on Android

Summary:
Currently if you invoke `fetch()` with an invalid URL ("aaa" for
example) you cannot catch the error in javascript since it's not
reported. Instead the entire app crashes.

Fixes #7436 and #18087

Hopefully.

<!--
Thank you for sending the PR! We appreciate you spending the time to work on these changes.

Help us understand your motivation by explaining why you decided to make this change.

You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html

Happy contributing!

-->

Fix using fetch on Android with user generated input.

Added relevant unit test

`./scripts/run-android-local-unit-tests.sh` all pass

[ANDROID] [BUGFIX] [fetch] - Allow "unexpected url" exception to be caught on Android when using fetch
<!--
Help reviewers and the release process by writing your own release notes

**INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.**

  CATEGORY
[----------]        TYPE
[ CLI      ]   [-------------]      LOCATION
[ DOCS     ]   [ BREAKING    ]   [-------------]
[ GENERAL  ]   [ BUGFIX      ]   [-{Component}-]
[ INTERNAL ]   [ ENHANCEMENT ]   [ {File}      ]
[ IOS      ]   [ FEATURE     ]   [ {Directory} ]   |-----------|
[ ANDROID  ]   [ MINOR       ]   [ {Framework} ] - | {Message} |
[----------]   [-------------]   [-------------]   |-----------|

[CATEGORY] [TYPE] [LOCATION] - MESSAGE

 EXAMPLES:

 [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things
 [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput
 [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with
 [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word
 [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position
 [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see
-->
Closes #18103

Differential Revision: D7097110

Pulled By: hramos

fbshipit-source-id: 69144e8a0f7404d9bcc7c71a94650de36a48c84a
@react-native-bot react-native-bot added the Platform: Windows Building on Windows. label Mar 20, 2018
@hramos hramos removed the Platform: Windows Building on Windows. label Mar 29, 2018
@stale
Copy link

stale bot commented Jun 27, 2018

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

@stale stale bot added Stale There has been a lack of activity on this issue and it may be closed soon. and removed Stale There has been a lack of activity on this issue and it may be closed soon. labels Jun 27, 2018
@stale
Copy link

stale bot commented Sep 25, 2018

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as "For Discussion" or "Good first issue" and I will leave it open. Thank you for your contributions.

@stale stale bot added Stale There has been a lack of activity on this issue and it may be closed soon. and removed Stale There has been a lack of activity on this issue and it may be closed soon. labels Sep 25, 2018
@kelset
Copy link
Contributor

kelset commented Dec 17, 2018

This issue has been inactive for a very long time, closing. There are newer and more active issues on this subject.

@kelset kelset closed this as completed Dec 17, 2018
@facebook facebook locked as resolved and limited conversation to collaborators Dec 17, 2019
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Dec 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

6 participants