Skip to content

Commit 1d949a1

Browse files
Salakargrabbou
authored andcommitted
Allow 'userInfo' for native promise.reject + add native error stack support (#20940)
Summary: As mentioned [here](react-native-community/releases#34 (comment)), Android is missing native Promise reject with a `userInfo` `WritableMap` support and also `nativeStack` support (which addresses `TODO(8850038)`). This PR adds Android support for both of these. React Native on iOS ([here](https://github.com/facebook/react-native/blob/master/React/Base/RCTUtils.m#L433)) and Windows ([here](microsoft/react-native-windows#732)) already support this so this is a relatively minor addition to bring Android in line with the other platforms. (JS support is also [here](https://github.com/facebook/react-native/blob/master/Libraries/BatchedBridge/NativeModules.js#L145-L148)) Existing methods remain unchanged other than general cleanup of variable names (`e -> throwable`) and adding code comments/docs. Additionally, the `ShareTestModule` implementation of Promise (SimplePromise) was updated to reflect these changes - other line changes in this file are from formatting in Android Studio - if this is an issue let me know. - Currently inconsistent with other platforms. - Blocking a couple of issues over at [invertase/react-native-firebase](https://github.com/invertase/react-native-firebase) - save for re-writing everything to Promise resolve only - which is no small change and isn't a great solution either. Pull Request resolved: #20940 Differential Revision: D13412527 Pulled By: cpojer fbshipit-source-id: 2ca6c5f3db9ff2c2986b02edda80bc73432f66d3
1 parent 8302e0c commit 1d949a1

File tree

5 files changed

+382
-78
lines changed

5 files changed

+382
-78
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rn_android_library(
2222
proguard_config = "reactnative.pro",
2323
provided_deps = [
2424
react_native_dep("third-party/android/support/v4:lib-support-v4"),
25+
react_native_dep("third-party/android/support-annotations:android-support-annotations"),
2526
],
2627
required_for_source_only_abi = True,
2728
visibility = [

ReactAndroid/src/main/java/com/facebook/react/bridge/Promise.java

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,119 @@
77

88
package com.facebook.react.bridge;
99

10+
import javax.annotation.Nonnull;
1011
import javax.annotation.Nullable;
1112

12-
/**
13+
/*
1314
* Interface that represents a JavaScript Promise which can be passed to the native module as a
1415
* method parameter.
1516
*
16-
* Methods annotated with {@link ReactMethod} that use {@link Promise} as type of the last parameter
17+
* Methods annotated with {@link ReactMethod} that use a {@link Promise} as the last parameter
1718
* will be marked as "promise" and will return a promise when invoked from JavaScript.
1819
*/
1920
public interface Promise {
2021

2122
/**
22-
* Successfully resolve the Promise.
23+
* Successfully resolve the Promise with an optional value.
24+
*
25+
* @param value Object
2326
*/
2427
void resolve(@Nullable Object value);
2528

2629
/**
27-
* Report an error which wasn't caused by an exception.
30+
* Report an error without an exception using a custom code and error message.
31+
*
32+
* @param code String
33+
* @param message String
2834
*/
2935
void reject(String code, String message);
3036

3137
/**
32-
* Report an exception.
38+
* Report an exception with a custom code.
39+
*
40+
* @param code String
41+
* @param throwable Throwable
3342
*/
34-
void reject(String code, Throwable e);
43+
void reject(String code, Throwable throwable);
3544

3645
/**
37-
* Report an exception with a custom error message.
46+
* Report an exception with a custom code and error message.
47+
*
48+
* @param code String
49+
* @param message String
50+
* @param throwable Throwable
3851
*/
39-
void reject(String code, String message, Throwable e);
52+
void reject(String code, String message, Throwable throwable);
53+
4054

4155
/**
42-
* Report an error which wasn't caused by an exception.
43-
* @deprecated Prefer passing a module-specific error code to JS.
44-
* Using this method will pass the error code "EUNSPECIFIED".
56+
* Report an exception, with default error code.
57+
* Useful in catch-all scenarios where it's unclear why the error occurred.
58+
*
59+
* @param throwable Throwable
4560
*/
46-
@Deprecated
47-
void reject(String message);
61+
void reject(Throwable throwable);
62+
63+
/* ---------------------------
64+
* With userInfo WritableMap
65+
* --------------------------- */
4866

4967
/**
50-
* Report an exception, with default error code.
68+
* Report an exception, with default error code, with userInfo.
5169
* Useful in catch-all scenarios where it's unclear why the error occurred.
70+
*
71+
* @param throwable Throwable
72+
* @param userInfo WritableMap
73+
*/
74+
void reject(Throwable throwable, WritableMap userInfo);
75+
76+
/**
77+
* Reject with a code and userInfo WritableMap.
78+
*
79+
* @param code String
80+
* @param userInfo WritableMap
5281
*/
53-
void reject(Throwable reason);
82+
void reject(String code, @Nonnull WritableMap userInfo);
83+
84+
/**
85+
* Report an exception with a custom code and userInfo.
86+
*
87+
* @param code String
88+
* @param throwable Throwable
89+
* @param userInfo WritableMap
90+
*/
91+
void reject(String code, Throwable throwable, WritableMap userInfo);
92+
93+
/**
94+
* Report an error with a custom code, error message and userInfo,
95+
* an error not caused by an exception.
96+
*
97+
* @param code String
98+
* @param message String
99+
* @param userInfo WritableMap
100+
*/
101+
void reject(String code, String message, @Nonnull WritableMap userInfo);
102+
103+
/**
104+
* Report an exception with a custom code, error message and userInfo.
105+
*
106+
* @param code String
107+
* @param message String
108+
* @param throwable Throwable
109+
* @param userInfo WritableMap
110+
*/
111+
void reject(String code, String message, Throwable throwable, WritableMap userInfo);
112+
113+
/* ------------
114+
* Deprecated
115+
* ------------ */
116+
117+
/**
118+
* Report an error which wasn't caused by an exception.
119+
*
120+
* @deprecated Prefer passing a module-specific error code to JS.
121+
* Using this method will pass the error code "EUNSPECIFIED".
122+
*/
123+
@Deprecated
124+
void reject(String message);
54125
}

0 commit comments

Comments
 (0)