FIRAppCheckErrorUtil.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. NSString *const kFIRAppCheckErrorDomain = @"com.firebase.appCheck";
  18. @implementation FIRAppCheckErrorUtil
  19. + (NSError *)cachedTokenNotFound {
  20. NSString *failureReason = [NSString stringWithFormat:@"Cached token not found."];
  21. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  22. failureReason:failureReason
  23. underlyingError:nil];
  24. }
  25. + (NSError *)cachedTokenExpired {
  26. NSString *failureReason = [NSString stringWithFormat:@"Cached token expired."];
  27. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  28. failureReason:failureReason
  29. underlyingError:nil];
  30. }
  31. + (NSError *)APIErrorWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse
  32. data:(nullable NSData *)data {
  33. NSString *body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"";
  34. NSString *failureReason =
  35. [NSString stringWithFormat:@"Unexpected API response. HTTP code: %ld, body: \n%@",
  36. (long)HTTPResponse.statusCode, body];
  37. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  38. failureReason:failureReason
  39. underlyingError:nil];
  40. }
  41. + (NSError *)APIErrorWithNetworkError:(NSError *)networkError {
  42. NSString *failureReason = [NSString stringWithFormat:@"API request error."];
  43. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  44. failureReason:failureReason
  45. underlyingError:networkError];
  46. }
  47. + (NSError *)appCheckTokenResponseErrorWithMissingField:(NSString *)fieldName {
  48. NSString *failureReason = [NSString
  49. stringWithFormat:@"Unexpected app check token response format. Field `%@` is missing.",
  50. fieldName];
  51. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  52. failureReason:failureReason
  53. underlyingError:nil];
  54. }
  55. + (NSError *)JSONSerializationError:(NSError *)error {
  56. NSString *failureReason = [NSString stringWithFormat:@"JSON serialization error."];
  57. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  58. failureReason:failureReason
  59. underlyingError:error];
  60. }
  61. + (NSError *)errorWithFailureReason:(NSString *)failureReason {
  62. return [self appCheckErrorWithCode:FIRAppCheckErrorCodeUnknown
  63. failureReason:failureReason
  64. underlyingError:nil];
  65. }
  66. + (NSError *)appCheckErrorWithCode:(FIRAppCheckErrorCode)code
  67. failureReason:(nullable NSString *)failureReason
  68. underlyingError:(nullable NSError *)underlyingError {
  69. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  70. userInfo[NSUnderlyingErrorKey] = underlyingError;
  71. userInfo[NSLocalizedFailureReasonErrorKey] = failureReason;
  72. return [NSError errorWithDomain:kFIRAppCheckErrorDomain code:code userInfo:userInfo];
  73. }
  74. @end
  75. void FIRAppCheckSetErrorToPointer(NSError *error, NSError **pointer) {
  76. if (pointer != NULL) {
  77. *pointer = error;
  78. }
  79. }