Improve fatal error message.
This commit is contained in:
parent
cb559c396d
commit
7b8a016a9e
@ -76,7 +76,9 @@ Status SqliteDb::init(CSlice path, bool allow_creation) {
|
|||||||
auto database_stat = stat(path);
|
auto database_stat = stat(path);
|
||||||
if (database_stat.is_error()) {
|
if (database_stat.is_error()) {
|
||||||
if (!allow_creation) {
|
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));
|
TRY_STATUS(destroy(path));
|
||||||
}
|
}
|
||||||
|
@ -63,8 +63,6 @@ class SqliteDb {
|
|||||||
static Result<SqliteDb> change_key(CSlice path, bool allow_creation, const DbKey &new_db_key,
|
static Result<SqliteDb> change_key(CSlice path, bool allow_creation, const DbKey &new_db_key,
|
||||||
const DbKey &old_db_key);
|
const DbKey &old_db_key);
|
||||||
|
|
||||||
Status last_error();
|
|
||||||
|
|
||||||
sqlite3 *get_native() const {
|
sqlite3 *get_native() const {
|
||||||
return raw_->db();
|
return raw_->db();
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,13 @@
|
|||||||
#include "td/utils/port/path.h"
|
#include "td/utils/port/path.h"
|
||||||
#include "td/utils/port/Stat.h"
|
#include "td/utils/port/Stat.h"
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
static std::atomic<bool> was_database_destroyed{false};
|
||||||
|
|
||||||
Status RawSqliteDb::last_error(sqlite3 *db, CSlice path) {
|
Status RawSqliteDb::last_error(sqlite3 *db, CSlice path) {
|
||||||
return Status::Error(PSLICE() << Slice(sqlite3_errmsg(db)) << " for database \"" << 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.
|
//If database was corrupted, try to delete it.
|
||||||
auto code = sqlite3_errcode(db_);
|
auto code = sqlite3_errcode(db_);
|
||||||
if (code == SQLITE_CORRUPT) {
|
if (code == SQLITE_CORRUPT) {
|
||||||
|
was_database_destroyed.store(true, std::memory_order_relaxed);
|
||||||
destroy(path_).ignore();
|
destroy(path_).ignore();
|
||||||
}
|
}
|
||||||
|
|
||||||
return last_error(db_, path());
|
return last_error(db_, path());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool RawSqliteDb::was_any_database_destroyed() {
|
||||||
|
return was_database_destroyed.load(std::memory_order_relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
RawSqliteDb::~RawSqliteDb() {
|
RawSqliteDb::~RawSqliteDb() {
|
||||||
auto rc = sqlite3_close(db_);
|
auto rc = sqlite3_close(db_);
|
||||||
LOG_IF(FATAL, rc != SQLITE_OK) << last_error(db_, path());
|
LOG_IF(FATAL, rc != SQLITE_OK) << last_error(db_, path());
|
||||||
|
@ -45,6 +45,8 @@ class RawSqliteDb {
|
|||||||
Status last_error();
|
Status last_error();
|
||||||
static Status last_error(sqlite3 *db, CSlice path);
|
static Status last_error(sqlite3 *db, CSlice path);
|
||||||
|
|
||||||
|
static bool was_any_database_destroyed();
|
||||||
|
|
||||||
bool on_begin() {
|
bool on_begin() {
|
||||||
begin_cnt_++;
|
begin_cnt_++;
|
||||||
return begin_cnt_ == 1;
|
return begin_cnt_ == 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user