Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename error codes #805

Merged
merged 7 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/breezy-hounds-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@capacitor-firebase/authentication': major
'@capacitor-firebase/firestore': major
---

refactor: add prefix to error codes on Android and iOS to be consistent with the Firebase Web SDK
4 changes: 4 additions & 0 deletions packages/authentication/BREAKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ This is a comprehensive list of the breaking changes introduced in the major ver

On **Android**, the `accessToken` and `serverAuthCode` are now only requested when the `scopes` option is set.

### Error codes

Error codes are now prefixed with `auth/` to be consistent with the Firebase Web SDK.

## Version 6.x.x

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.capawesome.capacitorjs.plugins.firebase.authentication;

import static io.capawesome.capacitorjs.plugins.firebase.authentication.FirebaseAuthenticationPlugin.ERROR_CODE_PREFIX;

import androidx.annotation.Nullable;
import com.getcapacitor.JSArray;
import com.getcapacitor.JSObject;
Expand Down Expand Up @@ -68,7 +70,8 @@ public static String createErrorCode(@Nullable Exception exception) {
} else if (exception instanceof FirebaseAuthException) {
String errorCode = ((FirebaseAuthException) exception).getErrorCode();
errorCode = errorCode.replaceFirst("ERROR_", "");
return snakeToKebabCase(errorCode);
String prefixedErrorCode = String.format("%s/%s", ERROR_CODE_PREFIX, errorCode);
return snakeToKebabCase(prefixedErrorCode);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class FirebaseAuthenticationPlugin extends Plugin {

public static final String TAG = "FirebaseAuthentication";
public static final String ERROR_CODE_PREFIX = "auth";
public static final String PHONE_VERIFICATION_COMPLETED_EVENT = "phoneVerificationCompleted";
public static final String PHONE_VERIFICATION_FAILED_EVENT = "phoneVerificationFailed";
public static final String PHONE_CODE_SENT_EVENT = "phoneCodeSent";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public class FirebaseAuthenticationHelper {

public static func createErrorCode(error: Error?) -> String? {
if let error = error as NSError? {
return convertErrorCodeToString(errorCode: error.code)
if let errorCode = convertErrorCodeToString(errorCode: error.code) {
let prefixedErrorCode = "auth/" + errorCode
return prefixedErrorCode
} else {
return nil
}
}
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions packages/firestore/BREAKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ This is a comprehensive list of the breaking changes introduced in the major ver

## Versions

- [Version 7.x.x](#version-7xx)
- [Version 6.x.x](#version-6xx)

## Version 7.x.x

### Error codes

Error codes are now prefixed with `firestore/` to be consistent with the Firebase Web SDK.

## Version 6.x.x

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore;

import static io.capawesome.capacitorjs.plugins.firebase.firestore.FirebaseFirestorePlugin.ERROR_CODE_PREFIX;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.getcapacitor.JSArray;
Expand Down Expand Up @@ -142,7 +144,8 @@ public static String createErrorCode(@Nullable Exception exception) {
return null;
} else if (exception instanceof FirebaseFirestoreException) {
String errorCode = ((FirebaseFirestoreException) exception).getCode().name();
return snakeToKebabCase(errorCode);
String prefixedErrorCode = String.format("%s/%s", ERROR_CODE_PREFIX, errorCode);
return snakeToKebabCase(prefixedErrorCode);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
public class FirebaseFirestorePlugin extends Plugin {

public static final String TAG = "FirebaseFirestore";
public static final String ERROR_CODE_PREFIX = "firestore";
public static final String ERROR_REFERENCE_MISSING = "reference must be provided.";
public static final String ERROR_HOST_MISSING = "host must be provided.";
public static final String ERROR_CALLBACK_ID_MISSING = "callbackId must be provided.";
Expand Down Expand Up @@ -414,14 +415,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.getCountFromServer(options, callback);
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
call.reject(exception.getMessage());
call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception));
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/firestore/ios/Plugin/FirebaseFirestoreHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ public class FirebaseFirestoreHelper {

public static func createErrorCode(error: Error?) -> String? {
if let error = error as NSError? {
return convertErrorCodeToString(errorCode: error.code)
if let errorCode = convertErrorCodeToString(errorCode: error.code) {
let prefixedErrorCode = "firestore/" + errorCode
return prefixedErrorCode
} else {
return nil
}
}
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions packages/firestore/ios/Plugin/FirebaseFirestorePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Capacitor
*/
@objc(FirebaseFirestorePlugin)
public class FirebaseFirestorePlugin: CAPPlugin, CAPBridgedPlugin {
public let identifier = "FirebaseFirestorePlugin"
public let jsName = "FirebaseFirestore"
public let identifier = "FirebaseFirestorePlugin"
public let jsName = "FirebaseFirestore"
public let pluginMethods: [CAPPluginMethod] = [
CAPPluginMethod(name: "addDocument", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setDocument", returnType: CAPPluginReturnPromise),
Expand All @@ -25,8 +25,8 @@ public class FirebaseFirestorePlugin: CAPPlugin, CAPBridgedPlugin {
CAPPluginMethod(name: "addDocumentSnapshotListener", returnType: CAPPluginReturnCallback),
CAPPluginMethod(name: "addCollectionSnapshotListener", returnType: CAPPluginReturnCallback),
CAPPluginMethod(name: "addCollectionGroupSnapshotListener", returnType: CAPPluginReturnCallback),
CAPPluginMethod(name: "removeSnapshotListener", returnType: CAPPluginReturnPromise),
]
CAPPluginMethod(name: "removeSnapshotListener", returnType: CAPPluginReturnPromise)
]
public let tag = "FirebaseFirestore"
public let errorReferenceMissing = "reference must be provided."
public let errorDataMissing = "data must be provided."
Expand Down Expand Up @@ -264,7 +264,7 @@ public class FirebaseFirestorePlugin: CAPPlugin, CAPBridgedPlugin {
implementation?.getCountFromServer(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,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(), FirebaseStorageHelper.createErrorCode(exception));
}
}
}
8 changes: 4 additions & 4 deletions packages/storage/ios/Plugin/FirebaseStoragePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import Capacitor
*/
@objc(FirebaseStoragePlugin)
public class FirebaseStoragePlugin: CAPPlugin, CAPBridgedPlugin {
public let identifier = "FirebaseStoragePlugin"
public let jsName = "FirebaseStorage"
public let identifier = "FirebaseStoragePlugin"
public let jsName = "FirebaseStorage"
public let pluginMethods: [CAPPluginMethod] = [
CAPPluginMethod(name: "deleteFile", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "getDownloadUrl", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "getMetadata", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "listFiles", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "updateMetadata", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "uploadFile", returnType: CAPPluginReturnCallback),
CAPPluginMethod(name: "useEmulator", returnType: CAPPluginReturnPromise),
]
CAPPluginMethod(name: "useEmulator", returnType: CAPPluginReturnPromise)
]
public let tag = "FirebaseFirestore"
public let errorPathMissing = "path must be provided."
public let errorUriMissing = "uri must be provided."
Expand Down