Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: response handler when 200 and various errors #424

Merged
merged 4 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -1020,26 +1020,22 @@ - (void)makeEventUploadPostRequest:(NSString *)url events:(NSString *)events num
BOOL uploadSuccessful = NO;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (response != nil) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if ([httpResponse statusCode] == 200) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if ([result isEqualToString:@"success"]) {
// success, remove existing events from dictionary
uploadSuccessful = YES;
if (maxEventId >= 0) {
(void) [self.dbHelper removeEvents:maxEventId];
}
if (maxIdentifyId >= 0) {
(void) [self.dbHelper removeIdentifys:maxIdentifyId];
}
} else if ([result isEqualToString:@"invalid_api_key"]) {
AMPLITUDE_ERROR(@"ERROR: Invalid API Key, make sure your API key is correct in initializeApiKey:");
} else if ([result isEqualToString:@"bad_checksum"]) {
AMPLITUDE_ERROR(@"ERROR: Bad checksum, post request was mangled in transit, will attempt to reupload later");
} else if ([result isEqualToString:@"request_db_write_failed"]) {
AMPLITUDE_ERROR(@"ERROR: Couldn't write to request database on server, will attempt to reupload later");
} else {
AMPLITUDE_ERROR(@"ERROR: %@, will attempt to reupload later", result);
// success, remove existing events from dictionary
uploadSuccessful = YES;
if (maxEventId >= 0) {
(void) [self.dbHelper removeEvents:maxEventId];
}
if (maxIdentifyId >= 0) {
(void) [self.dbHelper removeIdentifys:maxIdentifyId];
}
} else if ([result isEqualToString:@"invalid_api_key"]) {
liuyang1520 marked this conversation as resolved.
Show resolved Hide resolved
AMPLITUDE_ERROR(@"ERROR: Invalid API Key, make sure your API key is correct in initializeApiKey");
} else if ([result isEqualToString:@"bad_checksum"]) {
liuyang1520 marked this conversation as resolved.
Show resolved Hide resolved
AMPLITUDE_ERROR(@"ERROR: Bad checksum, post request was mangled in transit, will attempt to reupload later");
} else if ([result isEqualToString:@"request_db_write_failed"]) {
liuyang1520 marked this conversation as resolved.
Show resolved Hide resolved
AMPLITUDE_ERROR(@"ERROR: Couldn't write to request database on server, will attempt to reupload later");
} else if ([httpResponse statusCode] == 413) {
// If blocked by one massive event, drop it
if (numEvents == 1) {
Expand All @@ -1058,7 +1054,6 @@ - (void)makeEventUploadPostRequest:(NSString *)url events:(NSString *)events num
AMPLITUDE_LOG(@"Request too large, will decrease size and attempt to reupload");
self->_updatingCurrently = NO;
[self uploadEventsWithLimit:self->_backoffUploadBatchSize];

} else {
AMPLITUDE_ERROR(@"ERROR: Connection response received:%ld, %@", (long)[httpResponse statusCode],
[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
Expand Down
69 changes: 69 additions & 0 deletions Tests/AmplitudeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,75 @@ - (void)testUUIDInEvent {
XCTAssertNotEqual([events[0] objectForKey:@"uuid"], [events[1] objectForKey:@"uuid"]);
}

- (void)testLogEventSuccess {
[self.amplitude setEventUploadThreshold:1];
// configure response body to be random, as we only care about the status code
NSMutableDictionary *serverResponse = [NSMutableDictionary dictionaryWithDictionary:
@{ @"response" : [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"/"] statusCode:200 HTTPVersion:nil headerFields:@{}],
@"data" : [@"random-response-body" dataUsingEncoding:NSUTF8StringEncoding]
}];

[self setupAsyncResponse:serverResponse];
[self.amplitude logEvent:@"test"];
[self.amplitude flushQueue];

XCTAssertEqual(_connectionCallCount, 1);
XCTAssertEqual([self.databaseHelper getEventCount], 0);
XCTAssertEqual([self.databaseHelper getIdentifyCount], 0);
XCTAssertEqual([self.databaseHelper getTotalEventCount], 0);
}

- (void)testLogEventFailedWithInvalidApiKeyResponse {
[self.amplitude setEventUploadThreshold:1];
NSMutableDictionary *serverResponse = [NSMutableDictionary dictionaryWithDictionary:
@{ @"response" : [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"/"] statusCode:400 HTTPVersion:nil headerFields:@{}],
@"data" : [@"invalid_api_key" dataUsingEncoding:NSUTF8StringEncoding]
}];

[self setupAsyncResponse:serverResponse];
[self.amplitude logEvent:@"test"];
[self.amplitude flushQueue];

XCTAssertEqual(_connectionCallCount, 1);
XCTAssertEqual([self.databaseHelper getEventCount], 1);
XCTAssertEqual([self.databaseHelper getIdentifyCount], 0);
XCTAssertEqual([self.databaseHelper getTotalEventCount], 1);
}

- (void)testLogEventFailedWithBadChecksumResponse {
[self.amplitude setEventUploadThreshold:1];
NSMutableDictionary *serverResponse = [NSMutableDictionary dictionaryWithDictionary:
@{ @"response" : [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"/"] statusCode:400 HTTPVersion:nil headerFields:@{}],
@"data" : [@"bad_checksum" dataUsingEncoding:NSUTF8StringEncoding]
}];

[self setupAsyncResponse:serverResponse];
[self.amplitude logEvent:@"test"];
[self.amplitude flushQueue];

XCTAssertEqual(_connectionCallCount, 1);
XCTAssertEqual([self.databaseHelper getEventCount], 1);
XCTAssertEqual([self.databaseHelper getIdentifyCount], 0);
XCTAssertEqual([self.databaseHelper getTotalEventCount], 1);
}

- (void)testLogEventFailedWithRequestDbWriteFailedResponse {
[self.amplitude setEventUploadThreshold:1];
NSMutableDictionary *serverResponse = [NSMutableDictionary dictionaryWithDictionary:
@{ @"response" : [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"/"] statusCode:500 HTTPVersion:nil headerFields:@{}],
@"data" : [@"request_db_write_failed" dataUsingEncoding:NSUTF8StringEncoding]
}];

[self setupAsyncResponse:serverResponse];
[self.amplitude logEvent:@"test"];
[self.amplitude flushQueue];

XCTAssertEqual(_connectionCallCount, 1);
XCTAssertEqual([self.databaseHelper getEventCount], 1);
XCTAssertEqual([self.databaseHelper getIdentifyCount], 0);
XCTAssertEqual([self.databaseHelper getTotalEventCount], 1);
}

- (void)testIdentify {
AMPDatabaseHelper *dbHelper = [AMPDatabaseHelper getDatabaseHelper];
[self.amplitude setEventUploadThreshold:2];
Expand Down