/
error_reporting.dart
83 lines (78 loc) · 2.39 KB
/
error_reporting.dart
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
import 'dart:async';
import 'dart:io';
import 'package:device_info/device_info.dart';
import 'package:flutter/foundation.dart';
import 'package:package_info/package_info.dart';
import 'package:sentry/sentry.dart';
import 'package:sketchnotes2/env.dart';
import 'package:sketchnotes2/logging.dart';
Future<SentryClient> get _sentryClient async => SentryClient(
dsn: DSN,
environmentAttributes: await _environmentEvent,
);
Future<Event> get _environmentEvent async {
final packageInfo = await PackageInfo.fromPlatform();
final deviceInfoPlugin = await DeviceInfoPlugin();
OperatingSystem os;
Device device;
if (Platform.isAndroid) {
final androidInfo = await deviceInfoPlugin.androidInfo;
os = OperatingSystem(
name: 'android',
version: androidInfo.version.release,
);
device = Device(
model: androidInfo.model,
manufacturer: androidInfo.manufacturer,
modelId: androidInfo.product,
);
} else if (Platform.isIOS) {
final iosInfo = await deviceInfoPlugin.iosInfo;
os = OperatingSystem(
name: iosInfo.systemName,
version: iosInfo.systemVersion,
);
device = Device(
model: iosInfo.utsname.machine,
family: iosInfo.model,
manufacturer: 'Apple',
);
}
final environment = Event(
release: '${packageInfo.version} (${packageInfo.buildNumber})',
environment: 'production',
contexts: Contexts(
operatingSystem: os,
device: device,
app: App(
name: packageInfo.appName,
version: packageInfo.version,
build: packageInfo.buildNumber,
),
),
);
return environment;
}
void flutterOnErrorHandler(FlutterErrorDetails details) {
if (kReleaseMode) {
// In production mode, send error to the application zone.
Zone.current.handleUncaughtError(details.exception, details.stack);
} else {
// In debug mode, simply print to console.
FlutterError.dumpErrorToConsole(details);
}
}
Future<void> reportError(Object error, StackTrace stackTrace) async {
if (kReleaseMode) {
LOG.e('Reported to Sentry: $error');
final sentry = await _sentryClient;
// Send the Exception and Stacktrace to Sentry in Production mode.
await sentry.captureException(
exception: error,
stackTrace: stackTrace,
);
} else {
// In debug mode, simply log the error and stacktrace
LOG.e('Reportable Error:', error, stackTrace);
}
}