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

Is there a way to call Sentry.captureException on the native side? #393

Closed
mlostekk opened this issue Jun 22, 2023 · 10 comments
Closed

Is there a way to call Sentry.captureException on the native side? #393

mlostekk opened this issue Jun 22, 2023 · 10 comments
Labels
Platform: Capacitor question Further information is requested Status: Stale

Comments

@mlostekk
Copy link
Contributor

mlostekk commented Jun 22, 2023

I am using sentry in a capacitor project, and it is working as expected. The only thing I am wondering is if its possible to also call Sentry.captureException on the native side (lets say somewhere in Android native code).

We are developing a lot of own plugins and extending the native code pretty heavily, so this would be really helpful.

Thanks for any leads.

@mlostekk
Copy link
Contributor Author

mlostekk commented Jul 5, 2023

No way?

@lucas-zimerman
Copy link
Collaborator

Since the SDK is bundled with the Native SDKs you can follow the Native documentation of Android or iOS for capturing errors. Let me know if you need anything else.

@lucas-zimerman lucas-zimerman added the question Further information is requested label Jul 7, 2023
@github-actions
Copy link
Contributor

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@mlostekk
Copy link
Contributor Author

mlostekk commented Aug 2, 2023

The problem is that I cannot import these files. I have to explicitly add the native Sentry SDK as a dependency in my application's build.gradle file. Is there a trick to access this transitive dependency (app -> capacitor sentry -> native sentry)?

@github-actions
Copy link
Contributor

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@mlostekk
Copy link
Contributor Author

no trick?

@github-actions
Copy link
Contributor

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 27, 2023
@bourquep
Copy link

bourquep commented Nov 8, 2023

I want to do that as well and can't figure out how...

@emilianogonzalezar
Copy link

For anyone that needs to do this: you can export Sentry's captureException function through the window and executeJavascript from within the native code:

In Typescript:

declare const window: {
  sentry?: any;
} & Window;

window.sentry = { captureException };

In native Android Java:

public final class Sentry {
    public static void captureException(final Plugin plugin, final String message) {
        new Handler(Looper.getMainLooper()).post(() -> {
            final String script = String.format("window.sentry.captureException(\"%s\");", message);
            plugin.getBridge()
                    .getWebView()
                    .evaluateJavascript(script, null);
        });
    }
}

In native iOS Swift:

final class Sentry {
  private init() {
  }

  static func captureException(plugin: CAPPlugin, message: String) {
    plugin.webView?.evaluateJavaScript("window.sentry.captureException(\"" + message + "\");");
  }
}

You just need to call the Sentry.captureException function from within your plugins.

@mlostekk
Copy link
Contributor Author

Thats smart, but still huge overhead to just track a native exception through the JS layer. I have discarded all capacitor plugins and written my own version with Koin as DI. I think that is a more modern way (Plugins get their implementations injected via Koin, that way testing and invocation from native is super trivial, just thinking of Play Games Auth or Firebase restore being triggered while the JS layer is not even aware that it exists)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: Capacitor question Further information is requested Status: Stale
Projects
Archived in project
Development

No branches or pull requests

4 participants