Skip to content

Commit

Permalink
feat: add firestore & cloud storage error codes (#789)
Browse files Browse the repository at this point in the history
* feat(firestore): add the error code

* feat(storage): add the error code

* add the changeset

* remove the unused method
  • Loading branch information
mamillastre authored Jan 11, 2025
1 parent 5344006 commit ea16204
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 65 deletions.
6 changes: 6 additions & 0 deletions .changeset/tricky-papayas-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@capacitor-firebase/firestore': minor
'@capacitor-firebase/storage': minor
---

feat: add the error codes
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -341,15 +341,15 @@ 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.enableNetwork(callback);
call.resolve();
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
call.reject(exception.getMessage());
call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception));
}
}

Expand All @@ -365,15 +365,15 @@ 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.disableNetwork(callback);
call.resolve();
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
call.reject(exception.getMessage());
call.reject(exception.getMessage(), FirebaseFirestoreHelper.createErrorCode(exception));
}
}

Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}
}
30 changes: 30 additions & 0 deletions packages/firestore/ios/Plugin/FirebaseFirestoreHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
Loading

0 comments on commit ea16204

Please sign in to comment.