From 31c6857f19e2b923042b05d886a9113c4020e8ee Mon Sep 17 00:00:00 2001 From: Joseph Donofry Date: Fri, 9 Oct 2020 12:32:21 -0400 Subject: [PATCH] Add some try...catch logic around read-only databases so nheko won't crash if the db doesn't exist --- src/Cache.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index 08b6f15..088b6fc 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 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 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{};