nubank / nuvigator

A powerful routing abstraction over Flutter Navigator, with nested Navigator and Deeplinks
Apache License 2.0
277 stars 33 forks source link

Support errors and exceptions #75

Closed felipefpx closed 3 years ago

felipefpx commented 3 years ago

Currently, the router's onError function is only able to receive classes that extends Error and crashing when the init throws any class that extends Exception:

nu_router.dart

...
Widget onError(Error error, NuRouterController controller) => null;
...

example_router.dart

...
@override
  bool get awaitForInit => true;

@override
  Future<void> init(BuildContext context) async {
    throw Exception();
  }
...

Output:

The following _TypeError was thrown running a test:
type '_Exception' is not a subtype of type 'Error'

When the exception was thrown, this was the stack:
#0      _NuRouterLoaderState._initModule (package:nuvigator/src/next/v1/nu_router.dart:399:33)
<asynchronous suspension>
...

Based on the approach used in bloc library, this PR changes the onError function to receive an Object as error, so we can receive both Errors and Exceptions. It's important to say that in dart we can throw any class.

leoiacovini commented 3 years ago

Thanks! Do you mind bumping the package version in the pubspec.yaml and adding a changelog entry? I think this is an API breaking change, so maybe it's better to bump a major