Navaron Bracke

refactor iOS impl to use well formatted error codes & messages for FlutterError

@@ -153,20 +153,20 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -153,20 +153,20 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
153 } 153 }
154 } 154 }
155 } catch MobileScannerError.alreadyStarted { 155 } catch MobileScannerError.alreadyStarted {
156 - result(FlutterError(code: "MobileScanner",  
157 - message: "Called start() while already started!", 156 + result(FlutterError(code: MobileScannerErrorCodes.ALREADY_STARTED_ERROR,
  157 + message: MobileScannerErrorCodes.ALREADY_STARTED_ERROR_MESSAGE,
158 details: nil)) 158 details: nil))
159 } catch MobileScannerError.noCamera { 159 } catch MobileScannerError.noCamera {
160 - result(FlutterError(code: "MobileScanner",  
161 - message: "No camera found or failed to open camera!", 160 + result(FlutterError(code: MobileScannerErrorCodes.NO_CAMERA_ERROR,
  161 + message: MobileScannerErrorCodes.NO_CAMERA_ERROR_MESSAGE,
162 details: nil)) 162 details: nil))
163 } catch MobileScannerError.cameraError(let error) { 163 } catch MobileScannerError.cameraError(let error) {
164 - result(FlutterError(code: "MobileScanner",  
165 - message: "Error occured when setting up camera!",  
166 - details: error)) 164 + result(FlutterError(code: MobileScannerErrorCodes.CAMERA_ERROR,
  165 + message: error.localizedDescription,
  166 + details: nil))
167 } catch { 167 } catch {
168 - result(FlutterError(code: "MobileScanner",  
169 - message: "Unknown error occured.", 168 + result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR,
  169 + message: MobileScannerErrorCodes.GENERIC_ERROR_MESSAGE,
170 details: nil)) 170 details: nil))
171 } 171 }
172 } 172 }
@@ -189,25 +189,25 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -189,25 +189,25 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
189 private func setScale(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { 189 private func setScale(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
190 let scale = call.arguments as? CGFloat 190 let scale = call.arguments as? CGFloat
191 if (scale == nil) { 191 if (scale == nil) {
192 - result(FlutterError(code: "MobileScanner",  
193 - message: "You must provide a scale when calling setScale!",  
194 - details: nil)) 192 + result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR,
  193 + message: MobileScannerErrorCodes.INVALID_ZOOM_SCALE_ERROR_MESSAGE,
  194 + details: "The invalid zoom scale was nil."))
195 return 195 return
196 } 196 }
197 do { 197 do {
198 try mobileScanner.setScale(scale!) 198 try mobileScanner.setScale(scale!)
199 result(nil) 199 result(nil)
200 } catch MobileScannerError.zoomWhenStopped { 200 } catch MobileScannerError.zoomWhenStopped {
201 - result(FlutterError(code: "MobileScanner",  
202 - message: "Called setScale() while stopped!", 201 + result(FlutterError(code: MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR,
  202 + message: MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR_MESSAGE,
203 details: nil)) 203 details: nil))
204 } catch MobileScannerError.zoomError(let error) { 204 } catch MobileScannerError.zoomError(let error) {
205 - result(FlutterError(code: "MobileScanner",  
206 - message: "Error while zooming.",  
207 - details: error)) 205 + result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR,
  206 + message: error.localizedDescription,
  207 + details: nil))
208 } catch { 208 } catch {
209 - result(FlutterError(code: "MobileScanner",  
210 - message: "Error while zooming.", 209 + result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR,
  210 + message: MobileScannerErrorCodes.GENERIC_ERROR_MESSAGE,
211 details: nil)) 211 details: nil))
212 } 212 }
213 } 213 }
@@ -218,16 +218,16 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -218,16 +218,16 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
218 try mobileScanner.resetScale() 218 try mobileScanner.resetScale()
219 result(nil) 219 result(nil)
220 } catch MobileScannerError.zoomWhenStopped { 220 } catch MobileScannerError.zoomWhenStopped {
221 - result(FlutterError(code: "MobileScanner",  
222 - message: "Called resetScale() while stopped!", 221 + result(FlutterError(code: MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR,
  222 + message: MobileScannerErrorCodes.SET_SCALE_WHEN_STOPPED_ERROR_MESSAGE,
223 details: nil)) 223 details: nil))
224 } catch MobileScannerError.zoomError(let error) { 224 } catch MobileScannerError.zoomError(let error) {
225 - result(FlutterError(code: "MobileScanner",  
226 - message: "Error while zooming.",  
227 - details: error)) 225 + result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR,
  226 + message: error.localizedDescription,
  227 + details: nil))
228 } catch { 228 } catch {
229 - result(FlutterError(code: "MobileScanner",  
230 - message: "Error while zooming.", 229 + result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR,
  230 + message: MobileScannerErrorCodes.GENERIC_ERROR_MESSAGE,
231 details: nil)) 231 details: nil))
232 } 232 }
233 } 233 }
@@ -269,7 +269,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -269,7 +269,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
269 barcodeScannerOptions: scannerOptions, callback: { barcodes, error in 269 barcodeScannerOptions: scannerOptions, callback: { barcodes, error in
270 if error != nil { 270 if error != nil {
271 DispatchQueue.main.async { 271 DispatchQueue.main.async {
272 - result(FlutterError(code: "MobileScanner", 272 + result(FlutterError(code: MobileScannerErrorCodes.GENERIC_ERROR,
273 message: error?.localizedDescription, 273 message: error?.localizedDescription,
274 details: nil)) 274 details: nil))
275 } 275 }