FIRStorageErrors.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseStorage/Sources/FIRStorageErrors.h"
  15. #import "FirebaseStorage/Sources/Public/FIRStorageReference.h"
  16. #import "FirebaseStorage/Sources/FIRStorageConstants_Private.h"
  17. #import "FirebaseStorage/Sources/FIRStorageReference_Private.h"
  18. @implementation FIRStorageErrors
  19. + (NSError *)errorWithCode:(FIRStorageErrorCode)code {
  20. return [FIRStorageErrors errorWithCode:code infoDictionary:nil];
  21. }
  22. + (NSError *)errorWithCode:(FIRStorageErrorCode)code
  23. infoDictionary:(nullable NSDictionary *)dictionary {
  24. NSMutableDictionary *errorDictionary;
  25. if (dictionary) {
  26. errorDictionary = [dictionary mutableCopy];
  27. } else {
  28. errorDictionary = [[NSMutableDictionary alloc] init];
  29. }
  30. NSString *errorMessage;
  31. switch (code) {
  32. case FIRStorageErrorCodeObjectNotFound:
  33. errorMessage =
  34. [NSString stringWithFormat:@"Object %@ does not exist.", errorDictionary[@"object"]];
  35. break;
  36. case FIRStorageErrorCodeBucketNotFound:
  37. errorMessage =
  38. [NSString stringWithFormat:@"Bucket %@ does not exist.", errorDictionary[@"bucket"]];
  39. break;
  40. case FIRStorageErrorCodeProjectNotFound:
  41. errorMessage =
  42. [NSString stringWithFormat:@"Project %@ does not exist.", errorDictionary[@"project"]];
  43. break;
  44. case FIRStorageErrorCodeQuotaExceeded: {
  45. NSString *const kQuotaExceededFormat =
  46. @"Quota for bucket %@ exceeded, please view quota on firebase.google.com.";
  47. errorMessage = [NSString stringWithFormat:kQuotaExceededFormat, errorDictionary[@"bucket"]];
  48. break;
  49. }
  50. case FIRStorageErrorCodeDownloadSizeExceeded: {
  51. int64_t total = [errorDictionary[@"totalSize"] longLongValue];
  52. int64_t size = [errorDictionary[@"maxAllowedSize"] longLongValue];
  53. NSString *totalString = total ? @(total).stringValue : @"unknown";
  54. NSString *sizeString = total ? @(size).stringValue : @"unknown";
  55. NSString *const kSizeExceededErrorFormat =
  56. @"Attempted to download object with size of %@ bytes, "
  57. @"which exceeds the maximum size of %@ bytes. "
  58. @"Consider raising the maximum download size, or using "
  59. @"[FIRStorageReference writeToFile:]";
  60. errorMessage = [NSString stringWithFormat:kSizeExceededErrorFormat, totalString, sizeString];
  61. break;
  62. }
  63. case FIRStorageErrorCodeUnauthenticated:
  64. errorMessage = @"User is not authenticated, please authenticate using Firebase "
  65. @"Authentication and try again.";
  66. break;
  67. case FIRStorageErrorCodeUnauthorized: {
  68. NSString *bucket = errorDictionary[@"bucket"];
  69. NSString *object = errorDictionary[@"object"];
  70. NSString *const kUnauthorizedFormat = @"User does not have permission to access gs://%@/%@.";
  71. errorMessage = [NSString stringWithFormat:kUnauthorizedFormat, bucket, object];
  72. break;
  73. }
  74. case FIRStorageErrorCodeRetryLimitExceeded:
  75. errorMessage = @"Max retry time for operation exceeded, please try again.";
  76. break;
  77. case FIRStorageErrorCodeNonMatchingChecksum: {
  78. // TODO: replace with actual checksum strings when we choose to implement.
  79. NSString *const kChecksumFailedErrorFormat =
  80. @"Uploaded/downloaded object %@ has checksum: %@ "
  81. @"which does not match server checksum: %@. Please retry the upload/download.";
  82. errorMessage = [NSString stringWithFormat:kChecksumFailedErrorFormat, @"object",
  83. @"client checksum", @"server checksum"];
  84. break;
  85. }
  86. case FIRStorageErrorCodeCancelled:
  87. errorMessage = @"User cancelled the upload/download.";
  88. break;
  89. case FIRStorageErrorCodeUnknown:
  90. /* Fall through to default case for unknown errors */
  91. default:
  92. errorMessage = @"An unknown error occurred, please check the server response.";
  93. break;
  94. }
  95. errorDictionary[NSLocalizedDescriptionKey] = errorMessage;
  96. NSError *err = [NSError errorWithDomain:FIRStorageErrorDomain code:code userInfo:errorDictionary];
  97. return err;
  98. }
  99. + (nullable NSError *)errorWithServerError:(nullable NSError *)error
  100. reference:(nullable FIRStorageReference *)reference {
  101. if (error == nil) {
  102. return nil;
  103. }
  104. FIRStorageErrorCode errorCode;
  105. switch (error.code) {
  106. case 400:
  107. errorCode = FIRStorageErrorCodeUnknown;
  108. break;
  109. case 401:
  110. errorCode = FIRStorageErrorCodeUnauthenticated;
  111. break;
  112. case 402:
  113. errorCode = FIRStorageErrorCodeQuotaExceeded;
  114. break;
  115. case 403:
  116. errorCode = FIRStorageErrorCodeUnauthorized;
  117. break;
  118. case 404:
  119. errorCode = FIRStorageErrorCodeObjectNotFound;
  120. break;
  121. default:
  122. errorCode = FIRStorageErrorCodeUnknown;
  123. break;
  124. }
  125. NSMutableDictionary *errorDictionary =
  126. [[[NSDictionary alloc] initWithDictionary:error.userInfo] mutableCopy];
  127. errorDictionary[kFIRStorageResponseErrorDomain] = error.domain;
  128. errorDictionary[kFIRStorageResponseErrorCode] = @(error.code);
  129. // Turn raw response into a string
  130. NSData *responseData = errorDictionary[@"data"];
  131. if (responseData) {
  132. NSString *errorString = [[NSString alloc] initWithData:responseData
  133. encoding:NSUTF8StringEncoding];
  134. errorDictionary[kFIRStorageResponseBody] = errorString ?: @"No Response from Server.";
  135. }
  136. errorDictionary[@"bucket"] = reference.path.bucket;
  137. errorDictionary[@"object"] = reference.path.object;
  138. NSError *clientError = [FIRStorageErrors errorWithCode:errorCode infoDictionary:errorDictionary];
  139. return clientError;
  140. }
  141. + (NSError *)errorWithInvalidRequest:(NSData *)request {
  142. NSString *requestString = [[NSString alloc] initWithData:request encoding:NSUTF8StringEncoding];
  143. NSString *invalidDataString =
  144. [NSString stringWithFormat:kFIRStorageInvalidDataFormat, requestString];
  145. NSDictionary *dict;
  146. if (invalidDataString.length > 0) {
  147. dict = @{NSLocalizedFailureReasonErrorKey : invalidDataString};
  148. }
  149. return [FIRStorageErrors errorWithCode:FIRStorageErrorCodeUnknown infoDictionary:dict];
  150. }
  151. + (NSError *)errorWithCustomMessage:(NSString *)errorMessage {
  152. return [NSError errorWithDomain:FIRStorageErrorDomain
  153. code:FIRStorageErrorCodeUnknown
  154. userInfo:@{NSLocalizedDescriptionKey : errorMessage}];
  155. }
  156. @end