I've recently began trying to put more error handling in to my code so I thought I'd post a use-case to check I'm on the right path. I feel like this is going to make my functions much larger and more indented/nested than before. Is this the right way to go about things or should I be utilising try?
instead? Where do people generally put these Error
enum
definitions?
fileprivate enum LocationError: Error {
case noAuthorization
case noBeaconSupport
case rangingUnavailable
}
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var distanceReading: UILabel!
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
view.backgroundColor = UIColor.gray
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
do {
guard status == .authorizedWhenInUse else {
throw LocationError.noAuthorization
}
guard CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) else {
throw LocationError.noBeaconSupport
}
guard CLLocationManager.isRangingAvailable() else {
throw LocationError.rangingUnavailable
}
startScanning()
}
catch LocationError.noAuthorization {
print("User has not authorized us to use location")
}
catch LocationError.noBeaconSupport {
print("User's device does not support Beacons")
}
catch LocationError.rangingUnavailable {
print("User's device does not support ranging")
}
catch {
fatalError()
}
}