Skip to content

[local_auth] Improve exception handling of local_auth plugin #113687

@ueman

Description

@ueman

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
  }
} 

Metadata

Metadata

Labels

P3Issues that are less important to the Flutter projectc: proposalA detailed proposal for a change to Flutterp: local_authPlugin for local authentificationp: requires breaking changeA change that should be batched into the next breaking change to this packagepackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions