Skip to content

feat(crashlytics): support custom properties in recordException(...) method #825

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 6 commits into from
Feb 27, 2025

Conversation

adrenaline15
Copy link
Contributor

@adrenaline15 adrenaline15 commented Feb 18, 2025

https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#log-excepts

Pull request checklist

Please check if your PR fulfills the following requirements:

  • The changes have been tested successfully.
  • A changeset has been created (npm run changeset).
  • I have read and followed the pull request guidelines.

@adrenaline15 adrenaline15 changed the title * fixes #824 feat(crashlytics): Extend recordException to provide a possibility to add custom properties per exception and not only globally Feb 18, 2025
* correct usage of `JSONObject` instead of `JSObject`
* testing done
@adrenaline15
Copy link
Contributor Author

image

    const stacktrace = await StackTrace.fromError(err);
    await FirebaseCrashlytics.recordException({
      message: '[NOT AN EXCEPTION]',
      stacktrace,
      customProperties: [
        {
          type: 'string',
          key: 'error_name',
          value: err?.name,
        },
        {
          type: 'string',
          key: 'error_message',
          value: err?.message,
        },
        {
          type: 'string',
          key: 'error_type',
          value: err?.type,
        },
        !!err.code && {
          type: 'string',
          key: 'error_code',
          value: err.code,
        },
      ],
    });

Copy link
Member

@robingenz robingenz left a comment

Choose a reason for hiding this comment

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

Please also add iOS support if it's supported by the Firebase iOS SDK since this plugin supports both platforms.

@adrenaline15
Copy link
Contributor Author

adrenaline15 commented Feb 18, 2025

Please also add iOS support if it's supported by the Firebase iOS SDK since this plugin supports both platforms.

actually it looks like its not supported the same way on iOS.. (i also added it to the issue #824)

firebase/firebase-ios-sdk#13153

maybe it could be achieved tho..

https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#log-excepts

i just feel like it would be all reported as string then? so theoretically not the same.. but i could be wrong here.

@adrenaline15
Copy link
Contributor Author

to quickly follow-up on this topic for iOS:

looks like the userinfo-way will not work when using a custom error-model = when a stacktrace is provided:

firebase/firebase-ios-sdk#11452 (comment)

that means the only possibility to somehow achieve this would be to set them as global values before and resetting them right after the report.

Crashlytics.crashlytics().setCustomValue("value", forKey: key)
Crashlytics.crashlytics().record(exceptionModel: error)
Crashlytics.crashlytics().setCustomValue(nil, forKey: key)

@robingenz
Copy link
Member

In this case just ignore the customProperties property if a stacktrace is provided. Make sure to document this in the property description.

@adrenaline15
Copy link
Contributor Author

sadly that means there is no way to have both then right?

so a stacktrace + custom properties per non-fatal crash

which honestly was my initial motivation, since i wanted to debug keychain-errors for ios^^

@robingenz
Copy link
Member

sadly that means there is no way to have both then right?

Right. But you could implement the workaround you mentioned here yourself using the plugin API. This does not need to be implemented in the plugin itself.

@adrenaline15
Copy link
Contributor Author

valid point... seems to be suboptimal either way 🫤

i will try my luck with implementing this then.

@robingenz
Copy link
Member

Yes, i know. Unfortunately, there is nothing we can do about it. It's a limitation of the iOS SDK. Do you still want to finish the PR?

@adrenaline15
Copy link
Contributor Author

yes, i think i'm pretty close :)

…ations as outlined in the changeset/readme

* updates the definitions, readme and changeset
@adrenaline15
Copy link
Contributor Author

sorry, needed to finish some work.

i did now implement the iOS-part too, so feel free to have a look and change it accordingly 🙏

actually i forgot that this would not work in the current implementation:

Crashlytics.crashlytics().setCustomValue("value", forKey: key)
Crashlytics.crashlytics().record(exceptionModel: error)
Crashlytics.crashlytics().setCustomValue(nil, forKey: key)

for this there would need to be another method to clear/reset a global value by its key (or provide a dedicated type for it in the setCustomValue-fn)

Comment on lines 5 to 8
- adds the possibility to add dedicated custom properties when recording a non-fatal exception as outlined here:
- https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#log-excepts
- https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#log-excepts
> Note: on iOS this cannot be combined with `stacktrace` due to [limitations in the ios-sdk](https://github.com/firebase/firebase-ios-sdk/discussions/11452#discussioncomment-6277129).
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- adds the possibility to add dedicated custom properties when recording a non-fatal exception as outlined here:
- https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=android#log-excepts
- https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#log-excepts
> Note: on iOS this cannot be combined with `stacktrace` due to [limitations in the ios-sdk](https://github.com/firebase/firebase-ios-sdk/discussions/11452#discussioncomment-6277129).
feat: support custom properties in `recordException(...)` method

Please just use a one-liner here.

@@ -56,9 +59,67 @@ public void deleteUnsentReports() {
getFirebaseCrashlyticsInstance().deleteUnsentReports();
}

public void recordException(String message, JSArray stacktrace) {
public void recordException(String message, JSArray stacktrace, JSArray customProperties) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public void recordException(String message, JSArray stacktrace, JSArray customProperties) {
public void recordException(String message, JSArray stacktrace, @Nullable JSArray customProperties) {

Let's add the @Nullable annotation to make it easier to maintain.

@robingenz
Copy link
Member

@adrenaline15 FYI: I'm releasing a new version of Capacitor Firebase tomorrow. It would be cool if we could include your feature there.

@adrenaline15
Copy link
Contributor Author

i did now update the MR with the desired changes 🤞

Copy link
Member

@robingenz robingenz left a comment

Choose a reason for hiding this comment

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

Please run npm run fmt to format your code.

> `npm run fmt`
Copy link

pkg-pr-new bot commented Feb 27, 2025

Open in Stackblitz

@capacitor-firebase/analytics

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/analytics@825

@capacitor-firebase/app

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/app@825

@capacitor-firebase/app-check

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/app-check@825

@capacitor-firebase/authentication

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/authentication@825

@capacitor-firebase/crashlytics

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/crashlytics@825

@capacitor-firebase/firestore

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/firestore@825

@capacitor-firebase/functions

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/functions@825

@capacitor-firebase/messaging

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/messaging@825

@capacitor-firebase/performance

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/performance@825

@capacitor-firebase/remote-config

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/remote-config@825

@capacitor-firebase/storage

npm i https://pkg.pr.new/capawesome-team/capacitor-firebase/@capacitor-firebase/storage@825

commit: 41f9ff8

Copy link
Member

@robingenz robingenz left a comment

Choose a reason for hiding this comment

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

Thank you!

@robingenz robingenz changed the title feat(crashlytics): Extend recordException to provide a possibility to add custom properties per exception and not only globally feat(crashlytics): support custom properties in recordException(...) method Feb 27, 2025
@robingenz robingenz merged commit 9d67b1c into capawesome-team:main Feb 27, 2025
4 checks passed
@github-actions github-actions bot mentioned this pull request Feb 26, 2025
@robingenz
Copy link
Member

@adrenaline15 I just noticed that these custom properties are called keys and values in Crashlytics. I have therefore renamed the option, see #835. Please don't be surprised and thank you for your contribution!

@adrenaline15
Copy link
Contributor Author

adrenaline15 commented Feb 27, 2025

no worries :) thanks for your great contributions to the capacitor-ecosystem <3

@gcarpi
Copy link

gcarpi commented Apr 4, 2025

Screenshot 2025-04-03 at 11 57 26

Hi guys, I updated my project to v7.1.0, and it seems there's an error when calling the recordException method without passing the keysAndValues. In v7.0.0, this error didn't occur. I tested it on both Android and iOS, and it seems the error only occurs on Android.

To fix the issue, I had to pass keysAndValues as an empty array, like this:

FirebaseCrashlytics.recordException({ ...options, keysAndValues: []})

@robingenz
Copy link
Member

This is already fixed and will be published soon: #842

@gcarpi
Copy link

gcarpi commented Apr 4, 2025

Thank you @robingenz

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