How to effectively group non fatal exceptions in Crashlytics (Fabrics)?

前端 未结 5 839
忘了有多久
忘了有多久 2021-01-11 19:12

We are using Crashlytics in our app as the crash reporting tool. For Android native crashes, it\'s working fine and grouping the crashes correctly. Our app also has few comp

5条回答
  •  天涯浪人
    2021-01-11 19:24

    Best way I've found to do this is to manually chop the shared parts of the stacktrace off:

    
    private fun buildCrashlyticsSyntheticException(message: String): Exception {
      val stackTrace = Thread.currentThread().stackTrace
      val numToRemove = 8
      val lastToRemove = stackTrace[numToRemove - 1]
      // This ensures that if the stacktrace format changes, we get notified immediately by the app
      // crashing (as opposed to silently mis-grouping crashes for an entire release).
      check(lastToRemove.className == "timber.log.Timber" && lastToRemove.methodName == "e",
        { "Got unexpected stacktrace: $stackTrace" })
      val abbreviatedStackTrace = stackTrace.takeLast(stackTrace.size - numToRemove).toTypedArray()
      return SyntheticException("Synthetic Exception: $message", abbreviatedStackTrace)
    }
    
    class SyntheticException(
      message: String,
      private val abbreviatedStackTrace: Array
    ) : Exception(message) {
      override fun getStackTrace(): Array {
        return abbreviatedStackTrace
      }
    }
    

    This way the message can be parameterized Timber.e("Got a weird error $error while eating a taco") and all of that line's calls will be grouped together.

    Obviously, numToRemove will need to change depending on your exact mechanism for triggering nonfatals.

提交回复
热议问题