Powerful Error Handling for iOS and OSX apps
RMErrors describes instances NSError
and returns friendly messages following rules set in a simple property list.
The following example RMErrors.plist will setup RMErrors to:
- NSError with domain
NSURLErrorDomain
and code between-1003
and-1009
will be described with friendly messagethe connection failed because the device is not connected to the internet
. - NSError with domain
NSURLErrorDomain
and code-1001
will be described with friendly messageThe connection timed out, please make sure you're connected to the internet and try again
. - NSError with domain
NSURLErrorDomain
and other codes not included by any of the previous rules will be described with friendly messageNetwork issue
- NSError with domain other than
NSURLErrorDomain
will be described with friendly messageOopss, something went wrong
.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>(default)</key>
<dict>
<key>code</key>
<integer>-1</integer>
<key>friendlyMessage</key>
<string>Oopss, something went wrong</string>
</dict>
<key>domains</key>
<dict>
<key>NSURLErrorDomain</key>
<dict>
<key>(default)</key>
<dict>
<key>transient</key>
<true/>
<key>friendlyMessage</key>
<string>Network issue</string>
</dict>
<key>codes</key>
<dict>
<key>-1009..-1003</key>
<dict>
<key>transient</key>
<true/>
<key>friendlyMessage</key>
<string>the connection failed because the device is not connected to the internet</string>
</dict>
<key>-1001</key>
<dict>
<key>transient</key>
<true/>
<key>friendlyMessage</key>
<string>The connection timed out, please make sure you're connected to the internet and try again</string>
</dict>
</dict>
</dict>
</dict>
</dict>
</plist>
pod 'RMErrors'
$ pod install
Create a new property list file in your project and make sure it's included in all the Xcode targets you intent to use from.
RMErrors *errors = [[RMErrors alloc] init];
[errors loadPropertyList]; //loads from RMErrors.plist in main bundle.
RMErrorDescription *description = [errors describe:[NSError errorWithDomain:@"com.foo.bar.oopsie" code:500 userInfo:nil]];
NSLog(@"Friendly Message: %@", description.friendlyMessage);
Outputs:
"Oopss, something went wrong"
$ cd Tests && pod install
$ open RMErrors.xcworkspace