QNResponseInfo.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // QNResponseInfo.m
  3. // QiniuSDK
  4. //
  5. // Created by bailong on 14/10/2.
  6. // Copyright (c) 2014年 Qiniu. All rights reserved.
  7. //
  8. #import "QNResponseInfo.h"
  9. #import "QNHttpResponseInfo.h"
  10. #import "QNUserAgent.h"
  11. #import "QNVersion.h"
  12. #import "QNUploadInfoReporter.h"
  13. const int kQNZeroDataSize = -6;
  14. const int kQNInvalidToken = -5;
  15. const int kQNFileError = -4;
  16. const int kQNInvalidArgument = -3;
  17. const int kQNRequestCancelled = -2;
  18. const int kQNNetworkError = -1;
  19. /**
  20. https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/index.html#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes
  21. NSURLErrorUnknown = -1,
  22. NSURLErrorCancelled = -999,
  23. NSURLErrorBadURL = -1000,
  24. NSURLErrorTimedOut = -1001,
  25. NSURLErrorUnsupportedURL = -1002,
  26. NSURLErrorCannotFindHost = -1003,
  27. NSURLErrorCannotConnectToHost = -1004,
  28. NSURLErrorDataLengthExceedsMaximum = -1103,
  29. NSURLErrorNetworkConnectionLost = -1005,
  30. NSURLErrorDNSLookupFailed = -1006,
  31. NSURLErrorHTTPTooManyRedirects = -1007,
  32. NSURLErrorResourceUnavailable = -1008,
  33. NSURLErrorNotConnectedToInternet = -1009,
  34. NSURLErrorRedirectToNonExistentLocation = -1010,
  35. NSURLErrorBadServerResponse = -1011,
  36. NSURLErrorUserCancelledAuthentication = -1012,
  37. NSURLErrorUserAuthenticationRequired = -1013,
  38. NSURLErrorZeroByteResource = -1014,
  39. NSURLErrorCannotDecodeRawData = -1015,
  40. NSURLErrorCannotDecodeContentData = -1016,
  41. NSURLErrorCannotParseResponse = -1017,
  42. NSURLErrorInternationalRoamingOff = -1018,
  43. NSURLErrorCallIsActive = -1019,
  44. NSURLErrorDataNotAllowed = -1020,
  45. NSURLErrorRequestBodyStreamExhausted = -1021,
  46. NSURLErrorFileDoesNotExist = -1100,
  47. NSURLErrorFileIsDirectory = -1101,
  48. NSURLErrorNoPermissionsToReadFile = -1102,
  49. NSURLErrorSecureConnectionFailed = -1200,
  50. NSURLErrorServerCertificateHasBadDate = -1201,
  51. NSURLErrorServerCertificateUntrusted = -1202,
  52. NSURLErrorServerCertificateHasUnknownRoot = -1203,
  53. NSURLErrorServerCertificateNotYetValid = -1204,
  54. NSURLErrorClientCertificateRejected = -1205,
  55. NSURLErrorClientCertificateRequired = -1206,
  56. NSURLErrorCannotLoadFromNetwork = -2000,
  57. NSURLErrorCannotCreateFile = -3000,
  58. NSURLErrorCannotOpenFile = -3001,
  59. NSURLErrorCannotCloseFile = -3002,
  60. NSURLErrorCannotWriteToFile = -3003,
  61. NSURLErrorCannotRemoveFile = -3004,
  62. NSURLErrorCannotMoveFile = -3005,
  63. NSURLErrorDownloadDecodingFailedMidStream = -3006,
  64. NSURLErrorDownloadDecodingFailedToComplete = -3007
  65. */
  66. static NSString *domain = @"qiniu.com";
  67. @interface QNResponseInfo ()
  68. @end
  69. @implementation QNResponseInfo
  70. + (instancetype)cancelWithDuration:(double)duration {
  71. return [[QNResponseInfo alloc] initWithStatus:kQNRequestCancelled errorDescription:@"cancelled by user" duration:duration];
  72. }
  73. + (instancetype)responseInfoWithInvalidArgument:(NSString *)text duration:(double)duration {
  74. return [[QNResponseInfo alloc] initWithStatus:kQNInvalidArgument errorDescription:text duration:duration];
  75. }
  76. + (instancetype)responseInfoWithInvalidToken:(NSString *)text duration:(double)duration {
  77. return [[QNResponseInfo alloc] initWithStatus:kQNInvalidToken errorDescription:text duration:duration];
  78. }
  79. + (instancetype)responseInfoWithFileError:(NSError *)error duration:(double)duration {
  80. return [[QNResponseInfo alloc] initWithStatus:kQNFileError error:error host:nil duration:duration];
  81. }
  82. + (instancetype)responseInfoOfZeroData:(NSString *)path duration:(double)duration {
  83. NSString *desc;
  84. if (path == nil) {
  85. desc = @"data size is 0";
  86. } else {
  87. desc = [[NSString alloc] initWithFormat:@"file %@ size is 0", path];
  88. }
  89. return [[QNResponseInfo alloc] initWithStatus:kQNZeroDataSize errorDescription:desc duration:duration];
  90. }
  91. + (instancetype)responseInfoWithHttpResponseInfo:(QNHttpResponseInfo *)httpResponseInfo duration:(double)duration {
  92. if (httpResponseInfo.hasHttpResponse) {
  93. return [[QNResponseInfo alloc] initWithStatusCode:(int)httpResponseInfo.statusCode reqId:httpResponseInfo.reqId xlog:httpResponseInfo.xlog xvia:httpResponseInfo.xvia host:httpResponseInfo.host error:httpResponseInfo.error duration:duration];
  94. } else {
  95. return [[QNResponseInfo alloc] initWithNetError:httpResponseInfo.error host:httpResponseInfo.host duration:duration];
  96. }
  97. }
  98. - (instancetype)initWithStatus:(int)status
  99. errorDescription:(NSString *)text
  100. duration:(double)duration {
  101. NSError *error = [[NSError alloc] initWithDomain:domain code:status userInfo:@{ @"error" : text }];
  102. return [self initWithStatus:status error:error host:nil duration:duration];
  103. }
  104. - (instancetype)initWithStatus:(int)status
  105. error:(NSError *)error
  106. host:(NSString *)host
  107. duration:(double)duration {
  108. if (self = [super init]) {
  109. _statusCode = status;
  110. _error = error;
  111. _host = host;
  112. _duration = duration;
  113. _id = [QNUserAgent sharedInstance].id;
  114. _timeStamp = [[NSDate date] timeIntervalSince1970];
  115. _xClientId = Reporter.X_Log_Client_Id;
  116. }
  117. return self;
  118. }
  119. - (instancetype)initWithNetError:(NSError *)error
  120. host:(NSString *)host
  121. duration:(double)duration {
  122. int code = kQNNetworkError;
  123. if (error != nil) {
  124. code = (int)error.code;
  125. }
  126. return [[QNResponseInfo alloc] initWithStatus:code error:error host:host duration:duration];
  127. }
  128. - (instancetype)initWithStatusCode:(int)statusCode
  129. reqId:(NSString *)reqId
  130. xlog:(NSString *)xlog
  131. xvia:(NSString *)xvia
  132. host:(NSString *)host
  133. error:(NSError *)error
  134. duration:(double)duration {
  135. if (self = [super init]) {
  136. _statusCode = statusCode;
  137. _reqId = reqId;
  138. _xlog = xlog;
  139. _xvia = xvia;
  140. _host = host;
  141. _error = error;
  142. _duration = duration;
  143. _id = [QNUserAgent sharedInstance].id;
  144. _timeStamp = [[NSDate date] timeIntervalSince1970];
  145. _xClientId = Reporter.X_Log_Client_Id;
  146. }
  147. return self;
  148. }
  149. - (NSString *)description {
  150. return [NSString stringWithFormat:@"<%@= id: %@, ver: %@, status: %d, requestId: %@, xClientId: %@, xlog: %@, xvia: %@, host: %@ duration: %.3f s time: %llu error: %@>", NSStringFromClass([self class]), _id, kQiniuVersion, _statusCode, _reqId, _xClientId, _xlog, _xvia, _host, _duration, _timeStamp, _error];
  151. }
  152. - (BOOL)isCancelled {
  153. return _statusCode == kQNRequestCancelled || _statusCode == -999;
  154. }
  155. - (BOOL)isNotQiniu {
  156. // reqId is nill means the server is not qiniu
  157. return (_statusCode >= 200 && _statusCode < 500) && _reqId == nil;
  158. }
  159. - (BOOL)isOK {
  160. return _statusCode == 200 && _error == nil && _reqId != nil;
  161. }
  162. - (BOOL)isConnectionBroken {
  163. return _statusCode == kQNNetworkError || (_statusCode < -1000 && _statusCode != -1003);
  164. }
  165. @end