Skip to content

Commit 33608e7

Browse files
committed
avoid magic strings when converting PlatformException.code
1 parent 5f959b6 commit 33608e7

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

lib/src/enums/mobile_scanner_error_code.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/services.dart';
12
import 'package:mobile_scanner/src/mobile_scanner_controller.dart';
23

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

2627
/// Scanning is unsupported on the current device.
27-
unsupported,
28+
unsupported;
29+
30+
/// Convert the given [PlatformException.code] to a [MobileScannerErrorCode].
31+
factory MobileScannerErrorCode.fromPlatformException(
32+
PlatformException exception,
33+
) {
34+
// The following error code mapping should be kept in sync with their native counterparts.
35+
// These are located in `MobileScannerErrorCodes.kt` and `MobileScannerErrorCodes.swift`.
36+
return switch (exception.code) {
37+
// In case the scanner was already started, report the right error code.
38+
// If the scanner is already starting,
39+
// this error code is a signal to the controller to just ignore the attempt.
40+
'MOBILE_SCANNER_ALREADY_STARTED_ERROR' =>
41+
MobileScannerErrorCode.controllerAlreadyInitialized,
42+
// In case no cameras are available, using the scanner is not supported.
43+
'MOBILE_SCANNER_NO_CAMERA_ERROR' => MobileScannerErrorCode.unsupported,
44+
'MOBILE_SCANNER_CAMERA_PERMISSION_DENIED' =>
45+
MobileScannerErrorCode.permissionDenied,
46+
_ => MobileScannerErrorCode.genericError,
47+
};
48+
}
2849
}

lib/src/method_channel/mobile_scanner_method_channel.dart

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,37 +224,14 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
224224

225225
Map<String, Object?>? startResult;
226226

227-
MobileScannerErrorCode errorCode = MobileScannerErrorCode.genericError;
228-
229227
try {
230228
startResult = await methodChannel.invokeMapMethod<String, Object?>(
231229
'start',
232230
startOptions.toMap(),
233231
);
234232
} on PlatformException catch (error) {
235-
// Map the error code to a MobileScannerErrorCode.
236-
// The error code strings should be kept in sync with their native counterparts.
237-
// Any error code that is not mapped will be treated as a generic error.
238-
switch (error.code) {
239-
// In case the scanner was already started, report the right error code.
240-
// If the scanner is already starting,
241-
// this error code is a signal to the controller to just ignore the attempt.
242-
case 'MOBILE_SCANNER_ALREADY_STARTED_ERROR':
243-
errorCode = MobileScannerErrorCode.controllerAlreadyInitialized;
244-
// In case no cameras are available, using the scanner is not supported.
245-
case 'MOBILE_SCANNER_NO_CAMERA_ERROR':
246-
errorCode = MobileScannerErrorCode.unsupported;
247-
// This error code should have already been handled
248-
// by the _requestCameraPermission method above,
249-
// but just in case, also handle it here.
250-
case 'MOBILE_SCANNER_CAMERA_PERMISSION_DENIED':
251-
errorCode = MobileScannerErrorCode.permissionDenied;
252-
default:
253-
break;
254-
}
255-
256233
throw MobileScannerException(
257-
errorCode: errorCode,
234+
errorCode: MobileScannerErrorCode.fromPlatformException(error),
258235
errorDetails: MobileScannerErrorDetails(
259236
code: error.code,
260237
details: error.details as Object?,

0 commit comments

Comments
 (0)