This repository was archived by the owner on Dec 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathcrash_reporting.dart
More file actions
125 lines (109 loc) · 3.36 KB
/
crash_reporting.dart
File metadata and controls
125 lines (109 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// ignore_for_file: avoid_classes_with_only_static_members
import 'dart:async';
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:instabug_flutter/src/generated/crash_reporting.api.g.dart';
import 'package:instabug_flutter/src/models/crash_data.dart';
import 'package:instabug_flutter/src/models/exception_data.dart';
import 'package:instabug_flutter/src/utils/ibg_build_info.dart';
import 'package:stack_trace/stack_trace.dart';
enum NonFatalExceptionLevel { error, critical, info, warning }
class CrashReporting {
static var _host = CrashReportingHostApi();
static bool enabled = true;
/// @nodoc
@visibleForTesting
// ignore: use_setters_to_change_properties
static void $setHostApi(CrashReportingHostApi host) {
_host = host;
}
/// Enables and disables Enables and disables automatic crash reporting.
/// [boolean] isEnabled
static Future<void> setEnabled(bool isEnabled) async {
enabled = isEnabled;
return _host.setEnabled(isEnabled);
}
/// Enables and disables automatic NDK crash reporting on Android.
/// [boolean] isEnabled
static Future<void> setNDKEnabled(bool isEnabled) async {
return _host.setNDKEnabled(isEnabled);
}
static Future<void> reportCrash(Object exception, StackTrace stack) async {
if (IBGBuildInfo.instance.isReleaseMode && enabled) {
await _reportUnhandledCrash(exception, stack);
} else {
FlutterError.dumpErrorToConsole(
FlutterErrorDetails(stack: stack, exception: exception),
);
}
}
/// Reports a handled crash to you dashboard
/// [Object] exception
/// [StackTrace] stack
static Future<void> reportHandledCrash(
Object exception,
StackTrace? stack, {
Map<String, String>? userAttributes,
String? fingerprint,
NonFatalExceptionLevel level = NonFatalExceptionLevel.error,
}) async {
await _sendHandledCrash(
exception,
stack ?? StackTrace.current,
userAttributes,
fingerprint,
level,
);
}
static Future<void> _reportUnhandledCrash(
Object exception,
StackTrace stack,
) async {
await _sendCrash(exception, stack, false);
}
static Future<void> _sendCrash(
Object exception,
StackTrace stack,
bool handled,
) async {
final crashData = getCrashDataFromException(stack, exception);
return _host.send(jsonEncode(crashData), handled);
}
static Future<void> _sendHandledCrash(
Object exception,
StackTrace stack,
Map<String, String>? userAttributes,
String? fingerprint,
NonFatalExceptionLevel? nonFatalExceptionLevel,
) async {
final crashData = getCrashDataFromException(stack, exception);
return _host.sendNonFatalError(
jsonEncode(crashData),
userAttributes,
fingerprint,
nonFatalExceptionLevel.toString(),
);
}
static CrashData getCrashDataFromException(
StackTrace stack,
Object exception,
) {
final trace = Trace.from(stack);
final frames = trace.frames
.map(
(frame) => ExceptionData(
file: frame.uri.toString(),
methodName: frame.member,
lineNumber: frame.line,
column: frame.column ?? 0,
),
)
.toList();
final crashData = CrashData(
os: IBGBuildInfo.instance.operatingSystem,
message: exception.toString(),
exception: frames,
);
return crashData;
}
}