Skip to content
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

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

Merged
merged 1 commit into from
Jan 8, 2021

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.

@@ -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.

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

Successfully merging this pull request may close these issues.

2 participants