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

Notify handled android java exceptions #223

Open
troy-lamerton opened this issue Mar 27, 2021 · 4 comments
Open

Notify handled android java exceptions #223

troy-lamerton opened this issue Mar 27, 2021 · 4 comments
Labels
backlog We hope to fix this feature/bug in the future feature request Request for a new feature

Comments

@troy-lamerton
Copy link

Description

In our Unity game, we have a lot of android plugins that need to report exceptions.

Since we use the Unity SDK, there is no obvious way to report these java/kotlin exceptions.

Describe the solution you'd like

Any way to send a java/kotlin exception to the bugsnag dashboard.

A simple api could be: public void Notify(AndroidJavaObject jvmException)

Describe alternatives you've considered

Calling Bugsnag.Notify with stacktrace string

Bugsnag.Notify(new Exception("<my android stacktrace>"))

Result: I think the message is not retraced? (class names will show a.b.c.d)

Calling the bugsnag android SDK via reflection:

package com.demo.util

import android.content.Context
import android.util.Log
import com.unity3d.player.UnityPlayer
import java.lang.Exception
import java.lang.reflect.InvocationTargetException

object BugsnagTest {
    @JvmStatic
    fun reportException() {
        val clazz = Class.forName("com.bugsnag.android.Bugsnag")

        // required, otherwise notify() will throw an exception
        val bsgInit = clazz.getDeclaredMethod("init", Context::class.java)
        safeInvoke {
            bsgInit(null, UnityPlayer.currentActivity)
        }

        val bsgNotify = clazz.getDeclaredMethod("notify", java.lang.Throwable::class.java)

        val ex = Exception("hello")
        safeInvoke {
            bsgNotify(null, ex)
        }
    }

    private fun safeInvoke(block: () -> Unit) {
        try {
            block()
        } catch (ex: InvocationTargetException) {
            Log.wtf("BugsnagTest", ex.cause)

        } catch (ex: Throwable) {
            Log.wtf("BugsnagTest", ex)
        }
    }
}

Result: This does work.. but any native crash that happens after this (e.g. SIGTRAP) is not reported to bugsnag.

@johnkiely1
Copy link
Member

Hey @troy-lamerton
As you have seen, accessing the Unity notifier to manually notify on handled java exceptions is not currently supported. We are however looking at alternative ways to do this. At the moment we don't have a definite timeline but we will post here with any updates.

@johnkiely1 johnkiely1 added backlog We hope to fix this feature/bug in the future feature request Request for a new feature labels Mar 29, 2021
@interpegasus
Copy link

Hey @johnkiely1, I would like to know if the Unity notifier only reports exceptions on Unity scenes. It the game has portions of code that are native are exceptions reported?
Would it be possible to use two integrations on the same project? ( https://docs.bugsnag.com/platforms/android/ and https://docs.bugsnag.com/platforms/unity/) Thanks

@johnkiely1
Copy link
Member

Hey @interpegasus,

Unhandled native exceptions are captured when using the Unity notifier. This applies to Android iOS and MacOs. This would occur by default once you have integrated Bugsnag into your application. https://docs.bugsnag.com/platforms/unity/#reporting-unhandled-errors

The Android/iOS/macOs notifiers are used under the hood by the Unity notifier to do this.

@troy-lamerton
Copy link
Author

We have a solution internally and are testing it at the moment. So this is lower priority for us now.

Solution goes like this:

  1. Log.getStackTraceString to serialize a Java/Kotlin Throwable to a string
  2. Send string to Unity
  3. Use new AndroidJavaException() to create a C# exception from the Throwable stacktrace string
  4. Notify bugsnag of this exception, just like any other C# exception

Code snippet for steps 1 and 2:

    @AnyThread
    @JvmStatic
    fun reportPluginError(ex: Throwable) {
        // getStackTraceString: exception class, message and stacktrace
        val readableException = Log.getStackTraceString(ex).trim().replace("\t", "    ")
        UnityPlayer.UnitySendMessage("NativePluginListener", "ReportNativeWarning", readableException)
    }

Would be good to keep this issue open to track implementing the feature in the Bugsnag source code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog We hope to fix this feature/bug in the future feature request Request for a new feature
Projects
None yet
Development

No branches or pull requests

3 participants