getsentry / sentry-dart

Sentry SDK for Dart and Flutter
https://sentry.io/for/flutter/
MIT License
754 stars 237 forks source link

[sentry_flutter] Sentry will automatically uploads 'Flutter Error', Uncontrolled #216

Closed chvin closed 3 years ago

chvin commented 3 years ago

On sentry_flutter: ^4.0.0-alpha.2

After initialize sentry, sentry will automatically uploads 'Flutter Error', Uncontrolled. Full code:

// main.dart
import 'dart:async';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:flutter/material.dart';

void main() {
  // release: true, debug|profile: false
  // As suggested by: https://twitter.com/FlutterDev/status/1048278525432791041
  const bool isProduction = const bool.fromEnvironment('dart.vm.product');

  // handle Flutter error
  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<void>>(() 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,
    );
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Exception Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
      backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('Error test'),
        ),
        // https://flutter.dev/docs/testing/common-errors#a-renderflex-overflowed
        body: Row(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("Title", style: Theme.of(context).textTheme.headline4),
                Text(
                "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed"
                " do eiusmod tempor incididunt ut labore et dolore magna "
                "aliqua. Ut enim ad minim veniam, quis nostrud "
                "exercitation ullamco laboris nisi ut aliquip ex ea "
                "commodo consequat."),
              ],
            ),
          ],
        )
      )
    );
  }
}

The above code triggered a Flutter error(layout error). In debug mode running, Sentry still upload error.

marandaneto commented 3 years ago

@chvin that's true, you have 2 options, either you don't init. Sentry on debug mode or you can use the beforeSend hook to filter the events which are on a debug environment.

For example: https://docs.sentry.io/platforms/flutter/data-management/sensitive-data/#scrubbing-data

if an event has the environment field set debug, just return null.