FIRStorageErrors.m 6.7 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 "FIRStorageErrors.h"
  15. #import "FIRStorageConstants_Private.h"
  16. #import "FIRStorageReference.h"
  17. #import "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 =
  65. @"User is not authenticated, please authenticate using Firebase "
  66. @"Authentication and try again.";
  67. break;
  68. case FIRStorageErrorCodeUnauthorized: {
  69. NSString *bucket = errorDictionary[@"bucket"];
  70. NSString *object = errorDictionary[@"object"];
  71. NSString *const kUnauthorizedFormat = @"User does not have permission to access gs://%@/%@.";
  72. errorMessage = [NSString stringWithFormat:kUnauthorizedFormat, bucket, object];
  73. break;
  74. }
  75. case FIRStorageErrorCodeRetryLimitExceeded:
  76. errorMessage = @"Max retry time for operation exceeded, please try again.";
  77. break;
  78. case FIRStorageErrorCodeNonMatchingChecksum: {
  79. // TODO: replace with actual checksum strings when we choose to implement.
  80. NSString *const kChecksumFailedErrorFormat =
  81. @"Uploaded/downloaded object %@ has checksum: %@ "
  82. @"which does not match server checksum: %@. Please retry the upload/download.";
  83. errorMessage = [NSString stringWithFormat:kChecksumFailedErrorFormat, @"object",
  84. @"client checksum", @"server checksum"];
  85. break;
  86. }
  87. case FIRStorageErrorCodeCancelled:
  88. errorMessage = @"User cancelled the upload/download.";
  89. break;
  90. case FIRStorageErrorCodeUnknown:
  91. /* Fall through to default case for unknown errors */
  92. default:
  93. errorMessage = @"An unknown error occurred, please check the server response.";
  94. break;
  95. }
  96. errorDictionary[NSLocalizedDescriptionKey] = errorMessage;
  97. NSError *err = [NSError errorWithDomain:FIRStorageErrorDomain code:code userInfo:errorDictionary];
  98. return err;
  99. }
  100. + (nullable NSError *)errorWithServerError:(nullable NSError *)error
  101. reference:(nullable FIRStorageReference *)reference {
  102. if (error == nil) {
  103. return nil;
  104. }
  105. FIRStorageErrorCode errorCode;
  106. switch (error.code) {
  107. case 400:
  108. errorCode = FIRStorageErrorCodeUnknown;
  109. break;
  110. case 401:
  111. errorCode = FIRStorageErrorCodeUnauthenticated;
  112. break;
  113. case 402:
  114. errorCode = FIRStorageErrorCodeQuotaExceeded;
  115. break;
  116. case 403:
  117. errorCode = FIRStorageErrorCodeUnauthorized;
  118. break;
  119. case 404:
  120. errorCode = FIRStorageErrorCodeObjectNotFound;
  121. break;
  122. default:
  123. errorCode = FIRStorageErrorCodeUnknown;
  124. break;
  125. }
  126. NSMutableDictionary *errorDictionary =
  127. [[[NSDictionary alloc] initWithDictionary:error.userInfo] mutableCopy];
  128. errorDictionary[kFIRStorageResponseErrorDomain] = error.domain;
  129. errorDictionary[kFIRStorageResponseErrorCode] = @(error.code);
  130. // Turn raw response into a string
  131. NSData *responseData = errorDictionary[@"data"];
  132. if (responseData) {
  133. NSString *errorString =
  134. [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  135. errorDictionary[kFIRStorageResponseBody] = errorString ?: @"No Response from Server.";
  136. }
  137. errorDictionary[@"bucket"] = reference.path.bucket;
  138. errorDictionary[@"object"] = reference.path.object;
  139. NSError *clientError = [FIRStorageErrors errorWithCode:errorCode infoDictionary:errorDictionary];
  140. return clientError;
  141. }
  142. + (NSError *)errorWithInvalidRequest:(NSData *)request {
  143. NSString *requestString = [[NSString alloc] initWithData:request encoding:NSUTF8StringEncoding];
  144. NSString *invalidDataString =
  145. [NSString stringWithFormat:kFIRStorageInvalidDataFormat, requestString];
  146. NSDictionary *dict;
  147. if (invalidDataString.length > 0) {
  148. dict = @{NSLocalizedFailureReasonErrorKey : invalidDataString};
  149. }
  150. return [FIRStorageErrors errorWithCode:FIRStorageErrorCodeUnknown infoDictionary:dict];
  151. }
  152. + (NSError *)errorWithCustomMessage:(NSString *)errorMessage {
  153. return [NSError errorWithDomain:FIRStorageErrorDomain
  154. code:FIRStorageErrorCodeUnknown
  155. userInfo:@{NSLocalizedDescriptionKey : errorMessage}];
  156. }
  157. @end