Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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/FIRInstallationsErrorUtil.h"
18
 
19
#import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsHTTPError.h"
20
 
21
#if __has_include(<FBLPromises/FBLPromises.h>)
22
#import <FBLPromises/FBLPromises.h>
23
#else
24
#import "FBLPromises.h"
25
#endif
26
 
27
NSString *const kFirebaseInstallationsErrorDomain = @"com.firebase.installations";
28
 
29
void FIRInstallationsItemSetErrorToPointer(NSError *error, NSError **pointer) {
30
  if (pointer != NULL) {
31
    *pointer = error;
32
  }
33
}
34
 
35
@implementation FIRInstallationsErrorUtil
36
 
37
+ (NSError *)keyedArchiverErrorWithException:(NSException *)exception {
38
  NSString *failureReason = [NSString
39
      stringWithFormat:@"NSKeyedArchiver exception with name: %@, reason: %@, userInfo: %@",
40
                       exception.name, exception.reason, exception.userInfo];
41
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
42
                            failureReason:failureReason
43
                          underlyingError:nil];
44
}
45
 
46
+ (NSError *)keyedArchiverErrorWithError:(NSError *)error {
47
  NSString *failureReason = [NSString stringWithFormat:@"NSKeyedArchiver error."];
48
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
49
                            failureReason:failureReason
50
                          underlyingError:error];
51
}
52
 
53
+ (NSError *)keychainErrorWithFunction:(NSString *)keychainFunction status:(OSStatus)status {
54
  NSString *failureReason = [NSString stringWithFormat:@"%@ (%li)", keychainFunction, (long)status];
55
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeKeychain
56
                            failureReason:failureReason
57
                          underlyingError:nil];
58
}
59
 
60
+ (NSError *)installationItemNotFoundForAppID:(NSString *)appID appName:(NSString *)appName {
61
  NSString *failureReason =
62
      [NSString stringWithFormat:@"Installation for appID %@ appName %@ not found", appID, appName];
63
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
64
                            failureReason:failureReason
65
                          underlyingError:nil];
66
}
67
 
68
+ (NSError *)corruptedIIDTokenData {
69
  NSString *failureReason =
70
      @"IID token data stored in Keychain is corrupted or in an incompatible format.";
71
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
72
                            failureReason:failureReason
73
                          underlyingError:nil];
74
}
75
 
76
+ (FIRInstallationsHTTPError *)APIErrorWithHTTPResponse:(NSHTTPURLResponse *)HTTPResponse
77
                                                   data:(nullable NSData *)data {
78
  return [[FIRInstallationsHTTPError alloc] initWithHTTPResponse:HTTPResponse data:data];
79
}
80
 
81
+ (BOOL)isAPIError:(NSError *)error withHTTPCode:(NSInteger)HTTPCode {
82
  if (![error isKindOfClass:[FIRInstallationsHTTPError class]]) {
83
    return NO;
84
  }
85
 
86
  return [(FIRInstallationsHTTPError *)error HTTPResponse].statusCode == HTTPCode;
87
}
88
 
89
+ (NSError *)JSONSerializationError:(NSError *)error {
90
  NSString *failureReason = [NSString stringWithFormat:@"Failed to serialize JSON data."];
91
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
92
                            failureReason:failureReason
93
                          underlyingError:nil];
94
}
95
 
96
+ (NSError *)FIDRegistrationErrorWithResponseMissingField:(NSString *)missingFieldName {
97
  NSString *failureReason = [NSString
98
      stringWithFormat:@"A required response field with name %@ is missing", missingFieldName];
99
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
100
                            failureReason:failureReason
101
                          underlyingError:nil];
102
}
103
 
104
+ (NSError *)networkErrorWithError:(NSError *)error {
105
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeServerUnreachable
106
                            failureReason:@"Network connection error."
107
                          underlyingError:error];
108
}
109
 
110
+ (NSError *)backoffIntervalWaitError {
111
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeServerUnreachable
112
                            failureReason:@"Too many server requests."
113
                          underlyingError:nil];
114
}
115
 
116
+ (NSError *)publicDomainErrorWithError:(NSError *)error {
117
  if ([error.domain isEqualToString:kFirebaseInstallationsErrorDomain]) {
118
    return error;
119
  }
120
 
121
  return [self installationsErrorWithCode:FIRInstallationsErrorCodeUnknown
122
                            failureReason:nil
123
                          underlyingError:error];
124
}
125
 
126
+ (NSError *)installationsErrorWithCode:(FIRInstallationsErrorCode)code
127
                          failureReason:(nullable NSString *)failureReason
128
                        underlyingError:(nullable NSError *)underlyingError {
129
  NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
130
  userInfo[NSUnderlyingErrorKey] = underlyingError;
131
  userInfo[NSLocalizedFailureReasonErrorKey] =
132
      failureReason
133
          ?: [NSString
134
                 stringWithFormat:@"Underlying error: %@", underlyingError.localizedDescription];
135
 
136
  return [NSError errorWithDomain:kFirebaseInstallationsErrorDomain code:code userInfo:userInfo];
137
}
138
 
139
+ (FBLPromise *)rejectedPromiseWithError:(NSError *)error {
140
  FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
141
  [rejectedPromise reject:error];
142
  return rejectedPromise;
143
}
144
 
145
@end