CFStream: User Friendly Errors

I need to connect to a remote host and exchange data with some binary protocol. I am contacting:

CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"whatever.host.com", port, &readStream, &writeStream); 

Everything is fine except for error handling. I

  • handling NSStreamEventErrorOccurred in my stream delete
  • receiving an error using theError = [stream streamError];
  • trying to get handy text with text [theError localizedDescription] .

And here's the problem : instead of a nice error message like "Host not found", I get "Operation could not be completed. (NSUnknownErrorDomain error 8.)"
Or, when the device is in airplane mode, I get "The operation couldn't be completed. (kCFErrorDomainCFNetwork error 2.)"

Question : how to get a socket connection, where can I get the correct error messages? When I used NSURLConnection (in another application), the error messages were nice and friendly, so I would like to get something like this.

+4
source share
3 answers

Apple did not provide proper descriptions for the error conditions in iOS. This is not an error in the code or theirs - the lines are simply missing on iOS devices.

To provide more interesting error information, you will need to check the domain and code of the CFError and NSError objects and return something. A list of error descriptions from kCFErrorDomainCFNetwork is available here on the Apple website. Code like this will help - name it when building the error presentation interface.

 NSString *GetUsefulErrorDescription(NSError *e) { NSString *codeString = [NSString stringWithFormat: @"%li", (long)[e code]]; NSString *localized = NSLocalizedStringInTable(codeString, [e domain], nil); if (!localized || [localized isEqual: codeString]) return [e localizedDescription]; else return localized; } 

It looks in a file in your application bundle called "kCFErrorDomainCFNetwork.strings" for a string such as "100" (for kCFSOCKSErrorUnknownClientVersion, the value of which you can specify as "the SOCKS server denied access because it does not support connections with the requested version of SOCKS." ) If such an error description is not available, instead, the string returned by NSError , which will be "The operation cannot be completed. [...]"

+2
source

I believe that the provision of localized error descriptions should take place at the structure level. In other words, the developers of the Foundation structure provided more useful messages, and people associated with localization translated them, etc. Judging by your description, the CoreFoundation level has not received so much attention in this regard.

Are there Foundation Foundation APIs you could use here?

+1
source

Don't you find it easier to just catch the error, check its type and submit your own error message? I can do it much easier than rewriting code to make nice messages. Bonus: this way you have full control and can add longer descriptions if you want ...

-1
source

All Articles