Skip to content

Commit 4c8e12c

Browse files
committed
Refactor error message
1 parent 06f6cea commit 4c8e12c

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

android/src/main/java/com/rnbiometrics/ReactNativeBiometrics.java

+23-26
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public void isSensorAvailable(final ReadableMap params, final Promise promise) {
6868
resultMap.putString("biometryType", "Fingerprint");
6969
}
7070

71-
7271
promise.resolve(resultMap);
7372
} else if (allowDeviceCredentials) {
7473
int canAuthenticateCredentials = biometricManager.canAuthenticate(getAllowedAuthenticators(true));
@@ -77,40 +76,19 @@ public void isSensorAvailable(final ReadableMap params, final Promise promise) {
7776
WritableMap resultMap = new WritableNativeMap();
7877
resultMap.putBoolean("available", true);
7978
resultMap.putString("biometryType", "Credentials");
79+
8080
promise.resolve(resultMap);
8181
} else {
8282
WritableMap resultMap = new WritableNativeMap();
8383
resultMap.putBoolean("available", false);
84-
85-
switch (canAuthenticateCredentials) {
86-
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
87-
resultMap.putString("error", "BIOMETRIC_ERROR_NO_HARDWARE");
88-
break;
89-
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
90-
resultMap.putString("error", "BIOMETRIC_ERROR_HW_UNAVAILABLE");
91-
break;
92-
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
93-
resultMap.putString("error", "BIOMETRIC_ERROR_NONE_ENROLLED");
94-
break;
95-
}
84+
resultMap.putString("error", parseError(canAuthenticateCredentials));
9685

9786
promise.resolve(resultMap);
9887
}
9988
} else {
10089
WritableMap resultMap = new WritableNativeMap();
10190
resultMap.putBoolean("available", false);
102-
103-
switch (canAuthenticate) {
104-
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
105-
resultMap.putString("error", "BIOMETRIC_ERROR_NO_HARDWARE");
106-
break;
107-
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
108-
resultMap.putString("error", "BIOMETRIC_ERROR_HW_UNAVAILABLE");
109-
break;
110-
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
111-
resultMap.putString("error", "BIOMETRIC_ERROR_NONE_ENROLLED");
112-
break;
113-
}
91+
resultMap.putString("error", parseError(canAuthenticate));
11492

11593
promise.resolve(resultMap);
11694
}
@@ -121,10 +99,29 @@ public void isSensorAvailable(final ReadableMap params, final Promise promise) {
12199
promise.resolve(resultMap);
122100
}
123101
} catch (Exception e) {
124-
promise.reject("Error detecting biometrics availability: " + e.getMessage(), "Error detecting biometrics availability: " + e.getMessage());
102+
promise.reject("Error detecting biometrics availability: " + e.getMessage(),
103+
"Error detecting biometrics availability: " + e.getMessage());
125104
}
126105
}
127106

107+
private String parseError(final int authenticationResult) {
108+
String message = "";
109+
110+
switch (authenticationResult) {
111+
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
112+
message = "BIOMETRIC_ERROR_NO_HARDWARE";
113+
break;
114+
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
115+
message = "BIOMETRIC_ERROR_HW_UNAVAILABLE";
116+
break;
117+
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
118+
message = "BIOMETRIC_ERROR_NONE_ENROLLED";
119+
break;
120+
}
121+
122+
return message;
123+
}
124+
128125
@ReactMethod
129126
public void createKeys(final ReadableMap params, Promise promise) {
130127
try {

0 commit comments

Comments
 (0)