Skip to content

Commit a100a28

Browse files
fix: use OSBarcodeError class to handle error cases, using a description and code, instead of just sending an error message
1 parent 9d6b4b2 commit a100a28

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

plugin/ios/Plugin/CapacitorBarcodeScannerPlugin.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public class CapacitorBarcodeScannerPlugin: CAPPlugin {
2222
}
2323

2424
guard let manager = self.manager else {
25-
call.reject("Capacitor bridge or viewController is not initialized.")
25+
call.sendError(with: OSBarcodeError.bridgeNotInitialized)
2626
return
2727
}
2828

2929
guard let argumentsData = try? JSONSerialization.data(withJSONObject: call.jsObjectRepresentation),
3030
let scanArguments = try? JSONDecoder().decode(OSBarcodeScanArgumentsModel.self, from: argumentsData) else {
31-
call.reject("Error decoding scan arguments")
31+
call.sendError(with: OSBarcodeError.scanInputArgumentsIssue)
3232
return
3333
}
3434

@@ -37,12 +37,20 @@ public class CapacitorBarcodeScannerPlugin: CAPPlugin {
3737
let scannedBarcode = try await manager.scanBarcode(with: scanArguments.scanInstructions, scanArguments.scanButtonText, scanArguments.cameraDirection, and: scanArguments.scanOrientation)
3838
call.resolve(["ScanResult": scannedBarcode])
3939
} catch OSBARCManagerError.cameraAccessDenied {
40-
call.reject("Camera access denied")
40+
call.sendError(with: OSBarcodeError.cameraAccessDenied)
4141
} catch OSBARCManagerError.scanningCancelled {
42-
call.reject("Scanning cancelled")
42+
call.sendError(with: OSBarcodeError.scanningCancelled)
4343
} catch {
44-
call.reject("An unexpected error occurred: \(error.localizedDescription)")
44+
call.sendError(with: OSBarcodeError.scanningError)
4545
}
4646
}
4747
}
4848
}
49+
50+
extension CAPPluginCall {
51+
52+
func sendError(with error: OSBarcodeError) {
53+
self.reject(error.errorDescription, error.errorCode)
54+
}
55+
56+
}

plugin/ios/Plugin/OSBarcodeError.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ enum OSBarcodeError: Int, CustomNSError, LocalizedError {
1111
case scanningCancelled = 6
1212
case cameraAccessDenied = 7
1313
case scanInputArgumentsIssue = 8
14+
case bridgeNotInitialized = 13
1415

15-
var errorDescription: String? {
16+
var errorDescription: String {
1617
switch self {
1718
case .scanningError: return "Error while trying to scan code."
1819
case .scanningCancelled: return "Couldn’t scan because the process was cancelled."
1920
case .cameraAccessDenied: return "Couldn’t scan because camera access wasn’t provided. Check your camera permissions and try again."
2021
case .scanInputArgumentsIssue: return "Scanning parameters are invalid."
22+
case .bridgeNotInitialized: return "Capacitor bridge or viewController is not initialized."
2123
}
2224
}
25+
26+
var errorCode: String {
27+
return "OS-PLUG-BARC-\(String(format: "%04d", self.rawValue))"
28+
}
2329

2430
var errorDictionary: [String: String] {
2531
[

0 commit comments

Comments
 (0)