Skip to content

fix(messaging,android): include cause chain in native exception messages - #18999

Open
SelaseKay wants to merge 1 commit into
mainfrom
fix/messaging-android-exception-cause
Open

SelaseKay wants to merge 1 commit into
mainfrom
fix/messaging-android-exception-cause

Conversation

@SelaseKay

Copy link
Copy Markdown
Contributor

Description

Since firebase-messaging 25.1.0 (firebase/firebase-android-sdk#8087), FirebaseMessaging.blockingRegister() wraps registration failures in new IOException("FCM Registration failed!", e) instead of new IOException(e). The underlying failure (such as SERVICE_NOT_AVAILABLE, TIMEOUT, or AUTHENTICATION_FAILED) is preserved on Throwable.getCause(), rather than in the outer getMessage().

FlutterFirebaseMessagingPlugin previously forwarded only exception.getMessage() across the Android MethodChannel and dropped the cause chain, so Dart callers of getToken() and register() received the identical [firebase_messaging/unknown] java.io.IOException: FCM Registration failed! message regardless of the underlying reason.

This change adds FlutterFirebaseMessagingPlugin.formatExceptionMessage(...) and uses it in both result.error(...) and getExceptionDetails(...). It walks Throwable.getCause() (guarded against cycles), skips plain Tasks.await ExecutionException wrappers whose message only mirrors their cause, and appends each distinct underlying cause as Caused by: <cause> when not already present in the message.

Related Issues

Fixes #18997

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process. Updating the pubspec.yaml and changelogs is not required.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

Test plan

  • Added JUnit tests in FlutterFirebaseMessagingPluginExceptionTest covering FCM Registration failed! wrapped in ExecutionException, multi-hop cause chains, custom ExecutionException messages, pre-25.1.0 non-duplication, single/plain exceptions, null messages/exceptions, and cyclic cause chains; ran ./gradlew :firebase_messaging:testDebugUnitTest.
  • Added an Android E2E test in packages/firebase_messaging/firebase_messaging/example/integration_test/e2e_test.dart that unregisters, blocks com.google.android.gms networking via cmd connectivity, calls register(), and verifies the thrown FirebaseException.message matches FCM Registration failed! Caused by: \S+ before restoring networking; ran flutter test integration_test/e2e_test.dart --dart-define=CI=true on an Android emulator (20 passed, 0 failed).
  • Ran flutter_plugin_tools format, ./.github/workflows/scripts/validate-formatting.sh, melos run check-license-header, and flutter_plugin_tools analyze --base-branch=origin/main --custom-analysis=.github/workflows/config/custom_analysis.yaml.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request improves error reporting in the Firebase Messaging Android plugin by formatting nested exception causes (such as FCM registration failures) to include the underlying error details. It also adds comprehensive unit tests for this formatting logic and an integration test that simulates network blockages. The review feedback suggests wrapping a shell command execution inside a finally block in MainActivity.kt with a try-catch to prevent masking any original exceptions.

Comment on lines +164 to +166
} finally {
executeShell("cmd connectivity set-chain3-enabled false")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

In the finally block, if executeShell throws an exception, it will mask and discard any original exception thrown in the preceding try block. To prevent masking the original exception, wrap the finally block's shell execution in a try-catch block.

Suggested change
} finally {
executeShell("cmd connectivity set-chain3-enabled false")
}
} finally {
try {
executeShell("cmd connectivity set-chain3-enabled false")
} catch (_: Exception) {}
}

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[firebase_messaging]: Android getToken() errors lose their cause since firebase-messaging 25.1.0 ("FCM Registration failed!")

2 participants