Custom NSError Codes

I am creating a Cocoa Touch Framework that uses a custom NSError object to pass errors back to the caller. I need to declare an enum that defines custom error codes. I went to this Apple doc which lists the NSURLErrorDomain error codes shown below:

enum {    
   NSURLErrorUnknown = -1,   
   NSURLErrorCancelled = -999,   
   NSURLErrorBadURL = -1000,    
   NSURLErrorTimedOut = -1001,   
   NSURLErrorUnsupportedURL = -1002,
   NSURLErrorUnsupportedURL = -1002,
   NSURLErrorCannotConnectToHost = -1004,   
   NSURLErrorDataLengthExceedsMaximum = -1103,   
   NSURLErrorNetworkConnectionLost = -1005,    
   NSURLErrorDNSLookupFailed = -1006,   
   NSURLErrorHTTPTooManyRedirects = -1007,    
   NSURLErrorResourceUnavailable = -1008,   
   NSURLErrorNotConnectedToInternet = -1009,   
   NSURLErrorRedirectToNonExistentLocation = -1010,   
   NSURLErrorBadServerResponse = -1011,   
   NSURLErrorUserCancelledAuthentication = -1012,   
   NSURLErrorUserAuthenticationRequired = -1013,   
   NSURLErrorZeroByteResource = -1014,   
   NSURLErrorCannotDecodeRawData = -1015,    
   NSURLErrorCannotDecodeContentData = -1016,    
   NSURLErrorCannotParseResponse = -1017,   
   NSURLErrorInternationalRoamingOff = -1018,    
   NSURLErrorCallIsActive = -1019,    
   NSURLErrorDataNotAllowed = -1020,    
   NSURLErrorRequestBodyStreamExhausted = -1021,   
   NSURLErrorFileDoesNotExist = -1100,    
   NSURLErrorFileIsDirectory = -1101,    
   NSURLErrorNoPermissionsToReadFile = -1102,    
   NSURLErrorSecureConnectionFailed = -1200,   
   NSURLErrorServerCertificateHasBadDate = -1201,   
   NSURLErrorServerCertificateUntrusted = -1202,   
   NSURLErrorServerCertificateHasUnknownRoot = -1203,   
   NSURLErrorServerCertificateNotYetValid = -1204,  
   NSURLErrorClientCertificateRejected = -1205,   
   NSURLErrorClientCertificateRequired = -1206,   
   NSURLErrorCannotLoadFromNetwork = -2000,    
   NSURLErrorCannotCreateFile= -3000,   
   NSURLErrorCannotOpenFile = -3001,   
   NSURLErrorCannotCloseFile = -3002,    
   NSURLErrorCannotWriteToFile = -3003,    
   NSURLErrorCannotRemoveFile = -3004,    
   NSURLErrorCannotMoveFile = -3005,   
   NSURLErrorDownloadDecodingFailedMidStream = -3006,   
   NSURLErrorDownloadDecodingFailedToComplete = -3007 
   }

      

1: Why does Apple use negative values ​​for error codes? Is there any specific reason for this?

2: Is there any pattern for Apple to randomize error codes?

+3


source to share


1 answer


  • Because based on return codes, 0 is usually fine. This leaves positive and negative integers. Its general preference for using positive integers for success codes (with additional information) and negative values ​​for error codes.
  • As you can see, the error codes are grouped. Unknown and special canceled, 1000 are HTTP errors, 1100 are access errors, 1200 are connection and certificate errors, 2000 network, 3000 file and stream problems.


Group your errors based on problem areas and use negatives for return code purposes (since returning a negative value is usually interpreted as an error).

+4


source







All Articles