Skip to content

Conversation

hiranya911
Copy link
Contributor

@hiranya911 hiranya911 commented Jan 7, 2021

FCM service seems to occasionally respond with non-json (HTML) error responses, and if these responses contain certain special characters (e.g. [ or ]), the SDK throws a cryptic JSON parse exception (see #506).

The MessagingErrorHandler is supposed to catch all parse errors, but today it only handles IOExceptions thrown by the parser. But in the above case parser throws an IllegalArgumentException. This PR addresses this issue by updating the MessagingErrorHandler to catch all exceptions thrown by the parser. This makes sure the developer gets a FirebaseMessagingException with the original error response attached to it, when the above error condition occurs.

RELEASE NOTE: Improved support for handling certain non-JSON error responses sent by the FCM backend service.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants