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