11package com.nativebrik.flutter.nativebrik_bridge
22
33import android.content.Context
4+ import io.nubrick.nubrick.FlutterBridgeApi
45import io.nubrick.nubrick.NubrickClient
56import io.nubrick.nubrick.__DO_NOT_USE_THIS_INTERNAL_BRIDGE
7+ import io.nubrick.nubrick.data.ExceptionRecord
68import io.nubrick.nubrick.data.NotFoundException
9+ import io.nubrick.nubrick.data.StackFrame
10+ import io.nubrick.nubrick.data.TrackCrashEvent
711import io.flutter.plugin.platform.PlatformView
812import io.flutter.plugin.platform.PlatformViewFactory
913import io.flutter.plugin.common.StandardMessageCodec
@@ -25,6 +29,7 @@ import kotlinx.coroutines.launch
2529
2630internal data class ConfigEntity (val variant : RemoteConfigVariant ? , val experimentId : String? )
2731
32+ @OptIn(FlutterBridgeApi ::class )
2833internal class NativebrikBridgeManager (private val binaryMessenger : BinaryMessenger ) {
2934 private var nativebrikClient: NubrickClient ? = null
3035 private var bridgeClient: __DO_NOT_USE_THIS_INTERNAL_BRIDGE ? = null
@@ -214,12 +219,54 @@ internal class NativebrikBridgeManager(private val binaryMessenger: BinaryMessen
214219 }
215220
216221 /* *
217- * Records a crash with the given throwable .
222+ * Records exceptions from Flutter .
218223 *
219- * This method forwards the throwable to the Nativebrik SDK for crash reporting.
224+ * This method constructs a crash event and forwards it to the Nativebrik SDK for crash reporting
225+ * with platform set to "flutter".
220226 */
221- fun recordCrash (throwable : Throwable ) {
222- this .nativebrikClient?.experiment?.record(throwable)
227+ fun recordFlutterExceptions (exceptionsList : List <Map <String , Any ?>>, flutterSdkVersion : String? ) {
228+ try {
229+ val exceptions = exceptionsList.mapNotNull { exceptionMap ->
230+ try {
231+ val type = exceptionMap[" type" ] as ? String
232+ val message = exceptionMap[" message" ] as ? String
233+ val callStacksList = exceptionMap[" callStacks" ] as ? List <* >
234+
235+ val callStacks = callStacksList?.mapNotNull { frameMap ->
236+ try {
237+ val frame = frameMap as ? Map <* , * >
238+ StackFrame (
239+ fileName = frame?.get(" fileName" ) as ? String ,
240+ className = frame?.get(" className" ) as ? String ,
241+ methodName = frame?.get(" methodName" ) as ? String ,
242+ lineNumber = (frame?.get(" lineNumber" ) as ? Number )?.toInt()
243+ )
244+ } catch (e: Exception ) {
245+ null
246+ }
247+ }
248+
249+ ExceptionRecord (
250+ type = type,
251+ message = message,
252+ callStacks = callStacks
253+ )
254+ } catch (e: Exception ) {
255+ null
256+ }
257+ }
258+
259+ if (exceptions.isNotEmpty()) {
260+ val crashEvent = TrackCrashEvent (
261+ exceptions = exceptions,
262+ platform = " flutter" ,
263+ flutterSdkVersion = flutterSdkVersion
264+ )
265+ this .nativebrikClient?.experiment?.sendFlutterCrash(crashEvent)
266+ }
267+ } catch (e: Exception ) {
268+ // Silently fail to avoid causing crashes in error reporting
269+ }
223270 }
224271}
225272
0 commit comments