FUNError.m 7.0 KB

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