Skip to content

Commit 2ea26e2

Browse files
authored
Merge pull request GetStream#73 from GetStream/feat/sentry
feat(sample-app): add sentry
2 parents c653931 + dd65347 commit 2ea26e2

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

packages/stream_chat_v1/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ android {
4040
defaultConfig {
4141
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
4242
applicationId "com.example.example"
43-
minSdkVersion 21
43+
minSdkVersion 22
4444
targetSdkVersion 30
4545
versionCode flutterVersionCode.toInteger()
4646
versionName flutterVersionName

packages/stream_chat_v1/lib/advanced_options_page.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ class _AdvancedOptionsPageState extends State<AdvancedOptionsPage> {
294294
),
295295
);
296296

297-
final client = StreamChatClient(
298-
apiKey,
299-
logLevel: Level.INFO,
300-
)..chatPersistenceClient = chatPersistentClient;
297+
final client = buildStreamChatClient(apiKey);
301298

302299
try {
303300
await client.connectUser(

packages/stream_chat_v1/lib/main.dart

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import 'package:example/choose_user_page.dart';
44
import 'package:example/home_page.dart';
55
import 'package:example/localizations.dart';
66
import 'package:example/splash_screen.dart';
7-
import 'package:flutter/cupertino.dart';
87
import 'package:flutter/foundation.dart';
98
import 'package:flutter/material.dart';
109
import 'package:flutter/scheduler.dart';
1110
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
11+
import 'package:sentry_flutter/sentry_flutter.dart';
1212
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
1313
import 'package:stream_chat_localizations/stream_chat_localizations.dart';
1414
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
1515
import 'package:streaming_shared_preferences/streaming_shared_preferences.dart';
1616

17-
import 'app_config.dart';
1817
import 'routes/app_routes.dart';
1918
import 'routes/routes.dart';
2019

@@ -23,8 +22,65 @@ final chatPersistentClient = StreamChatPersistenceClient(
2322
connectionMode: ConnectionMode.regular,
2423
);
2524

25+
void sampleAppLogHandler(LogRecord record) async {
26+
if (kDebugMode) StreamChatClient.defaultLogHandler(record);
27+
28+
// report errors to sentry
29+
if (record.error != null || record.stackTrace != null) {
30+
await Sentry.captureException(
31+
record.error,
32+
stackTrace: record.stackTrace,
33+
);
34+
}
35+
}
36+
37+
StreamChatClient buildStreamChatClient(
38+
String apiKey, {
39+
Level logLevel = Level.SEVERE,
40+
}) {
41+
return StreamChatClient(
42+
apiKey,
43+
logLevel: logLevel,
44+
logHandlerFunction: sampleAppLogHandler,
45+
)..chatPersistenceClient = chatPersistentClient;
46+
}
47+
2648
void main() async {
27-
runApp(MyApp());
49+
const sentryDsn =
50+
'https://[email protected]/6352870';
51+
52+
/// Captures errors reported by the Flutter framework.
53+
FlutterError.onError = (FlutterErrorDetails details) {
54+
if (kDebugMode) {
55+
// In development mode, simply print to console.
56+
FlutterError.dumpErrorToConsole(details);
57+
} else {
58+
// In production mode, report to the application zone to report to sentry.
59+
Zone.current.handleUncaughtError(details.exception, details.stack!);
60+
}
61+
};
62+
63+
Future<void> _reportError(dynamic error, StackTrace stackTrace) async {
64+
// Print the exception to the console.
65+
if (kDebugMode) {
66+
// Print the full stacktrace in debug mode.
67+
print(stackTrace);
68+
return;
69+
} else {
70+
// Send the Exception and Stacktrace to sentry in Production mode.
71+
await Sentry.captureException(error, stackTrace: stackTrace);
72+
}
73+
}
74+
75+
runZonedGuarded(
76+
() async {
77+
await SentryFlutter.init(
78+
(options) => options.dsn = sentryDsn,
79+
);
80+
runApp(MyApp());
81+
},
82+
_reportError,
83+
);
2884
}
2985

3086
class MyApp extends StatefulWidget {
@@ -46,10 +102,7 @@ class _MyAppState extends State<MyApp>
46102
token = await secureStorage.read(key: kStreamToken);
47103
}
48104

49-
final client = StreamChatClient(
50-
apiKey ?? kDefaultStreamApiKey,
51-
logLevel: Level.SEVERE,
52-
)..chatPersistenceClient = chatPersistentClient;
105+
final client = buildStreamChatClient(apiKey ?? kStreamApiKey);
53106

54107
if (userId != null && token != null) {
55108
await client.connectUser(

packages/stream_chat_v1/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies:
3333
streaming_shared_preferences: ^2.0.0
3434
lottie: ^1.2.1
3535
collection: ^1.15.0
36+
sentry_flutter: ^6.5.0
3637

3738
dev_dependencies:
3839
flutter_launcher_icons: ^0.9.2

0 commit comments

Comments
 (0)