FUNError.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Copyright 2017 Google
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "FUNError.h"
  16. #import "FUNSerializer.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. NSString *const FIRFunctionsErrorDomain = @"com.firebase.functions";
  19. NSString *const FIRFunctionsErrorDetailsKey = @"details";
  20. /**
  21. * Takes an HTTP status code and returns the corresponding FIRFunctionsErrorCode error code.
  22. * This is the standard HTTP status code -> error mapping defined in:
  23. * https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
  24. * @param status An HTTP status code.
  25. * @return The corresponding error code, or FIRFunctionsErrorCodeUnknown if none.
  26. */
  27. FIRFunctionsErrorCode FIRFunctionsErrorCodeForHTTPStatus(NSInteger status) {
  28. switch (status) {
  29. case 200:
  30. return FIRFunctionsErrorCodeOK;
  31. case 400:
  32. return FIRFunctionsErrorCodeInvalidArgument;
  33. case 401:
  34. return FIRFunctionsErrorCodeUnauthenticated;
  35. case 403:
  36. return FIRFunctionsErrorCodePermissionDenied;
  37. case 404:
  38. return FIRFunctionsErrorCodeNotFound;
  39. case 409:
  40. return FIRFunctionsErrorCodeAborted;
  41. case 429:
  42. return FIRFunctionsErrorCodeResourceExhausted;
  43. case 499:
  44. return FIRFunctionsErrorCodeCancelled;
  45. case 500:
  46. return FIRFunctionsErrorCodeInternal;
  47. case 501:
  48. return FIRFunctionsErrorCodeUnimplemented;
  49. case 503:
  50. return FIRFunctionsErrorCodeUnavailable;
  51. case 504:
  52. return FIRFunctionsErrorCodeDeadlineExceeded;
  53. }
  54. return FIRFunctionsErrorCodeInternal;
  55. }
  56. /**
  57. * Takes the name of an error code and returns the enum value for it.
  58. * @param name An error name.
  59. * @return The error code with this name, or FIRFunctionsErrorCodeUnknown if none.
  60. */
  61. FIRFunctionsErrorCode FIRFunctionsErrorCodeForName(NSString *name) {
  62. static NSDictionary<NSString *, NSNumber *> *errors;
  63. static dispatch_once_t onceToken;
  64. dispatch_once(&onceToken, ^{
  65. errors = @{
  66. @"OK" : @(FIRFunctionsErrorCodeOK),
  67. @"CANCELLED" : @(FIRFunctionsErrorCodeCancelled),
  68. @"UNKNOWN" : @(FIRFunctionsErrorCodeUnknown),
  69. @"INVALID_ARGUMENT" : @(FIRFunctionsErrorCodeInvalidArgument),
  70. @"DEADLINE_EXCEEDED" : @(FIRFunctionsErrorCodeDeadlineExceeded),
  71. @"NOT_FOUND" : @(FIRFunctionsErrorCodeNotFound),
  72. @"ALREADY_EXISTS" : @(FIRFunctionsErrorCodeAlreadyExists),
  73. @"PERMISSION_DENIED" : @(FIRFunctionsErrorCodePermissionDenied),
  74. @"RESOURCE_EXHAUSTED" : @(FIRFunctionsErrorCodeResourceExhausted),
  75. @"FAILED_PRECONDITION" : @(FIRFunctionsErrorCodeFailedPrecondition),
  76. @"ABORTED" : @(FIRFunctionsErrorCodeAborted),
  77. @"OUT_OF_RANGE" : @(FIRFunctionsErrorCodeOutOfRange),
  78. @"UNIMPLEMENTED" : @(FIRFunctionsErrorCodeUnimplemented),
  79. @"INTERNAL" : @(FIRFunctionsErrorCodeInternal),
  80. @"UNAVAILABLE" : @(FIRFunctionsErrorCodeUnavailable),
  81. @"DATA_LOSS" : @(FIRFunctionsErrorCodeDataLoss),
  82. @"UNAUTHENTICATED" : @(FIRFunctionsErrorCodeUnauthenticated),
  83. };
  84. });
  85. NSNumber *code = errors[name];
  86. if (code != nil) {
  87. return code.intValue;
  88. }
  89. return FIRFunctionsErrorCodeInternal;
  90. }
  91. /**
  92. * Takes a FIRFunctionsErrorCode and returns an English description of it.
  93. * @param code An error code.
  94. * @return A description of the code, or "UNKNOWN" if none.
  95. */
  96. NSString *FUNDescriptionForErrorCode(FIRFunctionsErrorCode code) {
  97. switch (code) {
  98. case FIRFunctionsErrorCodeOK:
  99. return @"OK";
  100. case FIRFunctionsErrorCodeCancelled:
  101. return @"CANCELLED";
  102. case FIRFunctionsErrorCodeUnknown:
  103. return @"UNKNOWN";
  104. case FIRFunctionsErrorCodeInvalidArgument:
  105. return @"INVALID ARGUMENT";
  106. case FIRFunctionsErrorCodeDeadlineExceeded:
  107. return @"DEADLINE EXCEEDED";
  108. case FIRFunctionsErrorCodeNotFound:
  109. return @"NOT FOUND";
  110. case FIRFunctionsErrorCodeAlreadyExists:
  111. return @"ALREADY EXISTS";
  112. case FIRFunctionsErrorCodePermissionDenied:
  113. return @"PERMISSION DENIED";
  114. case FIRFunctionsErrorCodeResourceExhausted:
  115. return @"RESOURCE EXHAUSTED";
  116. case FIRFunctionsErrorCodeFailedPrecondition:
  117. return @"FAILED PRECONDITION";
  118. case FIRFunctionsErrorCodeAborted:
  119. return @"ABORTED";
  120. case FIRFunctionsErrorCodeOutOfRange:
  121. return @"OUT OF RANGE";
  122. case FIRFunctionsErrorCodeUnimplemented:
  123. return @"UNIMPLEMENTED";
  124. case FIRFunctionsErrorCodeInternal:
  125. return @"INTERNAL";
  126. case FIRFunctionsErrorCodeUnavailable:
  127. return @"UNAVAILABLE";
  128. case FIRFunctionsErrorCodeDataLoss:
  129. return @"DATA LOSS";
  130. case FIRFunctionsErrorCodeUnauthenticated:
  131. return @"UNAUTHENTICATED";
  132. }
  133. return @"UNKNOWN";
  134. }
  135. NSError *_Nullable FUNErrorForCode(FIRFunctionsErrorCode code) {
  136. NSDictionary *userInfo = @{NSLocalizedDescriptionKey : FUNDescriptionForErrorCode(code)};
  137. return [NSError errorWithDomain:FIRFunctionsErrorDomain code:code userInfo:userInfo];
  138. }
  139. NSError *_Nullable FUNErrorForResponse(NSInteger status,
  140. NSData *_Nullable body,
  141. FUNSerializer *serializer) {
  142. // Start with reasonable defaults from the status code.
  143. FIRFunctionsErrorCode code = FIRFunctionsErrorCodeForHTTPStatus(status);
  144. NSString *description = FUNDescriptionForErrorCode(code);
  145. id details = nil;
  146. // Then look through the body for explicit details.
  147. if (body) {
  148. NSError *parseError = nil;
  149. id json = [NSJSONSerialization JSONObjectWithData:body options:0 error:&parseError];
  150. if (!parseError && [json isKindOfClass:[NSDictionary class]]) {
  151. id errorDetails = json[@"error"];
  152. if ([errorDetails isKindOfClass:[NSDictionary class]]) {
  153. if ([errorDetails[@"status"] isKindOfClass:[NSString class]]) {
  154. code = FIRFunctionsErrorCodeForName(errorDetails[@"status"]);
  155. // The default description needs to be updated for the new code.
  156. description = FUNDescriptionForErrorCode(code);
  157. }
  158. if ([errorDetails[@"message"] isKindOfClass:[NSString class]]) {
  159. description = (NSString *)errorDetails[@"message"];
  160. }
  161. details = errorDetails[@"details"];
  162. if (details) {
  163. NSError *decodeError = nil;
  164. details = [serializer decode:details error:&decodeError];
  165. // Just ignore the details if there an error decoding them.
  166. }
  167. }
  168. }
  169. }
  170. if (code == FIRFunctionsErrorCodeOK) {
  171. // Technically, there's an edge case where a developer could explicitly return an error code of
  172. // OK, and we will treat it as success, but that seems reasonable.
  173. return nil;
  174. }
  175. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  176. userInfo[NSLocalizedDescriptionKey] = description;
  177. if (details) {
  178. userInfo[FIRFunctionsErrorDetailsKey] = details;
  179. }
  180. return [NSError errorWithDomain:FIRFunctionsErrorDomain code:code userInfo:userInfo];
  181. }
  182. NS_ASSUME_NONNULL_END