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
|
// 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 "FIRStorageErrors.h"
#import "FIRStorageConstants_Private.h"
#import "FIRStorageReference.h"
#import "FIRStorageReference_Private.h"
@implementation FIRStorageErrors
+ (NSError *)errorWithCode:(FIRStorageErrorCode)code {
return [FIRStorageErrors errorWithCode:code infoDictionary:nil];
}
+ (NSError *)errorWithCode:(FIRStorageErrorCode)code
infoDictionary:(nullable NSDictionary *)dictionary {
NSMutableDictionary *errorDictionary;
if (dictionary) {
errorDictionary = [dictionary mutableCopy];
} else {
errorDictionary = [[NSMutableDictionary alloc] init];
}
NSString *errorMessage;
switch (code) {
case FIRStorageErrorCodeObjectNotFound:
errorMessage =
[NSString stringWithFormat:@"Object %@ does not exist.", errorDictionary[@"object"]];
break;
case FIRStorageErrorCodeBucketNotFound:
errorMessage =
[NSString stringWithFormat:@"Bucket %@ does not exist.", errorDictionary[@"bucket"]];
break;
case FIRStorageErrorCodeProjectNotFound:
errorMessage =
[NSString stringWithFormat:@"Project %@ does not exist.", errorDictionary[@"project"]];
break;
case FIRStorageErrorCodeQuotaExceeded: {
NSString *const kQuotaExceededFormat =
@"Quota for bucket %@ exceeded, please view quota on firebase.google.com.";
errorMessage = [NSString stringWithFormat:kQuotaExceededFormat, errorDictionary[@"bucket"]];
break;
}
case FIRStorageErrorCodeDownloadSizeExceeded: {
int64_t total = [errorDictionary[@"totalSize"] longLongValue];
int64_t size = [errorDictionary[@"maxAllowedSize"] longLongValue];
NSString *totalString = total ? @(total).stringValue : @"unknown";
NSString *sizeString = total ? @(size).stringValue : @"unknown";
NSString *const kSizeExceededErrorFormat =
@"Attempeted to download object with size of %@ bytes, "
@"which exceeds the maximum size of %@ bytes. "
@"Consider raising the maximum download size, or using "
@"[FIRStorageReference writeToFile:]";
errorMessage = [NSString stringWithFormat:kSizeExceededErrorFormat, totalString, sizeString];
break;
}
case FIRStorageErrorCodeUnauthenticated:
errorMessage =
@"User is not authenticated, please authenticate using Firebase "
@"Authentication and try again.";
break;
case FIRStorageErrorCodeUnauthorized: {
NSString *bucket = errorDictionary[@"bucket"];
NSString *object = errorDictionary[@"object"];
NSString *const kUnauthorizedFormat = @"User does not have permission to access gs://%@/%@.";
errorMessage = [NSString stringWithFormat:kUnauthorizedFormat, bucket, object];
break;
}
case FIRStorageErrorCodeRetryLimitExceeded:
errorMessage = @"Max retry time for operation exceeded, please try again.";
break;
case FIRStorageErrorCodeNonMatchingChecksum: {
// TODO: replace with actual checksum strings when we choose to implement.
NSString *const kChecksumFailedErrorFormat =
@"Uploaded/downloaded object %@ has checksum: %@ "
@"which does not match server checksum: %@. Please retry the upload/download.";
errorMessage = [NSString stringWithFormat:kChecksumFailedErrorFormat, @"object",
@"client checksum", @"server checksum"];
break;
}
case FIRStorageErrorCodeCancelled:
errorMessage = @"User cancelled the upload/download.";
break;
case FIRStorageErrorCodeUnknown:
/* Fall through to default case for unknown errors */
default:
errorMessage = @"An unknown error occurred, please check the server response.";
break;
}
errorDictionary[NSLocalizedDescriptionKey] = errorMessage;
NSError *err = [NSError errorWithDomain:FIRStorageErrorDomain code:code userInfo:errorDictionary];
return err;
}
+ (nullable NSError *)errorWithServerError:(nullable NSError *)error
reference:(nullable FIRStorageReference *)reference {
if (error == nil) {
return nil;
}
FIRStorageErrorCode errorCode;
switch (error.code) {
case 400:
errorCode = FIRStorageErrorCodeUnknown;
break;
case 401:
errorCode = FIRStorageErrorCodeUnauthenticated;
break;
case 402:
errorCode = FIRStorageErrorCodeQuotaExceeded;
break;
case 403:
errorCode = FIRStorageErrorCodeUnauthorized;
break;
case 404:
errorCode = FIRStorageErrorCodeObjectNotFound;
break;
default:
errorCode = FIRStorageErrorCodeUnknown;
break;
}
NSMutableDictionary *errorDictionary =
[[[NSDictionary alloc] initWithDictionary:error.userInfo] mutableCopy];
errorDictionary[kFIRStorageResponseErrorDomain] = error.domain;
errorDictionary[kFIRStorageResponseErrorCode] = @(error.code);
// Turn raw response into a string
NSData *responseData = errorDictionary[@"data"];
if (responseData) {
NSString *errorString =
[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
errorDictionary[kFIRStorageResponseBody] = errorString ?: @"No Response from Server.";
}
errorDictionary[@"bucket"] = reference.path.bucket;
errorDictionary[@"object"] = reference.path.object;
NSError *clientError = [FIRStorageErrors errorWithCode:errorCode infoDictionary:errorDictionary];
return clientError;
}
@end
|