Skip to content

Commit d05cb36

Browse files
authored
fix(fcm): Gracefully handling non-json error responses (#508)
1 parent b5f8c2a commit d05cb36

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private MessagingServiceErrorResponse safeParse(String response) {
307307
try {
308308
return jsonFactory.createJsonParser(response)
309309
.parseAndClose(MessagingServiceErrorResponse.class);
310-
} catch (IOException ignore) {
310+
} catch (Exception ignore) {
311311
// Ignore any error that may occur while parsing the error response. The server
312312
// may have responded with a non-json payload.
313313
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class FirebaseMessagingClientImplTest {
8282
.setTopic("test-topic")
8383
.build();
8484
private static final List<Message> MESSAGE_LIST = ImmutableList.of(EMPTY_MESSAGE, EMPTY_MESSAGE);
85-
85+
8686
private static final boolean DRY_RUN_ENABLED = true;
8787
private static final boolean DRY_RUN_DISABLED = false;
8888

@@ -100,7 +100,7 @@ public void setUp() {
100100
@Test
101101
public void testSend() throws Exception {
102102
Map<Message, Map<String, Object>> testMessages = buildTestMessages();
103-
103+
104104
for (Map.Entry<Message, Map<String, Object>> entry : testMessages.entrySet()) {
105105
response.setContent(MOCK_RESPONSE);
106106
String resp = client.send(entry.getKey(), DRY_RUN_DISABLED);
@@ -200,14 +200,14 @@ public void testSendErrorWithZeroContentResponse() {
200200
@Test
201201
public void testSendErrorWithMalformedResponse() {
202202
for (int code : HTTP_ERRORS) {
203-
response.setStatusCode(code).setContent("not json");
203+
response.setStatusCode(code).setContent("[not json]");
204204

205205
try {
206206
client.send(EMPTY_MESSAGE, DRY_RUN_DISABLED);
207207
fail("No error thrown for HTTP error");
208208
} catch (FirebaseMessagingException error) {
209209
checkExceptionFromHttpResponse(error, HTTP_2_ERROR.get(code), null,
210-
"Unexpected HTTP response with status: " + code + "\nnot json");
210+
"Unexpected HTTP response with status: " + code + "\n[not json]");
211211
}
212212
checkRequestHeader(interceptor.getLastRequest());
213213
}

0 commit comments

Comments
 (0)