Skip to content

Commit dd006da

Browse files
tjleingThomas Leing
andauthored
chore(analytics): Simplify error code checking, uniformly across platforms (#3191)
* Simplify error code checking, uniformly across platforms * add unit test --------- Co-authored-by: Thomas Leing <[email protected]>
1 parent 9864a0f commit dd006da

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

aws-android-sdk-pinpoint/src/main/java/com/amazonaws/mobileconnectors/pinpoint/internal/event/EventRecorder.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,22 +438,22 @@ private void submitEventsAndEndpoint(final JSONArray eventArray,
438438
} catch (final AmazonServiceException amazonServiceException) {
439439
// This is service level exception, we also have item level exception.
440440
log.error("AmazonServiceException occurred during send of put event ", amazonServiceException);
441-
final String errorCode = amazonServiceException.getErrorCode();
441+
final int statusCode = amazonServiceException.getStatusCode();
442442

443443
// If the error is not a retryable error, delete the events from the local database.
444444
// Else if the error is a retryable error, keep the events in the local database.
445-
if (isRetryable(errorCode)) {
445+
if (isRetryable(statusCode)) {
446446
log.error(
447447
String.format("AmazonServiceException: Unable to successfully deliver events to server. " +
448448
"Events will be saved, error is likely recoverable. " +
449449
"Response Status code: %s, Response Error Code: %s",
450-
amazonServiceException.getStatusCode(), amazonServiceException.getErrorCode()),
450+
statusCode, amazonServiceException.getErrorCode()),
451451
amazonServiceException);
452452
batchIdsAndSizeToDelete.clear();
453453
} else {
454454
log.error(
455455
String.format(Locale.getDefault(), "Failed to submit events to EventService: statusCode: " +
456-
amazonServiceException.getStatusCode() + " errorCode: ", errorCode),
456+
statusCode + " errorCode: ", amazonServiceException.getErrorCode()),
457457
amazonServiceException);
458458
log.error(
459459
String.format(Locale.getDefault(), "Failed submission of %d events, events will be " +
@@ -531,7 +531,7 @@ private void processEventsResponse(final JSONArray eventArray,
531531
// so the event does not get deleted from the local database.
532532
if (responseMessage.getMessage().equalsIgnoreCase("Accepted")) {
533533
log.info(String.format("Successful submit event with event id %s", eventId));
534-
} else if (isRetryable(responseMessage.getMessage())) {
534+
} else if (isRetryable(responseMessage.getStatusCode())) {
535535
log.warn(String.format("Unable to successfully deliver event to server. " +
536536
"Event will be saved. Event id %s", eventId));
537537
batchIdsAndSizeToDelete.remove(eventArray.getJSONObject(i).getInt(DATABASE_ID_KEY));
@@ -548,13 +548,8 @@ private void processEventsResponse(final JSONArray eventArray,
548548
}
549549
}
550550

551-
private boolean isRetryable(String responseCode) {
552-
if (responseCode.equalsIgnoreCase("ValidationException") ||
553-
responseCode.equalsIgnoreCase("SerializationException") ||
554-
responseCode.equalsIgnoreCase("BadRequestException")) {
555-
return false;
556-
}
557-
return true;
551+
private boolean isRetryable(int httpCode) {
552+
return httpCode >= 500 && httpCode <= 599;
558553
}
559554

560555
private boolean isClientExceptionRetryable(Throwable amazonClientException) {

aws-android-sdk-pinpoint/src/test/java/com/amazonaws/mobileconnectors/pinpoint/internal/event/EventRecorderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.amazonaws.services.pinpoint.model.Event;
5454
import com.amazonaws.services.pinpoint.model.EventItemResponse;
5555
import com.amazonaws.services.pinpoint.model.EventsResponse;
56+
import com.amazonaws.services.pinpoint.model.InternalServerErrorException;
5657
import com.amazonaws.services.pinpoint.model.ItemResponse;
5758
import com.amazonaws.services.pinpoint.model.PutEventsRequest;
5859
import com.amazonaws.services.pinpoint.model.PutEventsResult;
@@ -194,6 +195,7 @@ public void testReadEventFromCursorThrowsException() throws Exception {
194195
public void testProcessEventWithAPIError() {
195196
BadRequestException badRequestException = new BadRequestException("BadRequestException");
196197
badRequestException.setErrorCode("BadRequestException");
198+
badRequestException.setStatusCode(400);
197199
eventRecorder.recordEvent(analyticsEvent);
198200
final ArrayList<String> attrValues = new ArrayList<String>();
199201
attrValues.add("TestValue");
@@ -209,6 +211,26 @@ public void testProcessEventWithAPIError() {
209211
assertTrue(dbUtil.queryAllEvents().getCount() == 0);
210212
}
211213

214+
@Test
215+
public void testProcessEventWithRetryableAPIError() {
216+
InternalServerErrorException internalServerErrorException = new InternalServerErrorException("InternalServerErrorException");
217+
internalServerErrorException.setErrorCode("InternalServerErrorException");
218+
internalServerErrorException.setStatusCode(500);
219+
eventRecorder.recordEvent(analyticsEvent);
220+
final ArrayList<String> attrValues = new ArrayList<String>();
221+
attrValues.add("TestValue");
222+
mockContext.getTargetingClient().addAttribute("Test", attrValues);
223+
//mock endpoint profile
224+
when(mockContext.getTargetingClient().currentEndpoint()).thenReturn(endpointProfile);
225+
//mock putEvents API response;
226+
when(mockContext.getPinpointServiceClient().putEvents(any(PutEventsRequest.class))).thenThrow(internalServerErrorException);
227+
//before processing events
228+
assertTrue(dbUtil.queryAllEvents().getCount() == 1);
229+
eventRecorder.processEvents();
230+
//retryable, not removed from db.
231+
assertTrue(dbUtil.queryAllEvents().getCount() == 1);
232+
}
233+
212234
@Test
213235
public void testProcessEvent_eventCount100() {
214236
PinpointDBUtil dbUtil = mock(PinpointDBUtil.class);

0 commit comments

Comments
 (0)