diff --git a/.changeset/tricky-papayas-shout.md b/.changeset/tricky-papayas-shout.md new file mode 100644 index 00000000..1e588719 --- /dev/null +++ b/.changeset/tricky-papayas-shout.md @@ -0,0 +1,6 @@ +--- +'@capacitor-firebase/firestore': minor +'@capacitor-firebase/storage': minor +--- + +feat: add the error codes diff --git a/packages/firestore/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/firestore/FirebaseFirestoreHelper.java b/packages/firestore/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/firestore/FirebaseFirestoreHelper.java index a3e18c5c..7f837f2c 100644 --- a/packages/firestore/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/firestore/FirebaseFirestoreHelper.java +++ b/packages/firestore/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/firestore/FirebaseFirestoreHelper.java @@ -5,6 +5,7 @@ import com.getcapacitor.JSArray; import com.getcapacitor.JSObject; import com.google.firebase.firestore.DocumentSnapshot; +import com.google.firebase.firestore.FirebaseFirestoreException; import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryCompositeFilterConstraint; import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryEndAtConstraint; import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryLimitConstraint; @@ -134,4 +135,19 @@ public static JSObject createSnapshotMetadataResult(DocumentSnapshot snapshot) { obj.put("hasPendingWrites", snapshot.getMetadata().hasPendingWrites()); return obj; } + + @Nullable + public static String createErrorCode(@Nullable Exception exception) { + if (exception == null) { + return null; + } else if (exception instanceof FirebaseFirestoreException) { + String errorCode = ((FirebaseFirestoreException) exception).getCode().name(); + return snakeToKebabCase(errorCode); + } + return null; + } + + private static String snakeToKebabCase(String snakeCase) { + return snakeCase.replaceAll("_+", "-").toLowerCase(); + } } diff --git a/packages/firestore/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/firestore/FirebaseFirestorePlugin.java b/packages/firestore/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/firestore/FirebaseFirestorePlugin.java index 32423703..68efacb9 100644 --- a/packages/firestore/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/firestore/FirebaseFirestorePlugin.java +++ b/packages/firestore/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/firestore/FirebaseFirestorePlugin.java @@ -71,14 +71,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.addDocument(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -107,14 +107,14 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.setDocument(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -137,14 +137,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.getDocument(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -172,14 +172,14 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.updateDocument(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -202,14 +202,14 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.deleteDocument(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -232,14 +232,14 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.writeBatch(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -264,14 +264,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.getCollection(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -296,14 +296,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.getCollectionGroup(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -319,13 +319,13 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.clearPersistence(callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -341,7 +341,7 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; @@ -349,7 +349,7 @@ public void error(Exception exception) { call.resolve(); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -365,7 +365,7 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; @@ -373,7 +373,7 @@ public void error(Exception exception) { call.resolve(); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -391,7 +391,7 @@ public void useEmulator(PluginCall call) { call.resolve(); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -454,14 +454,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.addDocumentSnapshotListener(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -498,14 +498,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.addCollectionSnapshotListener(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -542,14 +542,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } }; implementation.addCollectionGroupSnapshotListener(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -570,7 +570,7 @@ public void removeSnapshotListener(PluginCall call) { call.resolve(); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } @@ -589,7 +589,7 @@ public void removeAllListeners(PluginCall call) { super.removeAllListeners(call); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception)); } } } diff --git a/packages/firestore/ios/Plugin/FirebaseFirestoreHelper.swift b/packages/firestore/ios/Plugin/FirebaseFirestoreHelper.swift index cf94c9ab..f900a3cd 100644 --- a/packages/firestore/ios/Plugin/FirebaseFirestoreHelper.swift +++ b/packages/firestore/ios/Plugin/FirebaseFirestoreHelper.swift @@ -69,4 +69,34 @@ public class FirebaseFirestoreHelper { } return value["key"] } + + public static func createErrorCode(error: Error?) -> String? { + if let error = error as NSError? { + return convertErrorCodeToString(errorCode: error.code) + } + return nil + } + + private static func convertErrorCodeToString(errorCode: Int) -> String? { + let errorCodes: [Int: String] = [ + FirestoreErrorCode.aborted.rawValue: "aborted", + FirestoreErrorCode.alreadyExists.rawValue: "already-exists", + FirestoreErrorCode.cancelled.rawValue: "cancelled", + FirestoreErrorCode.dataLoss.rawValue: "data-loss", + FirestoreErrorCode.deadlineExceeded.rawValue: "deadline-exceeded", + FirestoreErrorCode.failedPrecondition.rawValue: "failed-precondition", + FirestoreErrorCode.internal.rawValue: "internal", + FirestoreErrorCode.invalidArgument.rawValue: "invalid-argument", + FirestoreErrorCode.notFound.rawValue: "not-found", + FirestoreErrorCode.outOfRange.rawValue: "out-of-range", + FirestoreErrorCode.permissionDenied.rawValue: "permission-denied", + FirestoreErrorCode.resourceExhausted.rawValue: "resource-exhausted", + FirestoreErrorCode.unauthenticated.rawValue: "unauthenticated", + FirestoreErrorCode.unavailable.rawValue: "unavailable", + FirestoreErrorCode.unimplemented.rawValue: "unimplemented", + FirestoreErrorCode.unknown.rawValue: "unknown" + ] + + return errorCodes[errorCode] + } } diff --git a/packages/firestore/ios/Plugin/FirebaseFirestorePlugin.swift b/packages/firestore/ios/Plugin/FirebaseFirestorePlugin.swift index c2e850d5..dde9c09b 100644 --- a/packages/firestore/ios/Plugin/FirebaseFirestorePlugin.swift +++ b/packages/firestore/ios/Plugin/FirebaseFirestorePlugin.swift @@ -35,7 +35,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.addDocument(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -60,7 +60,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.setDocument(options, completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } call.resolve() @@ -78,7 +78,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.getDocument(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -102,7 +102,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.updateDocument(options, completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } call.resolve() @@ -120,7 +120,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.deleteDocument(options, completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } call.resolve() @@ -138,7 +138,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.writeBatch(options, completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } call.resolve() @@ -158,7 +158,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.getCollection(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -180,7 +180,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.getCollectionGroup(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -193,7 +193,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.clearPersistence(completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } call.resolve() @@ -204,7 +204,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.enableNetwork(completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } call.resolve() @@ -215,7 +215,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.disableNetwork(completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } call.resolve() @@ -273,7 +273,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.addDocumentSnapshotListener(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -305,7 +305,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.addCollectionSnapshotListener(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -314,7 +314,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { }) } catch { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) } } @@ -341,7 +341,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { implementation?.addCollectionGroupSnapshotListener(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -350,7 +350,7 @@ public class FirebaseFirestorePlugin: CAPPlugin { }) } catch { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseFirestoreHelper.createErrorCode(error: error)) } } diff --git a/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/FirebaseStorageHelper.java b/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/FirebaseStorageHelper.java index 4153ab86..21bd62ed 100644 --- a/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/FirebaseStorageHelper.java +++ b/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/FirebaseStorageHelper.java @@ -1,7 +1,18 @@ package io.capawesome.capacitorjs.plugins.firebase.storage; +import static com.google.firebase.storage.StorageException.ERROR_BUCKET_NOT_FOUND; +import static com.google.firebase.storage.StorageException.ERROR_CANCELED; +import static com.google.firebase.storage.StorageException.ERROR_INVALID_CHECKSUM; +import static com.google.firebase.storage.StorageException.ERROR_NOT_AUTHENTICATED; +import static com.google.firebase.storage.StorageException.ERROR_NOT_AUTHORIZED; +import static com.google.firebase.storage.StorageException.ERROR_OBJECT_NOT_FOUND; +import static com.google.firebase.storage.StorageException.ERROR_PROJECT_NOT_FOUND; +import static com.google.firebase.storage.StorageException.ERROR_QUOTA_EXCEEDED; +import static com.google.firebase.storage.StorageException.ERROR_RETRY_LIMIT_EXCEEDED; + import androidx.annotation.Nullable; import com.getcapacitor.JSObject; +import com.google.firebase.storage.StorageException; import com.google.firebase.storage.StorageMetadata; import java.util.Iterator; import org.json.JSONException; @@ -50,4 +61,29 @@ public static StorageMetadata buildStorageMetadata(@Nullable JSObject metadata) return builder.build(); } + + @Nullable + public static String createErrorCode(@Nullable Exception exception) { + if (exception == null) { + return null; + } else if (exception instanceof StorageException) { + return convertErrorCodeToString(((StorageException) exception).getErrorCode()); + } + return null; + } + + private static String convertErrorCodeToString(int code) { + return switch (code) { + case ERROR_BUCKET_NOT_FOUND -> "storage/bucket-not-found"; + case ERROR_CANCELED -> "storage/canceled"; + case ERROR_INVALID_CHECKSUM -> "storage/invalid-checksum"; + case ERROR_NOT_AUTHENTICATED -> "storage/unauthenticated"; + case ERROR_NOT_AUTHORIZED -> "storage/unauthorized"; + case ERROR_OBJECT_NOT_FOUND -> "storage/object-not-found"; + case ERROR_PROJECT_NOT_FOUND -> "storage/project-not-found"; + case ERROR_QUOTA_EXCEEDED -> "storage/quota-exceeded"; + case ERROR_RETRY_LIMIT_EXCEEDED -> "storage/retry-limit-exceeded"; + default -> "storage/unknown"; + }; + } } diff --git a/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/FirebaseStoragePlugin.java b/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/FirebaseStoragePlugin.java index 1a9110fb..2e6ffa49 100644 --- a/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/FirebaseStoragePlugin.java +++ b/packages/storage/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/storage/FirebaseStoragePlugin.java @@ -51,14 +51,14 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } }; implementation.deleteFile(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } } @@ -81,14 +81,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } }; implementation.getDownloadUrl(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } } @@ -111,14 +111,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } }; implementation.getMetadata(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } } @@ -143,14 +143,14 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } }; implementation.listFiles(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } } @@ -178,14 +178,14 @@ public void success() { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } }; implementation.updateMetadata(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } } @@ -217,7 +217,7 @@ public void success(Result result) { @Override public void error(Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } @Override @@ -229,7 +229,7 @@ public void release() { implementation.uploadFile(options, callback); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); + call.reject(exception.getMessage(), FirebaseStorageHelper.createErrorCode(exception)); } } diff --git a/packages/storage/ios/Plugin/FirebaseStorageHelper.swift b/packages/storage/ios/Plugin/FirebaseStorageHelper.swift index 089b2f7c..4a4f4ad9 100644 --- a/packages/storage/ios/Plugin/FirebaseStorageHelper.swift +++ b/packages/storage/ios/Plugin/FirebaseStorageHelper.swift @@ -35,4 +35,33 @@ public class FirebaseStorageHelper { } return map } + + public static func createErrorCode(error: Error?) -> String? { + if let error = error as NSError? { + return convertErrorCodeToString(errorCode: error.code) + } + return nil + } + + private static func convertErrorCodeToString(errorCode: Int) -> String? { + let errorCodes: [Int: String] = [ + StorageErrorCode.bucketMismatch.rawValue: "storage/bucket-mismatch", + StorageErrorCode.bucketNotFound.rawValue: "storage/bucket-not-found", + StorageErrorCode.cancelled.rawValue: "storage/cancelled", + StorageErrorCode.downloadSizeExceeded.rawValue: "storage/download-size-exceeded", + StorageErrorCode.internalError.rawValue: "storage/internal-error", + StorageErrorCode.invalidArgument.rawValue: "storage/invalid-argument", + StorageErrorCode.nonMatchingChecksum.rawValue: "storage/non-matching-checksum", + StorageErrorCode.objectNotFound.rawValue: "storage/object-not-found", + StorageErrorCode.pathError.rawValue: "storage/path-error", + StorageErrorCode.projectNotFound.rawValue: "storage/project-not-found", + StorageErrorCode.quotaExceeded.rawValue: "storage/quota-exceeded", + StorageErrorCode.retryLimitExceeded.rawValue: "storage/retry-limit-exceeded", + StorageErrorCode.unauthenticated.rawValue: "storage/unauthenticated", + StorageErrorCode.unauthorized.rawValue: "storage/unauthorized", + StorageErrorCode.unknown.rawValue: "storage/unknown" + ] + + return errorCodes[errorCode] + } } diff --git a/packages/storage/ios/Plugin/FirebaseStoragePlugin.swift b/packages/storage/ios/Plugin/FirebaseStoragePlugin.swift index 0e8de134..1330b03e 100644 --- a/packages/storage/ios/Plugin/FirebaseStoragePlugin.swift +++ b/packages/storage/ios/Plugin/FirebaseStoragePlugin.swift @@ -31,7 +31,7 @@ public class FirebaseStoragePlugin: CAPPlugin { implementation?.deleteFile(options, completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseStorageHelper.createErrorCode(error: error)) return } call.resolve() @@ -49,7 +49,7 @@ public class FirebaseStoragePlugin: CAPPlugin { implementation?.getDownloadUrl(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseStorageHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -69,7 +69,7 @@ public class FirebaseStoragePlugin: CAPPlugin { implementation?.getMetadata(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseStorageHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -91,7 +91,7 @@ public class FirebaseStoragePlugin: CAPPlugin { implementation?.listFiles(options, completion: { result, error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseStorageHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject { @@ -115,7 +115,7 @@ public class FirebaseStoragePlugin: CAPPlugin { implementation?.updateMetadata(options, completion: { error in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseStorageHelper.createErrorCode(error: error)) return } call.resolve() @@ -148,7 +148,7 @@ public class FirebaseStoragePlugin: CAPPlugin { implementation?.uploadFile(options, completion: { result, error, releaseCall in if let error = error { CAPLog.print("[", self.tag, "] ", error) - call.reject(error.localizedDescription) + call.reject(error.localizedDescription, FirebaseStorageHelper.createErrorCode(error: error)) return } if let result = result?.toJSObject() as? JSObject {