Skip to content

Instantly share code, notes, and snippets.

@vdhanan
Last active March 19, 2024 14:31
Show Gist options
  • Save vdhanan/5e184ca8b51a6c2da75bce2c298a68bd to your computer and use it in GitHub Desktop.
Save vdhanan/5e184ca8b51a6c2da75bce2c298a68bd to your computer and use it in GitHub Desktop.
error handling
diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
index 747e4bae6..c5618795e 100644
--- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
+++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
@@ -5,6 +5,7 @@
#include "DatabaseManager.h"
#include "InternalModules/GlobalDBSingleton.h"
#include "InternalModules/RustPromiseManager.h"
+#include "Logger.h"
#include "NativeModuleUtils.h"
#include "TerminateApp.h"
@@ -589,14 +590,55 @@ jsi::Object parseOLMOneTimeKeys(jsi::Runtime &rt, std::string oneTimeKeysBlob) {
}
jsi::String parseOLMPrekey(jsi::Runtime &rt, std::string prekeyBlob) {
- folly::dynamic parsedPrekey = folly::parseJson(prekeyBlob);
+ Logger::log("Parsing prekey...");
+
+ folly::dynamic parsedPrekey;
+ try {
+ parsedPrekey = folly::parseJson(prekeyBlob);
+ } catch (const folly::json::parse_error &e) {
+ Logger::log("got the parse error " + std::string(e.what()));
+ throw jsi::JSError(
+ rt, "parsing prekey failed with: " + std::string(e.what()));
+ }
+
+ Logger::log("Prekey JSON parsed successfully.");
+
+ folly::dynamic innerObject = parsedPrekey["curve25519"];
+ if (!innerObject.isObject()) {
+ throw jsi::JSError(rt, "parsing prekey failed: inner object malformed");
+ }
+
+ Logger::log("Inner object is valid.");
+
+ if (innerObject.values().begin() == innerObject.values().end()) {
+ throw jsi::JSError(rt, "parsing prekey failed: prekey missing");
+ }
+
+ Logger::log("Prekey found.");
auto prekey = parsedPrekey["curve25519"].values().begin()->asString();
+ Logger::log("got prekey" + prekey);
return jsi::String::createFromUtf8(rt, prekey);
}
rust::String parseOLMPrekey(std::string prekeyBlob) {
- folly::dynamic parsedPrekey = folly::parseJson(prekeyBlob);
+ folly::dynamic parsedPrekey;
+ try {
+ parsedPrekey = folly::parseJson(prekeyBlob);
+ } catch (const folly::json::parse_error &e) {
+ throw new std::runtime_error(
+ "parsing prekey failed with: " + std::string(e.what()));
+ }
+
+ folly::dynamic innerObject = parsedPrekey["curve25519"];
+ if (!innerObject.isObject()) {
+ throw new std::runtime_error(
+ "parsing prekey failed: inner object malformed");
+ }
+
+ if (innerObject.values().begin() == innerObject.values().end()) {
+ throw new std::runtime_error("parsing prekey failed: prekey missing");
+ }
auto prekey = parsedPrekey["curve25519"].values().begin()->asString();
return rust::String(prekey);
@@ -792,8 +834,12 @@ jsi::Value CommCoreModule::validateAndGetPrekeys(jsi::Runtime &rt) {
std::tie(notifPrekey, notifPrekeySignature) =
getNotificationsPrekeyAndSignature();
+ auto letssee = parseOLMPrekey("{\"curve25519\":{}}");
+
} catch (const std::exception &e) {
error = e.what();
+ } catch (const std::runtime_error &e) {
+ error = e.what();
}
this->jsInvoker_->invokeAsync([=, &innerRt]() {
@@ -801,6 +847,13 @@ jsi::Value CommCoreModule::validateAndGetPrekeys(jsi::Runtime &rt) {
promise->reject(error);
return;
}
+ try {
+ auto thiswillfail =
+ parseOLMPrekey(innerRt, "{\"curve25519\":{}}");
+ } catch (const std::exception &e) {
+ promise->reject(e.what());
+ return;
+ }
auto contentPrekeyJSI =
parseOLMPrekey(innerRt, contentPrekey.value());
auto contentPrekeySignatureJSI =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment