Skip to content

Commit 0da4c47

Browse files
committed
feat: update error handling and console logs
1 parent 276aef9 commit 0da4c47

File tree

2 files changed

+134
-74
lines changed

2 files changed

+134
-74
lines changed

android/src/main/java/com/reactnativeone/OneModule.java

Lines changed: 121 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@
4040
import java.util.ArrayList;
4141
import java.util.EnumSet;
4242
import java.util.HashMap;
43+
import java.util.concurrent.CompletionException;
4344
import java.util.concurrent.ExecutionException;
4445
import java.util.concurrent.Executors;
4546
import java.util.concurrent.ExecutorService;
4647

4748
public class OneModule extends ReactContextBaseJavaModule {
4849
protected static final String NAME = "One";
4950
private final ExecutorService executor = Executors.newSingleThreadExecutor();
51+
private static final boolean THROW_ERRORS = true;
5052

5153
public OneModule(ReactApplicationContext reactContext) {
5254
super(reactContext);
@@ -86,30 +88,51 @@ public void sendInteraction(String interactionPath, ReadableMap propertiesMap, f
8688
.interactionPath(new OneInteractionPath(URI.create(interactionPath)))
8789
.properties(properties)
8890
.build();
91+
8992
executor.submit(() -> {
90-
try {
91-
OneResponse response;
92-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
93-
response = One.sendInteraction(true, sendInteractionRequest).join();
94-
} else {
95-
response = One.sendInteractionLegacySupport(true, sendInteractionRequest).join();
93+
OneResponse response;
94+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
95+
try {
96+
response = One.sendInteraction(THROW_ERRORS, sendInteractionRequest).join();
97+
WritableNativeMap responseMap = responseObjectToReadableMap(response);
98+
notifyResult(promise, responseMap);
99+
} catch (ExecutionException error) {
100+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
101+
Log.e(NAME, "[Thunderhead] Send Interaction Completion Error: " + error.getCause());
102+
} catch (OneSDKError error) {
103+
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
104+
Log.e(NAME, "[Thunderhead] Send Interaction SDK Error: " + error.getErrorMessage());
105+
} catch (OneAPIError error) {
106+
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
107+
Log.e(NAME, "[Thunderhead] Send Interaction Api Error: " + error.getErrorMessage());
108+
} catch (CompletionException error) {
109+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
110+
Log.e(NAME, "[Thunderhead] Send Interaction Completion Error: " + error.getLocalizedMessage());
111+
} catch (Exception error) {
112+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
113+
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
114+
}
115+
} else {
116+
try {
117+
response = One.sendInteractionLegacySupport(THROW_ERRORS, sendInteractionRequest).join();
118+
WritableNativeMap responseMap = responseObjectToReadableMap(response);
119+
notifyResult(promise, responseMap);
120+
} catch (ExecutionException error) {
121+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
122+
Log.e(NAME, "[Thunderhead] Send Interaction Completion Error: " + error.getCause());
123+
} catch (OneSDKError error) {
124+
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
125+
Log.e(NAME, "[Thunderhead] Send Interaction SDK Error: " + error.getErrorMessage());
126+
} catch (OneAPIError error) {
127+
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
128+
Log.e(NAME, "[Thunderhead] Send Interaction Api Error: " + error.getErrorMessage());
129+
} catch (Exception error) {
130+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
131+
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
96132
}
97-
One.processResponse(response);
98-
WritableNativeMap responseMap = responseObjectToReadableMap(response);
99-
notifyResult(promise, responseMap);
100-
} catch (ExecutionException error) {
101-
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
102-
Log.e(NAME, "[Thunderhead] Send Interaction Completion Error: " + error.getCause());
103-
} catch (OneSDKError error) {
104-
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
105-
Log.e(NAME, "[Thunderhead] Send Interaction SDK Error: " + error.getErrorMessage());
106-
} catch (OneAPIError error) {
107-
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
108-
Log.e(NAME, "[Thunderhead] Send Interaction Api Error: " + error.getErrorMessage());
109-
} catch (Exception error) {
110-
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
111-
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
112133
}
134+
WritableNativeMap responseMap = responseObjectToReadableMap(response);
135+
notifyResult(promise, responseMap);
113136
});
114137
}
115138

@@ -121,28 +144,47 @@ public void sendProperties(String interactionPath, ReadableMap propertiesMap, fi
121144
.properties(properties)
122145
.build();
123146
executor.submit(() -> {
124-
try {
125-
OneResponse response;
126-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
127-
response = One.sendProperties(true, sendPropertiesRequest).join();
128-
} else {
129-
response = One.sendPropertiesLegacySupport(true, sendPropertiesRequest).join();
147+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
148+
try {
149+
OneResponse response;
150+
response = One.sendProperties(THROW_ERRORS, sendPropertiesRequest).join();
151+
WritableNativeMap responseMap = responseObjectToReadableMap(response);
152+
notifyResult(promise, responseMap);
153+
} catch (ExecutionException error) {
154+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
155+
Log.e(NAME, "[Thunderhead] Send Properties Completion Error: " + error.getCause());
156+
} catch (OneSDKError error) {
157+
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
158+
Log.e(NAME, "[Thunderhead] Send Properties SDK Error: " + error.getErrorMessage());
159+
} catch (OneAPIError error) {
160+
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
161+
Log.e(NAME, "[Thunderhead] Send Properties Api Error: " + error.getErrorMessage());
162+
} catch (CompletionException error) {
163+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
164+
Log.e(NAME, "[Thunderhead] Send Properties Completion Error: " + error.getLocalizedMessage());
165+
} catch (Exception error) {
166+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
167+
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
168+
}
169+
} else {
170+
try {
171+
OneResponse response;
172+
response = One.sendPropertiesLegacySupport(THROW_ERRORS, sendPropertiesRequest).join();
173+
WritableNativeMap responseMap = responseObjectToReadableMap(response);
174+
notifyResult(promise, responseMap);
175+
} catch (ExecutionException error) {
176+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
177+
Log.e(NAME, "[Thunderhead] Send Properties Completion Error: " + error.getCause());
178+
} catch (OneSDKError error) {
179+
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
180+
Log.e(NAME, "[Thunderhead] Send Properties SDK Error: " + error.getErrorMessage());
181+
} catch (OneAPIError error) {
182+
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
183+
Log.e(NAME, "[Thunderhead] Send Properties Api Error: " + error.getErrorMessage());
184+
} catch (Exception error) {
185+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
186+
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
130187
}
131-
One.processResponse(response);
132-
WritableNativeMap responseMap = responseObjectToReadableMap(response);
133-
notifyResult(promise, responseMap);
134-
} catch (ExecutionException error) {
135-
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
136-
Log.e(NAME, "[Thunderhead] Send Properties Completion Error: " + error.getCause());
137-
} catch (OneSDKError error) {
138-
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
139-
Log.e(NAME, "[Thunderhead] Send Properties SDK Error: " + error.getErrorMessage());
140-
} catch (OneAPIError error) {
141-
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
142-
Log.e(NAME, "[Thunderhead] Send Properties Api Error: " + error.getErrorMessage());
143-
} catch (Exception error) {
144-
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
145-
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
146188
}
147189
});
148190
}
@@ -154,27 +196,45 @@ public void sendResponseCode(String interactionPath, String responseCode, final
154196
.responseCode(new OneResponseCode(responseCode))
155197
.interactionPath(new OneInteractionPath(URI.create(interactionPath)))
156198
.build();
157-
158-
try {
159-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
160-
One.sendResponseCode(true, responseCodeRequest).join();
161-
} else {
162-
One.sendResponseCodeLegacySupport(true, responseCodeRequest).join();
199+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
200+
try {
201+
One.sendResponseCode(THROW_ERRORS, responseCodeRequest).join();
202+
notifyResult(promise, null);
203+
} catch (ExecutionException error) {
204+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
205+
Log.e(NAME, "[Thunderhead] Send Response Code Completion Error: " + error.getCause());
206+
} catch (OneSDKError error) {
207+
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
208+
Log.e(NAME, "[Thunderhead] Send Response Code SDK Error: " + error.getErrorMessage());
209+
} catch (OneAPIError error) {
210+
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
211+
Log.e(NAME, "[Thunderhead] Send Response Code Api Error: " + error.getErrorMessage());
212+
} catch (CompletionException error) {
213+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
214+
Log.e(NAME, "[Thunderhead] Send Properties Completion Error: " + error.getLocalizedMessage());
215+
} catch (Exception error) {
216+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
217+
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
218+
}
219+
} else {
220+
try {
221+
One.sendResponseCodeLegacySupport(THROW_ERRORS, responseCodeRequest).join();
222+
notifyResult(promise, null);
223+
} catch (ExecutionException error) {
224+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
225+
Log.e(NAME, "[Thunderhead] Send Response Code Completion Error: " + error.getCause());
226+
} catch (OneSDKError error) {
227+
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
228+
Log.e(NAME, "[Thunderhead] Send Response Code SDK Error: " + error.getErrorMessage());
229+
} catch (OneAPIError error) {
230+
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
231+
Log.e(NAME, "[Thunderhead] Send Response Code Api Error: " + error.getErrorMessage());
232+
} catch (Exception error) {
233+
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
234+
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
163235
}
164-
notifyResult(promise, null);
165-
} catch (ExecutionException error) {
166-
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
167-
Log.e(NAME, "[Thunderhead] Send Response Code Completion Error: " + error.getCause());
168-
} catch (OneSDKError error) {
169-
notifyProblem(promise, Integer.toString(error.getSystemCode()), error.getLocalizedMessage());
170-
Log.e(NAME, "[Thunderhead] Send Response Code SDK Error: " + error.getErrorMessage());
171-
} catch (OneAPIError error) {
172-
notifyProblem(promise, Integer.toString(error.getHttpStatusCode()), error.getLocalizedMessage());
173-
Log.e(NAME, "[Thunderhead] Send Response Code Api Error: " + error.getErrorMessage());
174-
} catch (Exception error) {
175-
notifyProblem(promise, Integer.toString(error.hashCode()), error.getLocalizedMessage());
176-
Log.e(NAME, "[Thunderhead] Send Interaction Error: " + error.getLocalizedMessage());
177236
}
237+
178238
});
179239
}
180240

example/src/App.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,70 +37,70 @@ class ExampleProject extends Component {
3737
}
3838

3939
_onSendInteractionWithPropertiesButtonPress = () => {
40-
console.log('Sending interaction with Properties...');
40+
console.info('Sending interaction with Properties...');
4141
One.sendInteraction(interaction, properties).then(
4242
(response: any) => {
43-
console.log('response = ' + JSON.stringify(response));
43+
console.info('response = ' + JSON.stringify(response));
4444
Alert.alert('Success response =', JSON.stringify(response));
4545
},
4646
(error: any) => {
47-
console.log(error);
47+
console.error(error);
4848
Alert.alert('Error response = ', JSON.stringify(error));
4949
}
5050
);
5151
};
5252

5353
_onSendInteractionWithoutPropertiesButtonPress = () => {
54-
console.log('Sending interaction without Properties...');
54+
console.info('Sending interaction without Properties...');
5555
One.sendInteraction(interaction, null).then(
5656
(response: any) => {
5757
console.log('response = ' + JSON.stringify(response));
5858
Alert.alert('Success response =', JSON.stringify(response));
5959
},
6060
(error: any) => {
61-
console.log(error);
61+
console.error(error);
6262
Alert.alert('Error response =', JSON.stringify(error));
6363
}
6464
);
6565
};
6666

6767
_onSendPropertiesButtonPress = () => {
68-
console.log('Sent properties...');
68+
console.info('Sent properties...');
6969
One.sendProperties(interaction, properties).then(
7070
(response: any) => {
71-
console.log('response = ' + JSON.stringify(response));
71+
console.info('response = ' + JSON.stringify(response));
7272
Alert.alert('Success response =', JSON.stringify(response));
7373
},
7474
(error: any) => {
75-
console.log(error);
75+
console.error(error);
7676
Alert.alert('Error response =', JSON.stringify(error));
7777
}
7878
);
7979
};
8080

8181
_onSendResponseCodeButtonPress = () => {
82-
console.log('Sending response code...');
82+
console.info('Sending response code...');
8383
// This is just an example response code. A response code would
8484
// typically be returned in an optimization.
8585
var responseCode =
8686
'dGlkPThmZDhkZmIwLTIwNzAtNDk5ZC04NjczLWEyM2YxNDNiYjhlNSxhYz0yOTAzMjM5OTcsY250PTI5NzIyNDA0MCxvcD0xNjkxNTc5MzAscnQ9UE9TSVRJVkVfQ0xJQ0ssc2s9T05FLUFUN0JUU0ExSEotNzQyMg';
8787
One.sendResponseCode('/home', responseCode).then(
8888
(success: any) => {
89-
console.log('Send response code success');
89+
console.info('Send response code success');
9090
Alert.alert('Send response code success');
9191
},
9292
(error: any) => {
93-
console.log(error);
93+
console.error(error);
9494
Alert.alert('Error response =', JSON.stringify(error));
9595
}
9696
);
9797
};
9898

9999
_onGetTidButtonPress = () => {
100-
console.log('Getting tid...');
100+
console.info('Getting tid...');
101101
One.getTid().then((tid: String) => {
102102
Alert.alert('Tid = ', tid);
103-
console.log('tid = ' + tid);
103+
console.info('tid = ' + tid);
104104
});
105105
};
106106

0 commit comments

Comments
 (0)