It is good practice to extend NSError

Sorry to ask this question. I know that in java we extend the exception class for custom exceptions. But I do not see any scenarios for this in lens c.

So my question is: is it good to propagate NSError and introduce custom errors? If so, when should we extend the NSError class. I also checked the documentation. But I do not see wonderful notes for NSError.

+7
ios objective-c nserror
source share
4 answers

I have never seen this because NSError already very versatile. It allows you to determine the type of error by setting the domain and code properties and allowing arbitrary additional information in the userInfo dictionary.

So no, this is not good practice.

+6
source share

While I agree that you should not subclass NSError , it is very useful to place categories on it, and I do this regularly. For example, let's say your system often sends errors that come from some JSON block. It would be very convenient for me to create a category such as:

 @interface NSError (MyErrors) // Construct an NSError from data in JSON. // Include full JSON response in the userInfo + (NSError *)myErrorWithJSON:(JSON *)json; // Parse the text out of the user info - (NSString *)myErrorMessage; // The full JSON error block as a string - (NSString *)myErrorJSON; // BOOLs can be helpful sometimes, or you could return an enum for use in a switch. - (BOOL)myIsNetworkError; - (BOOL)myIsAuthError; @end 

I often write small helpers to build NSError easier, build userinfo the way I want, and pull the data back from userinfo without callers who need to know its internal representation. I believe this is a very good form of data hiding and encourages the use of more descriptive messages.

Similarly, even for small projects, I often create a category method +myErrorWithCode:localizedDescription: I know my domain, so I usually donโ€™t need to transfer it, and this makes it easier to set the NSLocalizedDescription key in user information. Again, this contributes to improving errors by simplifying their creation and simplifying changing the implementation details of your error handling.

+13
source share

The documentation says that this is normal for a subclass:

Applications can choose to subclass NSError, for example, to provide better localized error strings by overriding localizedDescription.

In my case, I work with OSStatus , which is Int32 . The NSError constructor only supports Int . Therefore, I need to subclass it to support OSSStatus .

+3
source share

Nice idea to extend an NSError. I also made a category on NSError for my own use. I would like to share it with you.

(1) Create a strings file to determine all error codes:

 /* Following are general error messgaes those we can show to user regarding to Internet connection and request. You can add more codes. */ "-1001" = "Connection time out"; "-1003" = "Cannot find Host"; "-1004" = "Cannot connect to Host"; "-1005" = "Server is temporarily down"; "-1009" = "The Internet connection appears to be offline"; "-1012" = "Authentication failed"; "2000" = "This is a custom error message"; // custom created error code /* Always describe unknow error with whatever you want in except case (ie except error codes). If not mentioned the following line, still you will get message "Unknown error" */ "Unknown error" = "Network error occured"; 

(2) Create a category in NSError, say, "NSError + ErrorInfo":

 @interface NSError (ErrorInfo) -(NSString *)userDescription; @end 

(3) Define this:

 #define ERROR_KEY(code) [NSString stringWithFormat:@"%d",code] #define ERROR_LOCALIZED_DESCRIPTION(code) NSLocalizedStringFromTable(ERROR_KEY(code),@"Errors",nil) @implementation NSError (ErrorInfo) -(NSString *)userDescription { NSString *errorDescrption = NSLocalizedStringFromTable(ERROR_KEY(self.code),@"Errors",nil); if (!errorDescrption || [errorDescrption isEqual:ERROR_KEY(self.code)]){ return NSLocalizedStringFromTable(@"Unknown error",@"Errors",nil);; } else{ return ERROR_LOCALIZED_DESCRIPTION(self.code); } return nil; } @end 

(4) Use it:

 NSError *yourError; // This can be any NSError object you get yourError = [NSError errorWithDomain:@"yourDomain" code:2000 userInfo:details]; // Just for test NSLog(@"%@",[yourError userDescription]); 
0
source share

All Articles