summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorJoseph Donofry <joedonofry@gmail.com>2020-10-09 12:32:21 -0400
committerJoseph Donofry <joedonofry@gmail.com>2020-10-09 12:32:21 -0400
commit31c6857f19e2b923042b05d886a9113c4020e8ee (patch)
treee1e2cc7b54227076bcb7998f4440ddd370eea802 /src
parentMerge pull request #299 from Nheko-Reborn/qml-linting (diff)
downloadnheko-31c6857f19e2b923042b05d886a9113c4020e8ee.tar.xz
Add some try...catch logic around read-only databases so nheko won't crash if the db doesn't exist
Diffstat (limited to 'src')
-rw-r--r--src/Cache.cpp55
1 files changed, 48 insertions, 7 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp
index 08b6f155..088b6fc6 100644
--- a/src/Cache.cpp
+++ b/src/Cache.cpp
@@ -1580,7 +1580,15 @@ Cache::roomInfo(bool withInvites)
 std::string
 Cache::getLastEventId(lmdb::txn &txn, const std::string &room_id)
 {
-        auto orderDb = getOrderToMessageDb(txn, room_id);
+        lmdb::dbi orderDb{0};
+        try {
+                orderDb = getOrderToMessageDb(txn, room_id);
+        } catch (lmdb::runtime_error &e) {
+                nhlog::db()->error("Can't open db for room '{}', probably doesn't exist yet. ({})",
+                                   room_id,
+                                   e.what());
+                return {};
+        }
 
         lmdb::val indexVal, val;
 
@@ -1626,8 +1634,17 @@ Cache::getTimelineRange(const std::string &room_id)
 std::optional<uint64_t>
 Cache::getTimelineIndex(const std::string &room_id, std::string_view event_id)
 {
-        auto txn     = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
-        auto orderDb = getMessageToOrderDb(txn, room_id);
+        auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
+
+        lmdb::dbi orderDb{0};
+        try {
+                orderDb = getOrderToMessageDb(txn, room_id);
+        } catch (lmdb::runtime_error &e) {
+                nhlog::db()->error("Can't open db for room '{}', probably doesn't exist yet. ({})",
+                                   room_id,
+                                   e.what());
+                return {};
+        }
 
         lmdb::val indexVal{event_id.data(), event_id.size()}, val;
 
@@ -1642,8 +1659,16 @@ Cache::getTimelineIndex(const std::string &room_id, std::string_view event_id)
 std::optional<std::string>
 Cache::getTimelineEventId(const std::string &room_id, uint64_t index)
 {
-        auto txn     = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
-        auto orderDb = getOrderToMessageDb(txn, room_id);
+        auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
+        lmdb::dbi orderDb{0};
+        try {
+                orderDb = getOrderToMessageDb(txn, room_id);
+        } catch (lmdb::runtime_error &e) {
+                nhlog::db()->error("Can't open db for room '{}', probably doesn't exist yet. ({})",
+                                   room_id,
+                                   e.what());
+                return {};
+        }
 
         lmdb::val indexVal{&index, sizeof(index)}, val;
 
@@ -1658,8 +1683,24 @@ Cache::getTimelineEventId(const std::string &room_id, uint64_t index)
 DescInfo
 Cache::getLastMessageInfo(lmdb::txn &txn, const std::string &room_id)
 {
-        auto orderDb  = getOrderToMessageDb(txn, room_id);
-        auto eventsDb = getEventsDb(txn, room_id);
+        lmdb::dbi orderDb{0};
+        try {
+                orderDb = getOrderToMessageDb(txn, room_id);
+        } catch (lmdb::runtime_error &e) {
+                nhlog::db()->error("Can't open db for room '{}', probably doesn't exist yet. ({})",
+                                   room_id,
+                                   e.what());
+                return {};
+        }
+        lmdb::dbi eventsDb{0};
+        try {
+                eventsDb = getEventsDb(txn, room_id);
+        } catch (lmdb::runtime_error &e) {
+                nhlog::db()->error("Can't open db for room '{}', probably doesn't exist yet. ({})",
+                                   room_id,
+                                   e.what());
+                return {};
+        }
         if (orderDb.size(txn) == 0)
                 return DescInfo{};