Skip to content

Commit da84eba

Browse files
jcurtisfacebook-github-bot
authored andcommittedFeb 27, 2018
Catch exception and report it when making a network request with invalid 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
1 parent 07334cb commit da84eba

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
 

‎ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,13 @@ public void sendRequest(
264264
return;
265265
}
266266

267-
Request.Builder requestBuilder = new Request.Builder().url(url);
267+
Request.Builder requestBuilder;
268+
try {
269+
requestBuilder = new Request.Builder().url(url);
270+
} catch (Exception e) {
271+
ResponseUtil.onRequestError(eventEmitter, requestId, e.getMessage(), null);
272+
return;
273+
}
268274

269275
if (requestId != 0) {
270276
requestBuilder.tag(requestId);

‎ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java

+28
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,34 @@ public void testFailPostWithoutContentType() throws Exception {
168168
verifyErrorEmit(emitter, 0);
169169
}
170170

171+
@Test
172+
public void testFailInvalidUrl() throws Exception {
173+
RCTDeviceEventEmitter emitter = mock(RCTDeviceEventEmitter.class);
174+
ReactApplicationContext context = mock(ReactApplicationContext.class);
175+
when(context.getJSModule(any(Class.class))).thenReturn(emitter);
176+
177+
OkHttpClient httpClient = mock(OkHttpClient.class);
178+
OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class);
179+
when(clientBuilder.build()).thenReturn(httpClient);
180+
when(httpClient.newBuilder()).thenReturn(clientBuilder);
181+
NetworkingModule networkingModule = new NetworkingModule(context, "", httpClient);
182+
183+
mockEvents();
184+
185+
networkingModule.sendRequest(
186+
"GET",
187+
"aaa",
188+
/* requestId */ 0,
189+
/* headers */ JavaOnlyArray.of(),
190+
/* body */ null,
191+
/* responseType */ "text",
192+
/* useIncrementalUpdates*/ true,
193+
/* timeout */ 0,
194+
/* withCredentials */ false);
195+
196+
verifyErrorEmit(emitter, 0);
197+
}
198+
171199
private static void verifyErrorEmit(RCTDeviceEventEmitter emitter, int requestId) {
172200
ArgumentCaptor<WritableArray> captor = ArgumentCaptor.forClass(WritableArray.class);
173201
verify(emitter).emit(eq("didCompleteNetworkResponse"), captor.capture());

0 commit comments

Comments
 (0)
Please sign in to comment.