Skip to content

fix(fcm): Gracefully handling non-json error responses #508

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

Merged
merged 1 commit into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private MessagingServiceErrorResponse safeParse(String response) {
try {
return jsonFactory.createJsonParser(response)
.parseAndClose(MessagingServiceErrorResponse.class);
} catch (IOException ignore) {
} catch (Exception ignore) {
// Ignore any error that may occur while parsing the error response. The server
// may have responded with a non-json payload.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class FirebaseMessagingClientImplTest {
.setTopic("test-topic")
.build();
private static final List<Message> MESSAGE_LIST = ImmutableList.of(EMPTY_MESSAGE, EMPTY_MESSAGE);

private static final boolean DRY_RUN_ENABLED = true;
private static final boolean DRY_RUN_DISABLED = false;

Expand All @@ -100,7 +100,7 @@ public void setUp() {
@Test
public void testSend() throws Exception {
Map<Message, Map<String, Object>> testMessages = buildTestMessages();

for (Map.Entry<Message, Map<String, Object>> entry : testMessages.entrySet()) {
response.setContent(MOCK_RESPONSE);
String resp = client.send(entry.getKey(), DRY_RUN_DISABLED);
Expand Down Expand Up @@ -200,14 +200,14 @@ public void testSendErrorWithZeroContentResponse() {
@Test
public void testSendErrorWithMalformedResponse() {
for (int code : HTTP_ERRORS) {
response.setStatusCode(code).setContent("not json");
response.setStatusCode(code).setContent("[not json]");
Copy link
Contributor

@chong-shao chong-shao Jan 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if a text that looks more like HTML can be more representative here, e.g., <html>"not json"</html> ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my tests and inspection of the JsonParser source, we need more than just HTML tags to trigger this IllegalArgumentException. Square brackets seems to be the easiest and most reliable way. Although it makes me wonder what kind of response was served by FCM to cause this error in the first place.


try {
client.send(EMPTY_MESSAGE, DRY_RUN_DISABLED);
fail("No error thrown for HTTP error");
} catch (FirebaseMessagingException error) {
checkExceptionFromHttpResponse(error, HTTP_2_ERROR.get(code), null,
"Unexpected HTTP response with status: " + code + "\nnot json");
"Unexpected HTTP response with status: " + code + "\n[not json]");
}
checkRequestHeader(interceptor.getLastRequest());
}
Expand Down