FIRAppCheckHTTPError.m 2.6 KB

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