flutter_reactive_ble
flutter_reactive_ble copied to clipboard
Make GenericFailure implement Exception
Is your feature request related to a problem? (please describe)
In some cases Result<GenericFailure>
is dematerialised from a Result, instead of being thrown directly. Because GenericFailure does not implement exception, it gets turned into a string by the Exception constructor, and all type information is lost. We are not able to read the error code anymore. This is the same issue reported by #674
The throw happens here: https://github.com/PhilipsHue/flutter_reactive_ble/blob/e9b16c4ec4f3499b279794c080965c12f480f12e/packages/reactive_ble_platform_interface/lib/src/model/result.dart#L20
Describe the solution you'd like
Change the GenericFailure class to implement Exception. This requires a single line change, since this interface only requires toString
which is already implemented.
class GenericFailure<T> implements Exception {
Describe alternatives you've considered
I'm reading the string right now, but this has no type safety and typos in the string could cause the code to fail. Also the library right now is inconsistent, some functions throw an Exception with a stringified GenericFailure and others throw a GenericFailure as expected.
Additional context
This could be considered a breaking change. User code that was trying to catch the GenericFailure used to be dead code in cases where the GenericFailure is dematerialised. With this change, this code would start catching errors. Any existing code that was catching these as Exception and reading the string would still work OK, since GenericFailure would be a subclass of Exception.
I second this, as I've had to resort to such methods as well:
test: (e) => !e.toString().startsWith("Exception: GenericFailure")