FIRInstallationsErrorUtil.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "FIRInstallationsErrorUtil.h"
  17. #import "FIRInstallationsHTTPError.h"
  18. NSString *const kFirebaseInstallationsErrorDomain = @"com.firebase.installations";
  19. void FIRInstallationsItemSetErrorToPointer(NSError *error, NSError **pointer) {
  20. if (pointer != NULL) {
  21. *pointer = error;
  22. }
  23. }
  24. @implementation FIRInstallationsErrorUtil
  25. + (NSError *)keyedArchiverErrorWithException:(NSException *)exception {
  26. NSString *failureReason = [NSString
  27. stringWithFormat:@"NSKeyedArchiver exception with name: %@, reason: %@, userInfo: %@",
  28. exception.name, exception.reason, exception.userInfo];
  29. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  30. failureReason:failureReason
  31. underlyingError:nil];
  32. }
  33. + (NSError *)keyedArchiverErrorWithError:(NSError *)error {
  34. NSString *failureReason = [NSString stringWithFormat:@"NSKeyedArchiver error."];
  35. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  36. failureReason:failureReason
  37. underlyingError:error];
  38. }
  39. + (NSError *)keychainErrorWithFunction:(NSString *)keychainFunction status:(OSStatus)status {
  40. NSString *failureReason = [NSString stringWithFormat:@"%@ (%li)", keychainFunction, (long)status];
  41. return [self installationsErrorWithCode:FIRInstallationsErrorCodeKeychain
  42. failureReason:failureReason
  43. underlyingError:nil];
  44. }
  45. + (NSError *)installationItemNotFoundForAppID:(NSString *)appID appName:(NSString *)appName {
  46. NSString *failureReason =
  47. [NSString stringWithFormat:@"Installation for appID %@ appName %@ not found", appID, appName];
  48. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  49. failureReason:failureReason
  50. underlyingError:nil];
  51. }
  52. + (FIRInstallationsHTTPError *)APIErrorWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse
  53. data:(nullable NSData *)data {
  54. return [[FIRInstallationsHTTPError alloc] initWithHTTPResponse:HTTPResponse data:data];
  55. }
  56. + (BOOL)isAPIError:(NSError *)error withHTTPCode:(NSInteger)HTTPCode {
  57. if (![error isKindOfClass:[FIRInstallationsHTTPError class]]) {
  58. return NO;
  59. }
  60. return [(FIRInstallationsHTTPError *)error HTTPResponse].statusCode == HTTPCode;
  61. }
  62. + (NSError *)JSONSerializationError:(NSError *)error {
  63. NSString *failureReason = [NSString stringWithFormat:@"Failed to serialize JSON data."];
  64. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  65. failureReason:failureReason
  66. underlyingError:nil];
  67. }
  68. + (NSError *)FIDRegistrationErrorWithResponseMissingField:(NSString *)missingFieldName {
  69. NSString *failureReason = [NSString
  70. stringWithFormat:@"A required response field with name %@ is missing", missingFieldName];
  71. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  72. failureReason:failureReason
  73. underlyingError:nil];
  74. }
  75. + (NSError *)networkErrorWithError:(NSError *)error {
  76. return [self installationsErrorWithCode:FIRInstallationsErrorCodeServerUnreachable
  77. failureReason:@"Network connection error."
  78. underlyingError:error];
  79. }
  80. + (NSError *)publicDomainErrorWithError:(NSError *)error {
  81. if ([error.domain isEqualToString:kFirebaseInstallationsErrorDomain]) {
  82. return error;
  83. }
  84. return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
  85. failureReason:nil
  86. underlyingError:error];
  87. }
  88. + (NSError *)installationsErrorWithCode:(FIRInstallationsErrorCode)code
  89. failureReason:(nullable NSString *)failureReason
  90. underlyingError:(nullable NSError *)underlyingError {
  91. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  92. userInfo[NSUnderlyingErrorKey] = underlyingError;
  93. userInfo[NSLocalizedFailureReasonErrorKey] = failureReason;
  94. return [NSError errorWithDomain:kFirebaseInstallationsErrorDomain code:code userInfo:userInfo];
  95. }
  96. @end