Skip to content

Commit ad9e237

Browse files
authored
[App Distribution] Fix crash caused by response being nil (#7067)
* [App Distribution] Handle response being nil When an error occurs, the response data will be nil. This causes a crash because we are trying to parse the response as JSON. Instead, return a generic error message if the response data is nil.
1 parent 33173a2 commit ad9e237

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

FirebaseAppDistribution/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22
- [changed] Sign out the Tester when the call to fetch releases fails with an unauthenticated error.
3+
- [fixed] Crash caused by trying to parse response as JSON when response is nil (#6996).
34

45
# v0.9.3
56
- [changed] Updated error log for non-200 API Service calls.

FirebaseAppDistribution/Sources/FIRFADApiService.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ + (NSMutableURLRequest *)createHTTPRequest:(NSString *)method
7979
}
8080

8181
+ (NSString *)tryParseGoogleAPIErrorFromResponse:(NSData *)data {
82+
if (!data) {
83+
return @"No data in response.";
84+
}
85+
8286
NSError *parseError;
8387
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
8488
options:0
@@ -151,7 +155,6 @@ + (BOOL)handleHttpResponseError:(NSHTTPURLResponse *)httpResponse error:(NSError
151155
return [self handleError:error
152156
description:@"Unknown http error occurred"
153157
code:FIRApiErrorUnknownFailure];
154-
;
155158
}
156159

157160
if ([httpResponse statusCode] != 200) {

FirebaseAppDistribution/Tests/Unit/FIRFADApiServiceTests.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ - (void)testTryParseGoogleAPIErrorFromResponseSuccess {
175175
XCTAssertTrue([message isEqualToString:_mockAPINotEnabledMessage]);
176176
}
177177

178+
- (void)testTryParseGoogleAPIErrorFromNilResponse {
179+
NSString *message = [FIRFADApiService tryParseGoogleAPIErrorFromResponse:nil];
180+
XCTAssertTrue([message isEqualToString:@"No data in response."]);
181+
}
182+
178183
- (void)testTryParseGoogleAPIErrorFromResponseParseFailure {
179184
NSData *data = [@"malformed{json[data" dataUsingEncoding:NSUTF8StringEncoding];
180185
NSString *message = [FIRFADApiService tryParseGoogleAPIErrorFromResponse:data];

0 commit comments

Comments
 (0)