FIRAppCheckErrorUtil.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2020 Google LLC
  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 "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  17. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckHTTPError.h"
  18. NSString *const kFIRAppCheckErrorDomain = @"com.firebase.appCheck";
  19. @implementation FIRAppCheckErrorUtil
  20. + (NSError *)cachedTokenNotFound {
  21. NSString *failureReason = [NSString stringWithFormat:@"Cached token not found."];
  22. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  23. failureReason:failureReason
  24. underlyingError:nil];
  25. }
  26. + (NSError *)cachedTokenExpired {
  27. NSString *failureReason = [NSString stringWithFormat:@"Cached token expired."];
  28. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  29. failureReason:failureReason
  30. underlyingError:nil];
  31. }
  32. + (FIRAppCheckHTTPError *)APIErrorWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse
  33. data:(nullable NSData *)data {
  34. return [[FIRAppCheckHTTPError alloc] initWithHTTPResponse:HTTPResponse data:data];
  35. }
  36. + (NSError *)APIErrorWithNetworkError:(NSError *)networkError {
  37. NSString *failureReason = [NSString stringWithFormat:@"API request error."];
  38. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  39. failureReason:failureReason
  40. underlyingError:networkError];
  41. }
  42. + (NSError *)appCheckTokenResponseErrorWithMissingField:(NSString *)fieldName {
  43. NSString *failureReason = [NSString
  44. stringWithFormat:@"Unexpected app check token response format. Field `%@` is missing.",
  45. fieldName];
  46. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  47. failureReason:failureReason
  48. underlyingError:nil];
  49. }
  50. + (NSError *)appAttestAttestationResponseErrorWithMissingField:(NSString *)fieldName {
  51. NSString *failureReason =
  52. [NSString stringWithFormat:@"Unexpected attestation response format. Field `%@` is missing.",
  53. fieldName];
  54. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  55. failureReason:failureReason
  56. underlyingError:nil];
  57. }
  58. + (NSError *)JSONSerializationError:(NSError *)error {
  59. NSString *failureReason = [NSString stringWithFormat:@"JSON serialization error."];
  60. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  61. failureReason:failureReason
  62. underlyingError:error];
  63. }
  64. + (NSError *)unsupportedAttestationProvider:(NSString *)providerName {
  65. NSString *failureReason = [NSString
  66. stringWithFormat:
  67. @"The attestation provider %@ is not supported on current platform and OS version.",
  68. providerName];
  69. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnsupported
  70. failureReason:failureReason
  71. underlyingError:nil];
  72. }
  73. + (NSError *)appAttestKeyIDNotFound {
  74. NSString *failureReason = @"App attest key ID not found.";
  75. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  76. failureReason:failureReason
  77. underlyingError:nil];
  78. }
  79. + (NSError *)errorWithFailureReason:(NSString *)failureReason {
  80. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  81. failureReason:failureReason
  82. underlyingError:nil];
  83. }
  84. + (NSError *)appCheckErrorWithCode:(FIRAppCheckErrorCode)code
  85. failureReason:(nullable NSString *)failureReason
  86. underlyingError:(nullable NSError *)underlyingError {
  87. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  88. userInfo[NSUnderlyingErrorKey] = underlyingError;
  89. userInfo[NSLocalizedFailureReasonErrorKey] = failureReason;
  90. return [NSError errorWithDomain:kFIRAppCheckErrorDomain code:code userInfo:userInfo];
  91. }
  92. @end
  93. void FIRAppCheckSetErrorToPointer(NSError *error, NSError **pointer) {
  94. if (pointer != NULL) {
  95. *pointer = error;
  96. }
  97. }