Improve fatal error message.

This commit is contained in:
levlam 2022-06-30 01:32:32 +03:00
parent cb559c396d
commit 7b8a016a9e
4 changed files with 14 additions and 3 deletions

View File

@ -76,7 +76,9 @@ Status SqliteDb::init(CSlice path, bool allow_creation) {
auto database_stat = stat(path);
if (database_stat.is_error()) {
if (!allow_creation) {
LOG(FATAL) << "Database was deleted during execution and can't be recreated: " << database_stat.error();
bool was_destroyed = detail::RawSqliteDb::was_any_database_destroyed();
auto reason = was_destroyed ? Slice("was corrupted and deleted") : Slice("disappeared");
LOG(FATAL) << "Database " << reason << " during execution and can't be recreated: " << database_stat.error();
}
TRY_STATUS(destroy(path));
}

View File

@ -63,8 +63,6 @@ class SqliteDb {
static Result<SqliteDb> change_key(CSlice path, bool allow_creation, const DbKey &new_db_key,
const DbKey &old_db_key);
Status last_error();
sqlite3 *get_native() const {
return raw_->db();
}

View File

@ -14,9 +14,13 @@
#include "td/utils/port/path.h"
#include "td/utils/port/Stat.h"
#include <atomic>
namespace td {
namespace detail {
static std::atomic<bool> was_database_destroyed{false};
Status RawSqliteDb::last_error(sqlite3 *db, CSlice path) {
return Status::Error(PSLICE() << Slice(sqlite3_errmsg(db)) << " for database \"" << path << '"');
}
@ -36,12 +40,17 @@ Status RawSqliteDb::last_error() {
//If database was corrupted, try to delete it.
auto code = sqlite3_errcode(db_);
if (code == SQLITE_CORRUPT) {
was_database_destroyed.store(true, std::memory_order_relaxed);
destroy(path_).ignore();
}
return last_error(db_, path());
}
bool RawSqliteDb::was_any_database_destroyed() {
return was_database_destroyed.load(std::memory_order_relaxed);
}
RawSqliteDb::~RawSqliteDb() {
auto rc = sqlite3_close(db_);
LOG_IF(FATAL, rc != SQLITE_OK) << last_error(db_, path());

View File

@ -45,6 +45,8 @@ class RawSqliteDb {
Status last_error();
static Status last_error(sqlite3 *db, CSlice path);
static bool was_any_database_destroyed();
bool on_begin() {
begin_cnt_++;
return begin_cnt_ == 1;