FIRAppCheckHTTPError.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2021 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/FIRAppCheckHTTPError.h"
  17. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  18. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckErrors.h"
  19. @implementation FIRAppCheckHTTPError
  20. - (instancetype)initWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse
  21. data:(nullable NSData *)data {
  22. NSDictionary *userInfo = [[self class] userInfoWithHTTPResponse:HTTPResponse data:data];
  23. self = [super initWithDomain:FIRAppCheckErrorDomain
  24. code:FIRAppCheckErrorCodeUnknown
  25. userInfo:userInfo];
  26. if (self) {
  27. _HTTPResponse = HTTPResponse;
  28. _data = data;
  29. }
  30. return self;
  31. }
  32. + (NSDictionary *)userInfoWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse
  33. data:(nullable NSData *)data {
  34. NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  35. NSString *failureReason =
  36. [NSString stringWithFormat:@"The server responded with an error: \n - URL: %@ \n - HTTP "
  37. @"status code: %ld \n - Response body: %@",
  38. HTTPResponse.URL, (long)HTTPResponse.statusCode, responseString];
  39. return @{NSLocalizedFailureReasonErrorKey : failureReason};
  40. }
  41. #pragma mark - NSCopying
  42. - (id)copyWithZone:(NSZone *)zone {
  43. return [[[self class] alloc] initWithHTTPResponse:self.HTTPResponse data:self.data];
  44. }
  45. #pragma mark - NSSecureCoding
  46. - (nullable instancetype)initWithCoder:(NSCoder *)coder {
  47. NSHTTPURLResponse *HTTPResponse = [coder decodeObjectOfClass:[NSHTTPURLResponse class]
  48. forKey:@"HTTPResponse"];
  49. if (!HTTPResponse) {
  50. return nil;
  51. }
  52. NSData *data = [coder decodeObjectOfClass:[NSData class] forKey:@"data"];
  53. return [self initWithHTTPResponse:HTTPResponse data:data];
  54. }
  55. - (void)encodeWithCoder:(NSCoder *)coder {
  56. [coder encodeObject:self.HTTPResponse forKey:@"HTTPResponse"];
  57. [coder encodeObject:self.data forKey:@"data"];
  58. }
  59. + (BOOL)supportsSecureCoding {
  60. return YES;
  61. }
  62. @end