Add database path to most database errors.

GitOrigin-RevId: 469033593d39a542d517d328de546d24c76c77b1
This commit is contained in:
levlam 2020-02-19 04:34:55 +03:00
parent e72346cb6f
commit 811c4ed95c
4 changed files with 12 additions and 13 deletions

View File

@ -19,9 +19,9 @@ SqliteConnectionSafe::SqliteConnectionSafe(string path, DbKey key)
if (r_db.is_error()) { if (r_db.is_error()) {
auto r_stat = stat(path); auto r_stat = stat(path);
if (r_stat.is_error()) { if (r_stat.is_error()) {
LOG(FATAL) << "Can't open database " << path << " (" << r_stat.error() << "): " << r_db.error(); LOG(FATAL) << "Can't open database (" << r_stat.error() << "): " << r_db.error();
} else { } else {
LOG(FATAL) << "Can't open database " << path << " of size " << r_stat.ok().size_ << ": " << r_db.error(); LOG(FATAL) << "Can't open database of size " << r_stat.ok().size_ << ": " << r_db.error();
} }
} }
auto db = r_db.move_as_ok(); auto db = r_db.move_as_ok();

View File

@ -63,7 +63,7 @@ Status SqliteDb::init(CSlice path, bool *was_created) {
int rc = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE /*| SQLITE_OPEN_SHAREDCACHE*/, int rc = sqlite3_open_v2(path.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE /*| SQLITE_OPEN_SHAREDCACHE*/,
nullptr); nullptr);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
auto res = Status::Error(PSLICE() << "Failed to open database: " << detail::RawSqliteDb::last_error(db)); auto res = Status::Error(PSLICE() << "Failed to open database: " << detail::RawSqliteDb::last_error(db, path));
sqlite3_close(db); sqlite3_close(db);
return res; return res;
} }
@ -98,7 +98,7 @@ Status SqliteDb::exec(CSlice cmd) {
VLOG(sqlite) << "Finish exec " << tag("query", cmd) << tag("database", raw_->db()); VLOG(sqlite) << "Finish exec " << tag("query", cmd) << tag("database", raw_->db());
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
CHECK(msg != nullptr); CHECK(msg != nullptr);
return Status::Error(PSLICE() << tag("query", cmd) << " failed: " << msg); return Status::Error(PSLICE() << tag("query", cmd) << " to database \"" << raw_->path() << "\" failed: " << msg);
} }
CHECK(msg == nullptr); CHECK(msg == nullptr);
return Status::OK(); return Status::OK();
@ -126,7 +126,7 @@ Result<int32> SqliteDb::user_version() {
TRY_RESULT(get_version_stmt, get_statement("PRAGMA user_version")); TRY_RESULT(get_version_stmt, get_statement("PRAGMA user_version"));
TRY_STATUS(get_version_stmt.step()); TRY_STATUS(get_version_stmt.step());
if (!get_version_stmt.has_row()) { if (!get_version_stmt.has_row()) {
return Status::Error("PRAGMA user_version failed"); return Status::Error(PSLICE() << "PRAGMA user_version failed for database \"" << raw_->path() << '"');
} }
return get_version_stmt.view_int32(0); return get_version_stmt.view_int32(0);
} }
@ -159,7 +159,7 @@ Result<SqliteDb> SqliteDb::open_with_key(CSlice path, const DbKey &db_key) {
TRY_STATUS(db.init(path)); TRY_STATUS(db.init(path));
if (!db_key.is_empty()) { if (!db_key.is_empty()) {
if (db.check_encryption().is_ok()) { if (db.check_encryption().is_ok()) {
return Status::Error("No key is needed"); return Status::Error(PSLICE() << "No key is needed for database \"" << path << '"');
} }
auto key = db_key_to_sqlcipher_key(db_key); auto key = db_key_to_sqlcipher_key(db_key);
TRY_STATUS(db.exec(PSLICE() << "PRAGMA key = " << key)); TRY_STATUS(db.exec(PSLICE() << "PRAGMA key = " << key));

View File

@ -9,7 +9,6 @@
#include "sqlite/sqlite3.h" #include "sqlite/sqlite3.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/format.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/port/path.h" #include "td/utils/port/path.h"
#include "td/utils/port/Stat.h" #include "td/utils/port/Stat.h"
@ -17,8 +16,8 @@
namespace td { namespace td {
namespace detail { namespace detail {
Status RawSqliteDb::last_error(sqlite3 *db) { Status RawSqliteDb::last_error(sqlite3 *db, CSlice path) {
return Status::Error(Slice(sqlite3_errmsg(db))); return Status::Error(PSLICE() << Slice(sqlite3_errmsg(db)) << " for database \"" << path << '"');
} }
Status RawSqliteDb::destroy(Slice path) { Status RawSqliteDb::destroy(Slice path) {
@ -26,7 +25,7 @@ Status RawSqliteDb::destroy(Slice path) {
with_db_path(path, [&](auto path) { with_db_path(path, [&](auto path) {
unlink(path).ignore(); unlink(path).ignore();
if (!stat(path).is_error()) { if (!stat(path).is_error()) {
error = Status::Error(PSLICE() << "Failed to delete " << tag("path", path)); error = Status::Error(PSLICE() << "Failed to delete file \"" << path << '"');
} }
}); });
return error; return error;
@ -39,12 +38,12 @@ Status RawSqliteDb::last_error() {
destroy(path_).ignore(); destroy(path_).ignore();
} }
return last_error(db_); return last_error(db_, path());
} }
RawSqliteDb::~RawSqliteDb() { RawSqliteDb::~RawSqliteDb() {
auto rc = sqlite3_close(db_); auto rc = sqlite3_close(db_);
LOG_IF(FATAL, rc != SQLITE_OK) << last_error(db_); LOG_IF(FATAL, rc != SQLITE_OK) << last_error(db_, path());
} }
} // namespace detail } // namespace detail

View File

@ -42,7 +42,7 @@ class RawSqliteDb {
} }
Status last_error(); Status last_error();
static Status last_error(sqlite3 *db); static Status last_error(sqlite3 *db, CSlice path);
bool on_begin() { bool on_begin() {
begin_cnt_++; begin_cnt_++;