FIRInstallationsHTTPError.m 2.7 KB

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