FIRAppCheckErrorUtil.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <AppCheckCore/AppCheckCore.h>
  18. @implementation FIRAppCheckErrorUtil
  19. + (NSError *)publicDomainErrorWithError:(NSError *)error {
  20. if ([error.domain isEqualToString:GACAppCheckErrorDomain]) {
  21. return [self publicDomainErrorWithGACError:error];
  22. } else if ([error.domain isEqualToString:FIRAppCheckErrorDomain]) {
  23. return error;
  24. }
  25. return [self unknownErrorWithError:error];
  26. }
  27. /// Converts an App Check Core error (`GACAppCheckErrorDomain`) to a public error
  28. /// (`FIRAppCheckErrorDomain`).
  29. + (NSError *)publicDomainErrorWithGACError:(NSError *)appCheckCoreError {
  30. FIRAppCheckErrorCode errorCode;
  31. switch ((GACAppCheckErrorCode)appCheckCoreError.code) {
  32. case GACAppCheckErrorCodeUnknown:
  33. errorCode = FIRAppCheckErrorCodeUnknown;
  34. break;
  35. case GACAppCheckErrorCodeServerUnreachable:
  36. errorCode = FIRAppCheckErrorCodeServerUnreachable;
  37. break;
  38. case GACAppCheckErrorCodeInvalidConfiguration:
  39. errorCode = FIRAppCheckErrorCodeInvalidConfiguration;
  40. break;
  41. case GACAppCheckErrorCodeKeychain:
  42. errorCode = FIRAppCheckErrorCodeKeychain;
  43. break;
  44. case GACAppCheckErrorCodeUnsupported:
  45. errorCode = FIRAppCheckErrorCodeUnsupported;
  46. break;
  47. default:
  48. return [self unknownErrorWithError:appCheckCoreError];
  49. }
  50. return [NSError errorWithDomain:FIRAppCheckErrorDomain
  51. code:errorCode
  52. userInfo:appCheckCoreError.userInfo];
  53. }
  54. #pragma mark - Helpers
  55. + (NSError *)unknownErrorWithError:(NSError *)error {
  56. NSString *failureReason = error.userInfo[NSLocalizedFailureReasonErrorKey];
  57. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  58. failureReason:failureReason
  59. underlyingError:error];
  60. }
  61. + (NSError *)appCheckErrorWithCode:(FIRAppCheckErrorCode)code
  62. failureReason:(nullable NSString *)failureReason
  63. underlyingError:(nullable NSError *)underlyingError {
  64. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  65. userInfo[NSUnderlyingErrorKey] = underlyingError;
  66. userInfo[NSLocalizedFailureReasonErrorKey] = failureReason;
  67. return [NSError errorWithDomain:FIRAppCheckErrorDomain code:code userInfo:userInfo];
  68. }
  69. @end