- Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathStorageException.cs
214 lines (194 loc) · 9.36 KB
/
StorageException.cs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright 2017 Google LLC
*
* 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.
*/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
usingSystem.IO;
usingSystem.Net;
usingFirebase.Storage.Internal;
namespaceFirebase.Storage{
/// <summary>
/// Represents an Exception resulting from an operation on a
/// <see cref="StorageReference" />
/// </summary>
[Serializable]
publicsealedclassStorageException:Exception{
/// <returns>An unknown error has occurred. See the inner exception or
/// <see cref="StorageException.HttpResultCode" /> for more information.
/// </returns>
publicconstintErrorUnknown=-13000;
/// <returns>The specified object could not be found on the server.</returns>
publicconstintErrorObjectNotFound=-13010;
/// <returns>The specified bucket could not be found on the server.</returns>
publicconstintErrorBucketNotFound=-13011;
/// <returns>The specified project could not be found on the server.</returns>
publicconstintErrorProjectNotFound=-13012;
/// <returns>Free Tier quota has been exceeded. Change your pricing plan
/// to avoid this error.</returns>
publicconstintErrorQuotaExceeded=-13013;
/// <returns>The given signin credentials are not valid.</returns>
publicconstintErrorNotAuthenticated=-13020;
/// <returns>The given signin credentials are not allowed to perform this operation.</returns>
publicconstintErrorNotAuthorized=-13021;
/// <returns>The retry timeout was exceeded. Check your network connection
/// or increase the value of one of <see cref="FirebaseStorage.MaxDownloadRetryTime" />
/// <see cref="FirebaseStorage.MaxUploadRetryTime" />
/// or <see cref="FirebaseStorage.MaxOperationRetryTime" />
/// </returns>
publicconstintErrorRetryLimitExceeded=-13030;
/// <returns>There was an error validating the operation due to a checksum failure.</returns>
publicconstintErrorInvalidChecksum=-13031;
/// <returns>The operation was canceled from the client.</returns>
publicconstintErrorCanceled=-13040;
// Maps C++ error codes to StorageException errors and HTTP status code (where appropriate).
// NOTE: The C++ implementation does not forward HTTP status codes to the user visible API.
// Since the Mono implementation exposes HTTP status codes in StorageException, the following
// table adds the HTTP status code for each error where appropriate.
privatestaticreadonlyDictionary<ErrorInternal,Tuple<int,HttpStatusCode>>
errorToStorageErrorAndHttpStatusCode=
newDictionary<ErrorInternal,Tuple<int,HttpStatusCode>>(){
{ErrorInternal.ErrorObjectNotFound,
newTuple<int,HttpStatusCode>(StorageException.ErrorObjectNotFound,
HttpStatusCode.NotFound)},
{ErrorInternal.ErrorBucketNotFound,
newTuple<int,HttpStatusCode>(StorageException.ErrorBucketNotFound,
HttpStatusCode.NotFound)},
{ErrorInternal.ErrorProjectNotFound,
newTuple<int,HttpStatusCode>(StorageException.ErrorProjectNotFound,
HttpStatusCode.NotFound)},
{ErrorInternal.ErrorQuotaExceeded,
newTuple<int,HttpStatusCode>(StorageException.ErrorProjectNotFound,
HttpStatusCode.ServiceUnavailable)},
{ErrorInternal.ErrorUnauthenticated,
newTuple<int,HttpStatusCode>(StorageException.ErrorNotAuthenticated,
HttpStatusCode.Unauthorized)},
{ErrorInternal.ErrorUnauthorized,
newTuple<int,HttpStatusCode>(StorageException.ErrorNotAuthorized,
HttpStatusCode.Unauthorized)},
{ErrorInternal.ErrorRetryLimitExceeded,
newTuple<int,HttpStatusCode>(StorageException.ErrorRetryLimitExceeded,
HttpStatusCode.Conflict)},
{ErrorInternal.ErrorNonMatchingChecksum,
newTuple<int,HttpStatusCode>(StorageException.ErrorInvalidChecksum,
HttpStatusCode.Conflict)},
{ErrorInternal.ErrorDownloadSizeExceeded,
newTuple<int,HttpStatusCode>(StorageException.ErrorUnknown,0)},
{ErrorInternal.ErrorCancelled,
newTuple<int,HttpStatusCode>(StorageException.ErrorCanceled,0)},
};
// Used to construct an exception for an unknown network error.
privatestaticreadonlyTuple<int,HttpStatusCode>unknownError=
newTuple<int,HttpStatusCode>(StorageException.ErrorUnknown,HttpStatusCode.Ambiguous);
internalStorageException(interrorCode,inthttpResultCode,stringerrorMessage)
:base(String.IsNullOrEmpty(errorMessage)?
GetErrorMessageForCode(errorCode):errorMessage){
ErrorCode=errorCode;
HttpResultCode=httpResultCode;
}
/// <summary>
/// Construct a StorageException from an AggregateException class, converting FirebaseException
/// fields if it's present.
/// </summary>
/// <param name="exception">AggregateException to wrap. This accepts an Exception for
/// so that Task.Exception can be passed to the method without casting at the call site.</param>
/// <returns>StorageException instance wrapper.</returns>
internalstaticStorageExceptionCreateFromException(Exceptionexception){
Tuple<int,HttpStatusCode>errorAndStatusCode;
AggregateExceptionaggregateException=(AggregateException)exception;
FirebaseExceptionfirebaseException=null;
stringerrorMessage=null;
foreach(varinnerExceptioninaggregateException.InnerExceptions){
varstorageException=innerExceptionasStorageException;
firebaseException=innerExceptionasFirebaseException;
if(storageException!=null){
returnstorageException;
}elseif(firebaseException!=null){
break;
}
}
// Try to convert the exception to a StorageException.
if(firebaseException==null){
errorMessage=exception.ToString();
errorAndStatusCode=unknownError;
}else{
errorMessage=firebaseException.Message;
if(!errorToStorageErrorAndHttpStatusCode.TryGetValue(
(ErrorInternal)firebaseException.ErrorCode,outerrorAndStatusCode)){
errorAndStatusCode=unknownError;
}
}
inthttpStatusCodeInt=(int)errorAndStatusCode.Item2;
returnnewStorageException(errorAndStatusCode.Item1,httpStatusCodeInt,errorMessage);
}
/// <returns>
/// A code that indicates the type of error that occurred. This value will
/// be one of the set of constants defined on <see cref="StorageException" />.
/// </returns>
publicintErrorCode{get;privateset;}
/// <returns>the Http result code (if one exists) from a network operation.</returns>
publicintHttpResultCode{get;privateset;}
/// <returns>
/// True if this request failed due to a network condition that
/// may be resolved in a future attempt.
/// </returns>
publicboolIsRecoverableException{
get{returnErrorCode==ErrorRetryLimitExceeded;}
}
// TODO(smiles): Since we have error strings baked into the C++ library, determine whether this
// can be removed.
internalstaticstringGetErrorMessageForCode(interrorCode){
switch(errorCode){
caseErrorUnknown:{
return"An unknown error occurred, please check the HTTP result code and inner "
+"exception for server response.";
}
caseErrorObjectNotFound:{
return"Object does not exist at location.";
}
caseErrorBucketNotFound:{
return"Bucket does not exist.";
}
caseErrorProjectNotFound:{
return"Project does not exist.";
}
caseErrorQuotaExceeded:{
return"Quota for bucket exceeded, please view quota on www.firebase.google"
+".com/storage.";
}
caseErrorNotAuthenticated:{
return"User is not authenticated, please authenticate using Firebase "
+"Authentication and try again.";
}
caseErrorNotAuthorized:{
return"User does not have permission to access this object.";
}
caseErrorRetryLimitExceeded:{
return"The operation retry limit has been exceeded.";
}
caseErrorInvalidChecksum:{
return"Object has a checksum which does not match. Please retry the operation.";
}
caseErrorCanceled:{
return"The operation was cancelled.";
}
default:{
return"An unknown error occurred, please check the HTTP result code and inner "
+"exception for server response.";
}
}
}
}
}