I am writing some error handling for when my HealthKit client requests permission to data in Xamarin iOS. I make the request like so:
public HKClient()
{
var HealthKitStore = new HKHealthStore();
HealthKitStore.RequestAuthorizationToShare (dataTypesToWrite, dataTypesToRead, OnHealthPermissionsCompleted);
}
void OnHealthPermissionsCompleted (bool success, NSError error)
{
//Parse error.Domain and error.Code herere
}
In my OnHealthPermissionsCompleted, I want to parse the NSError in order to debug why our request failed. The first thing to do is check error.Domain to make sure it is a HealthKit error and then compare error.Code to the constants in the HKErrorCode enum. The problem is, I cannot find any constant for what should be in error.Domain for HealthKit related errors. The Apple documentation says there should be a constant called "HKErrorDomain" for me to compare to, however it is not there in Xamarin.
https://developer.apple.com/library/prerelease/watchos/documentation/HealthKit/Reference/HealthKit_Constants/index.html#//apple_ref/doc/constant_group/Health_Kit_Error_Domain
https://developer.xamarin.com/api/namespace/HealthKit/
If I force an error and then check it in the debugger, I do see that error.Domain = "com.apple.healthkit". I could just compare to that string,
void OnHealthPermissionsCompleted (bool success, NSError error)
{
if(!success && error.Domain == "com.apple.healthkit")
{
//continue parsing...
}
}
but putting magic strings in these kinds of things makes me feel icky, especially when I know a constant for this exists in native iOS. Am I missing something here or is this my only option?
There is now a bug filed on Xamarin's Bugzilla to surface this constant:
https://bugzilla.xamarin.com/show_bug.cgi?id=34140
Related
I used this method very much in Swift 1.2: NSURLConnection.sendSynchronousRequest(:_:_:_) but this is apparently deprecated in iOS9. It still works however but now it uses the new Swift 2.0 Error Handling and I don't know how I will get the error message if it fails, ex. if time runs out.
I know I have to put it into a do-catch and then say try before the metho but I dont know how to catch the error message.
do {
let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: nil)
return data
}
catch _ {
return nil
}
Before I used NSError and then its description property, but now I have no clue.
Use automatic error variable, and you can cast it to NSError if you wish:
catch {
let nsError = error as NSError
print(nsError.localizedDescription)
}
You can now throw any object inheriting ErrorType, and provide custom handling in the catch sentence. You can also cast the error to NSError to access localizedDescription for handling third party errors.
Casting an enum ErrorType will produce a NSError with domain equal to the enum name, code equal to the enum value and an auto-generated localizedDescription with the following format:
The operation couldn’t be completed. (DOMAIN error CODE.)
For example, the following code:
enum AwfulError: ErrorType {
case Bad
case Worse
case Terrible
}
func throwingFunction() throws {
throw AwfulError.Worse
}
do {
try throwingFunction()
}
catch AwfulError.Bad {
print("Bad error")
}
catch let error as NSError {
print(error.localizedDescription)
}
Will print
The operation couldn’t be completed. (AwfulError error 1.)
Despite the question title specifying Swift 2, this answer is for Swift 3.
As #redent84 points out, since Swift 2 an Error object may be a home-made one. Here's a method I wrote to analyze and print the default error object available in a "catch" statement that doesn't specify any specific error type:
// Method to print an unknown Error type object to the system output.
static func printCaughtError(_ unknownError : Error) {
let objectDescription = String(describing: unknownError)
let localizedDescription = unknownError.localizedDescription
if localizedDescription != "" {
if localizedDescription.contains(objectDescription) {
print(localizedDescription)
return
}
if !objectDescription.contains(localizedDescription) {
print(objectDescription + ": " + localizedDescription)
return
}
}
print(objectDescription)
}
Then you can call it like this:
catch {
printCaughtError(error)
}
How to get the error message that is inside userInfo:
let errorMessage = (error as NSError).userInfo["message"] as? String
I need some help to learn how to properly handle errors when fetching records via CloudKit. Currently I have an app that saves numerous records in the cloud, and will load them at launch. I have been referencing the records using a CKReference, and anytime I save the reference I use the CKReferenceAction.DeleteSelf option. A problem I've encountered periodically is that when a referenced record is deleted, sometimes there can be a significant amount of time before the reference deletes itself. This has caused me to occasionally come across the situation where my app has fetched a CKReference for a record that no longer exists. I'm able to manually find out when this happens just by inserting print(error!) in my error handler. What I would like to know is how I can add some code to detect this specific error i.e. if error.localizedDescription == ??? {.
Here is the basic code I'm using for the fetch:
let fetch = CKFetchRecordsOperation(recordIDs: recordIDs)
fetch.perRecordCompletionBlock = { (record:CKRecord?, recordID:CKRecordID?, error: NSError?) in
if error != nil {
// Error Line A (See below)
print("ERROR! : \(error!.localizedDescription)")
// Error Line B (See below)
print("ERROR: \(error!)")
}
else if let record = record {
// Record was found
}
}
if let database = self.privateDatabase {
fetch.database = database
fetch.start()
}
And then when it tries to fetch the non-existent record, here is the error message that prints out in the compiler window:
a) ERROR! : Error fetching record <CKRecordID: 0x10025b290; dbbda7c3-adcc-4271-848f-6702160ea34f:(_defaultZone:__defaultOwner__)> from server: Record not found
b) ERROR: <CKError 0x125e82820: "Unknown Item" (11/2003); server message = "Record not found"; uuid = (removed); container ID = "(removed)">
Above in error line B, where it says CKError 0x125e82820:, can I use this to create an if statement to check for this specific error type? I really could use any help finding a way to resolve this issue properly when it happens. I have set up some loading structure for my app, and when it thinks there is a record it needs to find, but can't, it screws up my loading process. I would really appreciate any help I can get, I assume it's an easy solution, but apparently not one I've been able to find. Thank you!
UPDATE -
Thanks to #AaronBrager, I was able to find the correct solution. You can verify the error code to match it to any specific error, and the domain to make sure it's a CKError. Here is the solution that works for me:
let fetch = CKFetchRecordsOperation(recordIDs: recordIDs)
fetch.perRecordCompletionBlock = { (record:CKRecord?, recordID:CKRecordID?, error: NSError?) in
if error != nil {
if error!.code == CKErrorCode.UnknownItem.rawValue && error!.domain == CKErrorDomain {
// This works great!
}
}
else if let record = record {
// Record was found
}
}
if let database = self.publicDatabase {
fetch.database = database
fetch.start()
}
You should be able to uniquely identify an error's cause by inspecting its domain and code variables. Same domain and code, same problem. And unlike localizedDescription, it won't change between users.
I'm trying to figure out a way to handle the authorization statuses for Motion activity
Here's what I came up with so far :
manager = CMMotionActivityManager()
manager.queryActivityStartingFromDate(now, toDate: now, toQueue: NSOperationQueue.mainQueue(),
withHandler: { (activities: [CMMotionActivity]?, error: NSError?) -> Void in
if(error != nil){
if(error!.code != Int(CMErrorMotionActivityNotAuthorized.rawValue)){
print("CMErrorMotionActivityNotAuthorized")
}else if(error!.code != Int(CMErrorMotionActivityNotEntitled.rawValue)){
print("CMErrorMotionActivityNotEntitled")
}else if(error!.code != Int(CMErrorMotionActivityNotAvailable.rawValue)){
print("CMErrorMotionActivityNotAvailable")
}
}
})
One problem though :
When I deny the app permission to motion activity (via settings), I get CMErrorMotionActivityNotEntitled
(I believe I should be getting CMErrorMotionActivityNotAuthorized instead)
Any ideas why ? or at least what's the proper way of doing this ?
Perhaps you are getting CMErrorMotionActivityNotAuthorized. You'll never know, with your code, because your code does not ask what code you are getting. It asks what code you are not getting:
if(error!.code != Int(CMErrorMotionActivityNotAuthorized.rawValue)){
print("CMErrorMotionActivityNotAuthorized")
}else if(error!.code != Int(CMErrorMotionActivityNotEntitled.rawValue)){
print("CMErrorMotionActivityNotEntitled")
}else if(error!.code != Int(CMErrorMotionActivityNotAvailable.rawValue)){
print("CMErrorMotionActivityNotAvailable")
}
The != operator means is not. So you are doing a series of checks about what the code is not. It's hard to see how you can get any useful information by asking that question. It might make more sense to ask what the code is, which would involve using the == operator.
I used this method very much in Swift 1.2: NSURLConnection.sendSynchronousRequest(:_:_:_) but this is apparently deprecated in iOS9. It still works however but now it uses the new Swift 2.0 Error Handling and I don't know how I will get the error message if it fails, ex. if time runs out.
I know I have to put it into a do-catch and then say try before the metho but I dont know how to catch the error message.
do {
let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: nil)
return data
}
catch _ {
return nil
}
Before I used NSError and then its description property, but now I have no clue.
Use automatic error variable, and you can cast it to NSError if you wish:
catch {
let nsError = error as NSError
print(nsError.localizedDescription)
}
You can now throw any object inheriting ErrorType, and provide custom handling in the catch sentence. You can also cast the error to NSError to access localizedDescription for handling third party errors.
Casting an enum ErrorType will produce a NSError with domain equal to the enum name, code equal to the enum value and an auto-generated localizedDescription with the following format:
The operation couldn’t be completed. (DOMAIN error CODE.)
For example, the following code:
enum AwfulError: ErrorType {
case Bad
case Worse
case Terrible
}
func throwingFunction() throws {
throw AwfulError.Worse
}
do {
try throwingFunction()
}
catch AwfulError.Bad {
print("Bad error")
}
catch let error as NSError {
print(error.localizedDescription)
}
Will print
The operation couldn’t be completed. (AwfulError error 1.)
Despite the question title specifying Swift 2, this answer is for Swift 3.
As #redent84 points out, since Swift 2 an Error object may be a home-made one. Here's a method I wrote to analyze and print the default error object available in a "catch" statement that doesn't specify any specific error type:
// Method to print an unknown Error type object to the system output.
static func printCaughtError(_ unknownError : Error) {
let objectDescription = String(describing: unknownError)
let localizedDescription = unknownError.localizedDescription
if localizedDescription != "" {
if localizedDescription.contains(objectDescription) {
print(localizedDescription)
return
}
if !objectDescription.contains(localizedDescription) {
print(objectDescription + ": " + localizedDescription)
return
}
}
print(objectDescription)
}
Then you can call it like this:
catch {
printCaughtError(error)
}
How to get the error message that is inside userInfo:
let errorMessage = (error as NSError).userInfo["message"] as? String
Can any one give an example of how use getFirstObjectInBackground and get the first Object returned into a PFObject in Swift?
let weatherObject:PFObject = query.getFirstObjectInBackground() as! PFObject
Gives a warning Cast from 'BFTask' to unrelated type 'PFObject' always fails is the warning / error I get in xcode.
TIA.
That variety of get returns a BFTask from the bolts framework, which is kind of like a JS promise for the iOS sdks. Your code casts the return from getFirstObjectInBackground to a PFObject, which it isn't, as if the method synchronously returns the object you're trying to fetch, which it doesn't.
The fix is either to treat the BFTask return value like a BFTask and assign it a completion block, or -- easier I think -- use the block variety of the get method (e.g. from the iOS guide):
query.getFirstObjectInBackgroundWithBlock {
(object: PFObject?, error: NSError?) -> Void in
if error != nil || object == nil {
println("The getFirstObject request failed.")
} else {
// The find succeeded.
println("Successfully retrieved the object.")
}
}
If your situation really calls for handling the bolts framework directly, a decent doc for it can be found on its github page.