FUNError.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "FirebaseFunctions/Sources/FUNError.h"
  16. #import "FirebaseFunctions/Sources/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. NSNumber *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. return errors[name];
  86. }
  87. /**
  88. * Takes a FIRFunctionsErrorCode and returns an English description of it.
  89. * @param code An error code.
  90. * @return A description of the code, or "UNKNOWN" if none.
  91. */
  92. NSString *FUNDescriptionForErrorCode(FIRFunctionsErrorCode code) {
  93. switch (code) {
  94. case FIRFunctionsErrorCodeOK:
  95. return @"OK";
  96. case FIRFunctionsErrorCodeCancelled:
  97. return @"CANCELLED";
  98. case FIRFunctionsErrorCodeUnknown:
  99. return @"UNKNOWN";
  100. case FIRFunctionsErrorCodeInvalidArgument:
  101. return @"INVALID ARGUMENT";
  102. case FIRFunctionsErrorCodeDeadlineExceeded:
  103. return @"DEADLINE EXCEEDED";
  104. case FIRFunctionsErrorCodeNotFound:
  105. return @"NOT FOUND";
  106. case FIRFunctionsErrorCodeAlreadyExists:
  107. return @"ALREADY EXISTS";
  108. case FIRFunctionsErrorCodePermissionDenied:
  109. return @"PERMISSION DENIED";
  110. case FIRFunctionsErrorCodeResourceExhausted:
  111. return @"RESOURCE EXHAUSTED";
  112. case FIRFunctionsErrorCodeFailedPrecondition:
  113. return @"FAILED PRECONDITION";
  114. case FIRFunctionsErrorCodeAborted:
  115. return @"ABORTED";
  116. case FIRFunctionsErrorCodeOutOfRange:
  117. return @"OUT OF RANGE";
  118. case FIRFunctionsErrorCodeUnimplemented:
  119. return @"UNIMPLEMENTED";
  120. case FIRFunctionsErrorCodeInternal:
  121. return @"INTERNAL";
  122. case FIRFunctionsErrorCodeUnavailable:
  123. return @"UNAVAILABLE";
  124. case FIRFunctionsErrorCodeDataLoss:
  125. return @"DATA LOSS";
  126. case FIRFunctionsErrorCodeUnauthenticated:
  127. return @"UNAUTHENTICATED";
  128. }
  129. return @"UNKNOWN";
  130. }
  131. NSError *_Nullable FUNErrorForCode(FIRFunctionsErrorCode code) {
  132. NSDictionary *userInfo = @{NSLocalizedDescriptionKey : FUNDescriptionForErrorCode(code)};
  133. return [NSError errorWithDomain:FIRFunctionsErrorDomain code:code userInfo:userInfo];
  134. }
  135. NSError *_Nullable FUNErrorForResponse(NSInteger status,
  136. NSData *_Nullable body,
  137. FUNSerializer *serializer) {
  138. // Start with reasonable defaults from the status code.
  139. FIRFunctionsErrorCode code = FIRFunctionsErrorCodeForHTTPStatus(status);
  140. NSString *description = FUNDescriptionForErrorCode(code);
  141. id details = nil;
  142. // Then look through the body for explicit details.
  143. if (body) {
  144. NSError *parseError = nil;
  145. id json = [NSJSONSerialization JSONObjectWithData:body options:0 error:&parseError];
  146. if (!parseError && [json isKindOfClass:[NSDictionary class]]) {
  147. id errorDetails = json[@"error"];
  148. if ([errorDetails isKindOfClass:[NSDictionary class]]) {
  149. if ([errorDetails[@"status"] isKindOfClass:[NSString class]]) {
  150. NSNumber *codeNumber = FIRFunctionsErrorCodeForName(errorDetails[@"status"]);
  151. if (codeNumber == nil) {
  152. // If the code in the body is invalid, treat the whole response as malformed.
  153. return FUNErrorForCode(FIRFunctionsErrorCodeInternal);
  154. }
  155. code = codeNumber.intValue;
  156. // The default description needs to be updated for the new code.
  157. description = FUNDescriptionForErrorCode(code);
  158. }
  159. if ([errorDetails[@"message"] isKindOfClass:[NSString class]]) {
  160. description = (NSString *)errorDetails[@"message"];
  161. }
  162. details = errorDetails[@"details"];
  163. if (details) {
  164. NSError *decodeError = nil;
  165. details = [serializer decode:details error:&decodeError];
  166. // Just ignore the details if there an error decoding them.
  167. }
  168. }
  169. }
  170. }
  171. if (code == FIRFunctionsErrorCodeOK) {
  172. // Technically, there's an edge case where a developer could explicitly return an error code of
  173. // OK, and we will treat it as success, but that seems reasonable.
  174. return nil;
  175. }
  176. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  177. userInfo[NSLocalizedDescriptionKey] = description;
  178. if (details) {
  179. userInfo[FIRFunctionsErrorDetailsKey] = details;
  180. }
  181. return [NSError errorWithDomain:FIRFunctionsErrorDomain code:code userInfo:userInfo];
  182. }
  183. NS_ASSUME_NONNULL_END