Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic reporting of errors? How? #474

Closed
sgehrman opened this issue May 25, 2021 · 2 comments
Closed

Automatic reporting of errors? How? #474

sgehrman opened this issue May 25, 2021 · 2 comments

Comments

@sgehrman
Copy link

Just started using this and the documentation is confusing.

I assume it would automatically work, but it seems I have to put Sentry.captureException in every catch()? All over my code?

I see in the docs it says "Sentry's Flutter SDK enables automatic reporting of errors, messages, and exceptions"

Is it automatic or not? If I remove the Sentry.captureException, nothing happens, so it doesn't seem automatic to me.

I saw this in another issue. Do I need runZonedGuarded for it to work?

FlutterError.onError = (FlutterErrorDetails details,) async {
if (!isProduction) {
FlutterError.dumpErrorToConsole(details);
return;
}
// turn to Zone
Zone.current.handleUncaughtError(details.exception, details.stack);
};

// Handle dart error
runZonedGuarded<Future>(() async {
SentryFlutter.init((options) => options.dsn = 'xxx',
() {
runApp(MyApp());
},
);
}, (Object error, StackTrace stackTrace) async {
if (!isProduction) {
print(error);
print(stackTrace);
return;
}
// Only upload error on release mode
Sentry.captureException(
error,
stackTrace: stackTrace,
);
});

@ueman
Copy link
Collaborator

ueman commented May 26, 2021

Sentry automatically captures uncaught exceptions.
Exceptions you catch urself aren't usually needed to be reported because you're handling themself and thus aren't really bad. Though there's nothing stopping from reporting them, too. If you're catching them yourself you need to use Sentry.captureException.

You don't need to add a runZoneGuarded. SentryFlutter does this for you if you're using the appRunner callback like in your example.
Also SentryFlutter automatically listens to FlutterError.onError, so you don't need that to do it yourself.

A minimal example with automatic capturing for runZoneGuarded and FlutterError.onError looks like this:

import 'package:flutter/widgets.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

Future<void> main() async {
  await SentryFlutter.init(
    (options) {
      options.dsn = 'https://example@sentry.io/add-your-dsn-here';
    },
    // Init your App.
    appRunner: () => runApp(MyApp()),
  );
}

In order to disable reporting to Sentry you can set the DSN to an empty string.

I hope that clears up your questions.

@ueman ueman added the question label May 26, 2021
@marandaneto
Copy link
Contributor

Also, https://docs.sentry.io/platforms/flutter/usage/#tips-for-catching-errors mention about not needing runZonedGuarded nor FlutterError.onError

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants