FIRInstallationsHTTPError.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "FirebaseInstallations/Source/Library/Errors/FIRInstallationsHTTPError.h"
  17. #import "FirebaseInstallations/Source/Library/Errors/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 =
  40. [NSString stringWithFormat:@"The server responded with an error: \n - URL: %@ \n - HTTP "
  41. @"status code: %ld \n - Response body: %@",
  42. HTTPResponse.URL, (long)HTTPResponse.statusCode, responseString];
  43. return @{NSLocalizedFailureReasonErrorKey : failureReason};
  44. }
  45. #pragma mark - NSCopying
  46. - (id)copyWithZone:(NSZone *)zone {
  47. return [[FIRInstallationsHTTPError alloc] initWithHTTPResponse:self.HTTPResponse data:self.data];
  48. }
  49. #pragma mark - NSSecureCoding
  50. - (nullable instancetype)initWithCoder:(NSCoder *)coder {
  51. NSHTTPURLResponse *HTTPResponse = [coder decodeObjectOfClass:[NSHTTPURLResponse class]
  52. forKey:@"HTTPResponse"];
  53. if (!HTTPResponse) {
  54. return nil;
  55. }
  56. NSData *data = [coder decodeObjectOfClass:[NSData class] forKey:@"data"];
  57. return [self initWithHTTPResponse:HTTPResponse data:data];
  58. }
  59. - (void)encodeWithCoder:(NSCoder *)coder {
  60. [coder encodeObject:self.HTTPResponse forKey:@"HTTPResponse"];
  61. [coder encodeObject:self.data forKey:@"data"];
  62. }
  63. + (BOOL)supportsSecureCoding {
  64. return YES;
  65. }
  66. @end