Add some try...catch logic around read-only databases so nheko won't crash if the db doesn't exist

This commit is contained in:
Joseph Donofry 2020-10-09 12:32:21 -04:00
parent a9c0684a5a
commit 31c6857f19
No known key found for this signature in database
GPG key ID: E8A1D78EF044B0CB

View file

@ -1580,7 +1580,15 @@ Cache::roomInfo(bool withInvites)
std::string std::string
Cache::getLastEventId(lmdb::txn &txn, const std::string &room_id) 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; lmdb::val indexVal, val;
@ -1626,8 +1634,17 @@ Cache::getTimelineRange(const std::string &room_id)
std::optional<uint64_t> std::optional<uint64_t>
Cache::getTimelineIndex(const std::string &room_id, std::string_view event_id) Cache::getTimelineIndex(const std::string &room_id, std::string_view event_id)
{ {
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
auto orderDb = getMessageToOrderDb(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{event_id.data(), event_id.size()}, val; 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> std::optional<std::string>
Cache::getTimelineEventId(const std::string &room_id, uint64_t index) Cache::getTimelineEventId(const std::string &room_id, uint64_t index)
{ {
auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY);
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{&index, sizeof(index)}, val; lmdb::val indexVal{&index, sizeof(index)}, val;
@ -1658,8 +1683,24 @@ Cache::getTimelineEventId(const std::string &room_id, uint64_t index)
DescInfo DescInfo
Cache::getLastMessageInfo(lmdb::txn &txn, const std::string &room_id) Cache::getLastMessageInfo(lmdb::txn &txn, const std::string &room_id)
{ {
auto orderDb = getOrderToMessageDb(txn, room_id); lmdb::dbi orderDb{0};
auto eventsDb = getEventsDb(txn, room_id); 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) if (orderDb.size(txn) == 0)
return DescInfo{}; return DescInfo{};