-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Use case
The local_auth plugin can throw a wide variety of exceptions. All of those exceptions are PlatformExceptions. That means, that you as a developer have to write code to decide based on the PlatformException.code on how to handle that error.
There's a file which contains a lot of those error codes, but the don't contain all possible error code. That can be seen by comparing the error code used in this file for example.
Even the example for the package, doesn't really teach you how to handle all the exception cases and just does generic catch all error handling.
Additionally, adding different exceptions for different error cases, makes it easier to monitor in error monitoring software, because they're not grouped together with other PlatformExceptions.
Proposal
Introduce specific exceptions for all the different error codes.
The following is an example of how I image it, for the isDeviceSupported() method. All the other methods should follow a similar pattern.
/// Returns true if device is capable of checking biometrics or is able to
/// fail over to device credentials.
Future<bool> isDeviceSupported() async {
try {
LocalAuthPlatform.instance.isDeviceSupported();
} on PlatformException catch (exception, stackTrace) {
Error.throwWithStackTrace(_convertException(exception), stackTrace);
}
}
Object _convertException(PlatformException e) {
switch (e.code) {
// Taken from https://github.com/flutter/plugins/blob/e6184f9cca0139ad3a45c212c2189b928b4e690d/packages/local_auth/local_auth/lib/error_codes.dart#L13
case 'NotEnrolled': return NotEnrolledException();
// handle all the other error codes
}
}