Skip to content

Commit 8e3ee43

Browse files
funkybookendshiranya911
authored andcommitted
Add handling of null response content (firebase#167)
* Add handling of null response content If an error response comes back with no content, the json factory throws a NullPointerException when it attempts to read the response, which hides the response code from the client. * Change catch NPE for null check in response content.
1 parent 6baace6 commit 8e3ee43

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/main/java/com/google/firebase/messaging/FirebaseMessaging.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,14 @@ private String makeSendRequest(Message message,
253253

254254
private void handleSendHttpError(HttpResponseException e) throws FirebaseMessagingException {
255255
MessagingServiceErrorResponse response = new MessagingServiceErrorResponse();
256-
try {
257-
JsonParser parser = jsonFactory.createJsonParser(e.getContent());
258-
parser.parseAndClose(response);
259-
} catch (IOException ignored) {
260-
// ignored
256+
if (e.getContent() != null) {
257+
try {
258+
JsonParser parser = jsonFactory.createJsonParser(e.getContent());
259+
parser.parseAndClose(response);
260+
} catch (IOException ignored) {
261+
// ignored
262+
}
261263
}
262-
263264
String code = FCM_ERROR_CODES.get(response.getErrorCode());
264265
if (code == null) {
265266
code = UNKNOWN_ERROR;

src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,29 @@ public void testSendError() throws Exception {
172172
}
173173
}
174174

175+
@Test
176+
public void testSendErrorWithZeroContentResponse() throws Exception {
177+
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
178+
FirebaseMessaging messaging = initMessaging(response);
179+
for (int code : HTTP_ERRORS) {
180+
response.setStatusCode(code).setZeroContent();
181+
TestResponseInterceptor interceptor = new TestResponseInterceptor();
182+
messaging.setInterceptor(interceptor);
183+
try {
184+
messaging.sendAsync(Message.builder().setTopic("test-topic").build()).get();
185+
fail("No error thrown for HTTP error");
186+
} catch (ExecutionException e) {
187+
assertTrue(e.getCause() instanceof FirebaseMessagingException);
188+
FirebaseMessagingException error = (FirebaseMessagingException) e.getCause();
189+
assertEquals("unknown-error", error.getErrorCode());
190+
assertEquals("Unexpected HTTP response with status: " + code + "; body: null",
191+
error.getMessage());
192+
assertTrue(error.getCause() instanceof HttpResponseException);
193+
}
194+
checkRequestHeader(interceptor);
195+
}
196+
}
197+
175198
@Test
176199
public void testSendErrorWithDetails() throws Exception {
177200
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();

0 commit comments

Comments
 (0)