rollbar-flutter icon indicating copy to clipboard operation
rollbar-flutter copied to clipboard

RollbarFlutter.run is limiting (and probably not working as intended)

Open rfuerst87 opened this issue 1 year ago • 17 comments

The current design of RollbarFlutter makes it impossible to integrate it with custom error handlers. Especially FlutterRollbar.run is very limiting. IMHO it has several issues:

  1. RollbarFlutter overwrites FlutterError.onError without any warnings. Having a custom onError function is not compatible with RollbarFlutter.
  2. There is no way to just initialize Rollbar without running an app. This basically makes RollbarFlutter incompatible with any other error handler.
  3. RollbarFlutter is creating it's own guarded zone without the possibility to bubble up errors. FlutterRollbar swallows all the errors without giving other error handlers a chance to catch an error.
  4. WidgetsFlutterBinding.ensureInitialized() is called in the wrong place. When initializing a guarded zone WidgetsFlutterBinding.ensureInitialized() must be called within that zone. Calling it outside (as it is done in run) causes the guarded zone to not catch errors. At this point I think catching async errors does not properly work. Said that, with the current design one has to ensure WidgetsFlutterBinding.ensureInitialized() is not called manually before running RollbarFlutter.run.

In my eyes the design of RollbarFlutter.run is flawed. While the intention to make it as easy to integrate as possible is good, but it also bears the downside of inflexibility. Instead RollbarFlutter should expose an API to initialize Rollbar and let the user decide how to implement. Something like this could work for example:

void main() async {
 
  runZonedGuarded(() async {
    // let the user be responsible for the guarded zone to 
    // properly run WidgetsFlutterBinding.ensureInitialized()
    WidgetsFlutterBinding.ensureInitialized();

    // Initialize rollbar
    await RollbarFlutter.initialize();
    
    // Give the users a chance to implement their own onError
    FlutterError.onError = RollbarFlutter.onError;

    runApp(MyApp());
  }, (exception, stackTrace) {
    // Also give users a chance to do anything else with async errors
    RollbarFlutter.error(exception, stackTrace));
  }
}

For convenience one could provide simple helper methods in RollbarFlutter to make this an one-liner again. I'm not sure whether this is needed to be honest.

Please let me know what you think. I'd offer my support here to come up with an improved version. We are currently forced to work with a fork which I'd get rid of better sooner than later.

rfuerst87 avatar Jan 10 '23 15:01 rfuerst87