FIRInstallationsErrorUtil.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2019 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. */
  16. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
  17. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsHTTPError.h"
  18. #if __has_include(<FBLPromises/FBLPromises.h>)
  19. #import <FBLPromises/FBLPromises.h>
  20. #else
  21. #import "FBLPromises.h"
  22. #endif
  23. NSString *const kFirebaseInstallationsErrorDomain = @"com.firebase.installations";
  24. void FIRInstallationsItemSetErrorToPointer(NSError *error, NSError **pointer) {
  25. if (pointer != NULL) {
  26. *pointer = error;
  27. }
  28. }
  29. @implementation FIRInstallationsErrorUtil
  30. + (NSError *)keyedArchiverErrorWithException:(NSException *)exception {
  31. NSString *failureReason = [NSString
  32. stringWithFormat:@"NSKeyedArchiver exception with name: %@, reason: %@, userInfo: %@",
  33. exception.name, exception.reason, exception.userInfo];
  34. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  35. failureReason:failureReason
  36. underlyingError:nil];
  37. }
  38. + (NSError *)keyedArchiverErrorWithError:(NSError *)error {
  39. NSString *failureReason = [NSString stringWithFormat:@"NSKeyedArchiver error."];
  40. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  41. failureReason:failureReason
  42. underlyingError:error];
  43. }
  44. + (NSError *)keychainErrorWithFunction:(NSString *)keychainFunction status:(OSStatus)status {
  45. NSString *failureReason = [NSString stringWithFormat:@"%@ (%li)", keychainFunction, (long)status];
  46. return [self installationsErrorWithCode:FIRInstallationsErrorCodeKeychain
  47. failureReason:failureReason
  48. underlyingError:nil];
  49. }
  50. + (NSError *)installationItemNotFoundForAppID:(NSString *)appID appName:(NSString *)appName {
  51. NSString *failureReason =
  52. [NSString stringWithFormat:@"Installation for appID %@ appName %@ not found", appID, appName];
  53. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  54. failureReason:failureReason
  55. underlyingError:nil];
  56. }
  57. + (NSError *)corruptedIIDTokenData {
  58. NSString *failureReason =
  59. @"IID token data stored in Keychain is corrupted or in an incompatible format.";
  60. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  61. failureReason:failureReason
  62. underlyingError:nil];
  63. }
  64. + (FIRInstallationsHTTPError *)APIErrorWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse
  65. data:(nullable NSData *)data {
  66. return [[FIRInstallationsHTTPError alloc] initWithHTTPResponse:HTTPResponse data:data];
  67. }
  68. + (BOOL)isAPIError:(NSError *)error withHTTPCode:(NSInteger)HTTPCode {
  69. if (![error isKindOfClass:[FIRInstallationsHTTPError class]]) {
  70. return NO;
  71. }
  72. return [(FIRInstallationsHTTPError *)error HTTPResponse].statusCode == HTTPCode;
  73. }
  74. + (NSError *)JSONSerializationError:(NSError *)error {
  75. NSString *failureReason = [NSString stringWithFormat:@"Failed to serialize JSON data."];
  76. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  77. failureReason:failureReason
  78. underlyingError:nil];
  79. }
  80. + (NSError *)FIDRegistrationErrorWithResponseMissingField:(NSString *)missingFieldName {
  81. NSString *failureReason = [NSString
  82. stringWithFormat:@"A required response field with name %@ is missing", missingFieldName];
  83. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  84. failureReason:failureReason
  85. underlyingError:nil];
  86. }
  87. + (NSError *)networkErrorWithError:(NSError *)error {
  88. return [self installationsErrorWithCode:FIRInstallationsErrorCodeServerUnreachable
  89. failureReason:@"Network connection error."
  90. underlyingError:error];
  91. }
  92. + (NSError *)backoffIntervalWaitError {
  93. return [self installationsErrorWithCode:FIRInstallationsErrorCodeServerUnreachable
  94. failureReason:@"Too many server requests."
  95. underlyingError:nil];
  96. }
  97. + (NSError *)publicDomainErrorWithError:(NSError *)error {
  98. if ([error.domain isEqualToString:kFirebaseInstallationsErrorDomain]) {
  99. return error;
  100. }
  101. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  102. failureReason:nil
  103. underlyingError:error];
  104. }
  105. + (NSError *)installationsErrorWithCode:(FIRInstallationsErrorCode)code
  106. failureReason:(nullable NSString *)failureReason
  107. underlyingError:(nullable NSError *)underlyingError {
  108. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  109. userInfo[NSUnderlyingErrorKey] = underlyingError;
  110. userInfo[NSLocalizedFailureReasonErrorKey] =
  111. failureReason
  112. ?: [NSString
  113. stringWithFormat:@"Underlying error: %@", underlyingError.localizedDescription];
  114. return [NSError errorWithDomain:kFirebaseInstallationsErrorDomain code:code userInfo:userInfo];
  115. }
  116. + (FBLPromise *)rejectedPromiseWithError:(NSError *)error {
  117. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  118. [rejectedPromise reject:error];
  119. return rejectedPromise;
  120. }
  121. @end