Skip to content

Commit 9baa0a1

Browse files
committed
profile: return promise from trackEvent to get errors in js side
1 parent 71e0bcb commit 9baa0a1

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

android/src/main/java/com/batch/batch_rn/RNBatchModule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,17 @@ public void profile_identify(String identifier) {
733733
}
734734

735735
@ReactMethod
736-
public void profile_trackEvent(String name, ReadableMap serializedEventData) {
736+
public void profile_trackEvent(@NonNull String name, @Nullable ReadableMap serializedEventData, @NonNull Promise promise) {
737737
BatchEventAttributes attributes = RNUtils.convertSerializedEventDataToEventAttributes(serializedEventData);
738+
if (attributes != null) {
739+
List<String> errors = attributes.validateEventAttributes();
740+
if (!errors.isEmpty()) {
741+
promise.reject(BATCH_BRIDGE_ERROR_CODE, errors.toString());
742+
return;
743+
}
744+
}
738745
Batch.Profile.trackEvent(name, attributes);
746+
promise.resolve(null);
739747
}
740748

741749
@ReactMethod

ios/RNBatch.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ -(void)stopObserving {
406406
[BatchProfile identify:identifier];
407407
}
408408

409-
RCT_EXPORT_METHOD(profile_trackEvent:(NSString*)name data:(NSDictionary*)serializedEventData)
409+
RCT_EXPORT_METHOD(profile_trackEvent:(NSString*)name data:(NSDictionary*)serializedEventData resolver: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
410410
{
411411
BatchEventAttributes *batchEventAttributes = nil;
412412

@@ -418,12 +418,14 @@ -(void)stopObserving {
418418
[batchEventAttributes validateWithError:&err];
419419
if (batchEventAttributes != nil && err == nil) {
420420
[BatchProfile trackEventWithName:name attributes:batchEventAttributes];
421+
resolve([NSNull null]);
421422
} else {
422-
NSLog(@"Event validation error: %@", err.description);
423+
reject(@"BatchBridgeError", @"Event attributes validation failed:", err);
423424
return;
424425
}
425426
}
426427
[BatchProfile trackEventWithName:name attributes:batchEventAttributes];
428+
resolve([NSNull null]);
427429
}
428430

429431
- (BatchEventAttributes*) convertSerializedEventDataToEventAttributes:(NSDictionary *) serializedAttributes {

src/BatchProfile.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ export const BatchProfile = {
5656
* @param name The event name. Must be a string.
5757
* @param data The event attributes (optional). Must be an object.
5858
*/
59-
trackEvent: (name: string, data?: BatchEventAttributes): void => {
59+
trackEvent: (name: string, data?: BatchEventAttributes): Promise<void> => {
6060
// Since _toInternalRepresentation is private, we have to resort to this little hack to access the method.
6161
// That syntax keeps the argument type checking, while casting as any would not.
62-
RNBatch.profile_trackEvent(name, data instanceof BatchEventAttributes ? data['_toInternalRepresentation']() : null);
62+
return RNBatch.profile_trackEvent(name, data instanceof BatchEventAttributes ? data['_toInternalRepresentation']() : null);
6363
},
6464

6565
/**

0 commit comments

Comments
 (0)