forked from firebase/firebase-ios-sdk
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFUNError.m
196 lines (183 loc) · 7.43 KB
/
FUNError.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Copyright 2017 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import"Functions/FirebaseFunctions/FUNError.h"
#import"Functions/FirebaseFunctions/FUNSerializer.h"
NS_ASSUME_NONNULL_BEGIN
NSString *const FIRFunctionsErrorDomain = @"com.firebase.functions";
NSString *const FIRFunctionsErrorDetailsKey = @"details";
/**
* Takes an HTTP status code and returns the corresponding FIRFunctionsErrorCode error code.
* This is the standard HTTP status code -> error mapping defined in:
* https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
* @param status An HTTP status code.
* @return The corresponding error code, or FIRFunctionsErrorCodeUnknown if none.
*/
FIRFunctionsErrorCode FIRFunctionsErrorCodeForHTTPStatus(NSInteger status) {
switch (status) {
case200:
return FIRFunctionsErrorCodeOK;
case400:
return FIRFunctionsErrorCodeInvalidArgument;
case401:
return FIRFunctionsErrorCodeUnauthenticated;
case403:
return FIRFunctionsErrorCodePermissionDenied;
case404:
return FIRFunctionsErrorCodeNotFound;
case409:
return FIRFunctionsErrorCodeAborted;
case429:
return FIRFunctionsErrorCodeResourceExhausted;
case499:
return FIRFunctionsErrorCodeCancelled;
case500:
return FIRFunctionsErrorCodeInternal;
case501:
return FIRFunctionsErrorCodeUnimplemented;
case503:
return FIRFunctionsErrorCodeUnavailable;
case504:
return FIRFunctionsErrorCodeDeadlineExceeded;
}
return FIRFunctionsErrorCodeInternal;
}
/**
* Takes the name of an error code and returns the enum value for it.
* @param name An error name.
* @return The error code with this name, or FIRFunctionsErrorCodeUnknown if none.
*/
NSNumber *FIRFunctionsErrorCodeForName(NSString *name) {
staticNSDictionary<NSString *, NSNumber *> *errors;
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
errors = @{
@"OK" : @(FIRFunctionsErrorCodeOK),
@"CANCELLED" : @(FIRFunctionsErrorCodeCancelled),
@"UNKNOWN" : @(FIRFunctionsErrorCodeUnknown),
@"INVALID_ARGUMENT" : @(FIRFunctionsErrorCodeInvalidArgument),
@"DEADLINE_EXCEEDED" : @(FIRFunctionsErrorCodeDeadlineExceeded),
@"NOT_FOUND" : @(FIRFunctionsErrorCodeNotFound),
@"ALREADY_EXISTS" : @(FIRFunctionsErrorCodeAlreadyExists),
@"PERMISSION_DENIED" : @(FIRFunctionsErrorCodePermissionDenied),
@"RESOURCE_EXHAUSTED" : @(FIRFunctionsErrorCodeResourceExhausted),
@"FAILED_PRECONDITION" : @(FIRFunctionsErrorCodeFailedPrecondition),
@"ABORTED" : @(FIRFunctionsErrorCodeAborted),
@"OUT_OF_RANGE" : @(FIRFunctionsErrorCodeOutOfRange),
@"UNIMPLEMENTED" : @(FIRFunctionsErrorCodeUnimplemented),
@"INTERNAL" : @(FIRFunctionsErrorCodeInternal),
@"UNAVAILABLE" : @(FIRFunctionsErrorCodeUnavailable),
@"DATA_LOSS" : @(FIRFunctionsErrorCodeDataLoss),
@"UNAUTHENTICATED" : @(FIRFunctionsErrorCodeUnauthenticated),
};
});
return errors[name];
}
/**
* Takes a FIRFunctionsErrorCode and returns an English description of it.
* @param code An error code.
* @return A description of the code, or "UNKNOWN" if none.
*/
NSString *FUNDescriptionForErrorCode(FIRFunctionsErrorCode code) {
switch (code) {
case FIRFunctionsErrorCodeOK:
return@"OK";
case FIRFunctionsErrorCodeCancelled:
return@"CANCELLED";
case FIRFunctionsErrorCodeUnknown:
return@"UNKNOWN";
case FIRFunctionsErrorCodeInvalidArgument:
return@"INVALID ARGUMENT";
case FIRFunctionsErrorCodeDeadlineExceeded:
return@"DEADLINE EXCEEDED";
case FIRFunctionsErrorCodeNotFound:
return@"NOT FOUND";
case FIRFunctionsErrorCodeAlreadyExists:
return@"ALREADY EXISTS";
case FIRFunctionsErrorCodePermissionDenied:
return@"PERMISSION DENIED";
case FIRFunctionsErrorCodeResourceExhausted:
return@"RESOURCE EXHAUSTED";
case FIRFunctionsErrorCodeFailedPrecondition:
return@"FAILED PRECONDITION";
case FIRFunctionsErrorCodeAborted:
return@"ABORTED";
case FIRFunctionsErrorCodeOutOfRange:
return@"OUT OF RANGE";
case FIRFunctionsErrorCodeUnimplemented:
return@"UNIMPLEMENTED";
case FIRFunctionsErrorCodeInternal:
return@"INTERNAL";
case FIRFunctionsErrorCodeUnavailable:
return@"UNAVAILABLE";
case FIRFunctionsErrorCodeDataLoss:
return@"DATA LOSS";
case FIRFunctionsErrorCodeUnauthenticated:
return@"UNAUTHENTICATED";
}
return@"UNKNOWN";
}
NSError *_Nullable FUNErrorForCode(FIRFunctionsErrorCode code) {
NSDictionary *userInfo = @{NSLocalizedDescriptionKey : FUNDescriptionForErrorCode(code)};
return [NSErrorerrorWithDomain:FIRFunctionsErrorDomain code:code userInfo:userInfo];
}
NSError *_Nullable FUNErrorForResponse(NSInteger status,
NSData *_Nullable body,
FUNSerializer *serializer) {
// Start with reasonable defaults from the status code.
FIRFunctionsErrorCode code = FIRFunctionsErrorCodeForHTTPStatus(status);
NSString *description = FUNDescriptionForErrorCode(code);
id details = nil;
// Then look through the body for explicit details.
if (body) {
NSError *parseError = nil;
id json = [NSJSONSerializationJSONObjectWithData:body options:0error:&parseError];
if (!parseError && [json isKindOfClass:[NSDictionaryclass]]) {
id errorDetails = json[@"error"];
if ([errorDetails isKindOfClass:[NSDictionaryclass]]) {
if ([errorDetails[@"status"] isKindOfClass:[NSStringclass]]) {
NSNumber *codeNumber = FIRFunctionsErrorCodeForName(errorDetails[@"status"]);
if (codeNumber == nil) {
// If the code in the body is invalid, treat the whole response as malformed.
returnFUNErrorForCode(FIRFunctionsErrorCodeInternal);
}
code = codeNumber.intValue;
// The default description needs to be updated for the new code.
description = FUNDescriptionForErrorCode(code);
}
if ([errorDetails[@"message"] isKindOfClass:[NSStringclass]]) {
description = (NSString *)errorDetails[@"message"];
}
details = errorDetails[@"details"];
if (details) {
NSError *decodeError = nil;
details = [serializer decode:details error:&decodeError];
// Just ignore the details if there an error decoding them.
}
}
}
}
if (code == FIRFunctionsErrorCodeOK) {
// Technically, there's an edge case where a developer could explicitly return an error code of
// OK, and we will treat it as success, but that seems reasonable.
returnnil;
}
NSMutableDictionary *userInfo = [NSMutableDictionarydictionary];
userInfo[NSLocalizedDescriptionKey] = description;
if (details) {
userInfo[FIRFunctionsErrorDetailsKey] = details;
}
return [NSErrorerrorWithDomain:FIRFunctionsErrorDomain code:code userInfo:userInfo];
}
NS_ASSUME_NONNULL_END