Skip to content

Instantly share code, notes, and snippets.

@motia
Created October 28, 2019 15:03
Show Gist options
  • Save motia/99df2545de5e28a6ce0a03c131cef971 to your computer and use it in GitHub Desktop.
Save motia/99df2545de5e28a6ce0a03c131cef971 to your computer and use it in GitHub Desktop.
Flutter Sentry Setup
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:sentry/sentry.dart';
class ErrorHandler {
static ErrorHandler _instance;
SentryClient _sentry;
final bool isInDebugMode;
ErrorHandler(String dsn, this.isInDebugMode) {
_sentry = SentryClient(dsn: dsn);
}
static ErrorHandler get instance => _instance;
runApp(Function function) {
runZoned<Future<void>>(() async {
function();
}, onError: (error, stackTrace) {
_reportError(error, stackTrace);
});
}
factory ErrorHandler.init(String dsn, bool isInDebugMode) {
if (ErrorHandler._instance != null) {
return ErrorHandler._instance;
}
FlutterError.onError = (FlutterErrorDetails details) {
if (isInDebugMode) {
// In development mode, simply print to console.
FlutterError.dumpErrorToConsole(details);
} else {
// In production mode, report to the application zone to report to
// Sentry.
Zone.current.handleUncaughtError(details.exception, details.stack);
}
};
return _instance = ErrorHandler(dsn, isInDebugMode);
}
Future<void> _reportError(dynamic error, dynamic stackTrace) async {
// Print the exception to the console.
print('Caught error: $error');
if (isInDebugMode) {
// Print the full stacktrace in debug mode.
print(stackTrace);
return;
} else {
// Send the Exception and Stacktrace to Sentry in Production mode.
_instance._sentry.captureException(
exception: error,
stackTrace: stackTrace,
);
}
}
}
ErrorHandler.init(_SENTRY_DSN, false);
final Widget app = await buildApp(_RELEASE_APP_CONFIG);
ErrorHandler.instance.runApp(() {
runApp(app);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment