Skip to content

Commit

Permalink
avoid magic strings when converting PlatformException.code
Browse files Browse the repository at this point in the history
  • Loading branch information
navaronbracke committed Sep 18, 2024
1 parent 5f959b6 commit 33608e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
23 changes: 22 additions & 1 deletion lib/src/enums/mobile_scanner_error_code.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/services.dart';
import 'package:mobile_scanner/src/mobile_scanner_controller.dart';

/// This enum defines the different error codes for the mobile scanner.
Expand All @@ -24,5 +25,25 @@ enum MobileScannerErrorCode {
permissionDenied,

/// Scanning is unsupported on the current device.
unsupported,
unsupported;

/// Convert the given [PlatformException.code] to a [MobileScannerErrorCode].
factory MobileScannerErrorCode.fromPlatformException(
PlatformException exception,
) {
// The following error code mapping should be kept in sync with their native counterparts.
// These are located in `MobileScannerErrorCodes.kt` and `MobileScannerErrorCodes.swift`.
return switch (exception.code) {
// In case the scanner was already started, report the right error code.
// If the scanner is already starting,
// this error code is a signal to the controller to just ignore the attempt.
'MOBILE_SCANNER_ALREADY_STARTED_ERROR' =>
MobileScannerErrorCode.controllerAlreadyInitialized,
// In case no cameras are available, using the scanner is not supported.
'MOBILE_SCANNER_NO_CAMERA_ERROR' => MobileScannerErrorCode.unsupported,
'MOBILE_SCANNER_CAMERA_PERMISSION_DENIED' =>
MobileScannerErrorCode.permissionDenied,
_ => MobileScannerErrorCode.genericError,
};
}
}
25 changes: 1 addition & 24 deletions lib/src/method_channel/mobile_scanner_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,37 +224,14 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {

Map<String, Object?>? startResult;

MobileScannerErrorCode errorCode = MobileScannerErrorCode.genericError;

try {
startResult = await methodChannel.invokeMapMethod<String, Object?>(
'start',
startOptions.toMap(),
);
} on PlatformException catch (error) {
// Map the error code to a MobileScannerErrorCode.
// The error code strings should be kept in sync with their native counterparts.
// Any error code that is not mapped will be treated as a generic error.
switch (error.code) {
// In case the scanner was already started, report the right error code.
// If the scanner is already starting,
// this error code is a signal to the controller to just ignore the attempt.
case 'MOBILE_SCANNER_ALREADY_STARTED_ERROR':
errorCode = MobileScannerErrorCode.controllerAlreadyInitialized;
// In case no cameras are available, using the scanner is not supported.
case 'MOBILE_SCANNER_NO_CAMERA_ERROR':
errorCode = MobileScannerErrorCode.unsupported;
// This error code should have already been handled
// by the _requestCameraPermission method above,
// but just in case, also handle it here.
case 'MOBILE_SCANNER_CAMERA_PERMISSION_DENIED':
errorCode = MobileScannerErrorCode.permissionDenied;
default:
break;
}

throw MobileScannerException(
errorCode: errorCode,
errorCode: MobileScannerErrorCode.fromPlatformException(error),
errorDetails: MobileScannerErrorDetails(
code: error.code,
details: error.details as Object?,
Expand Down

0 comments on commit 33608e7

Please sign in to comment.