From b0bfdd2b35589ca262b46819a3b23757aeec536e Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 30 Dec 2022 23:13:40 +0300 Subject: [PATCH] Improve setTdlibParameters errors. --- td/generate/scheme/td_api.tl | 2 +- td/telegram/Td.cpp | 3 +-- td/telegram/TdDb.cpp | 22 ++++++++++++++-------- td/telegram/cli.cpp | 1 + tddb/td/db/binlog/Binlog.cpp | 2 +- tddb/td/db/binlog/Binlog.h | 2 +- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 10f53a413..8ce34dd37 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -5457,7 +5457,7 @@ getAuthorizationState = AuthorizationState; //@use_test_dc Pass true to use Telegram test environment instead of the production environment //@database_directory The path to the directory for the persistent database; if empty, the current working directory will be used //@files_directory The path to the directory for storing files; if empty, database_directory will be used -//@database_encryption_key Encryption key for the database +//@database_encryption_key Encryption key for the database. If the encryption key is invalid, then an error with code 401 will be returned //@use_file_database Pass true to keep information about downloaded and uploaded files between application restarts //@use_chat_info_database Pass true to keep cache of users, basic groups, supergroups, channels and secret chats between restarts. Implies use_file_database //@use_message_database Pass true to keep cache of chats and messages between restarts. Implies use_chat_info_database diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 9b130d088..9c0050cca 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3542,8 +3542,7 @@ void Td::init(Result r_opened_database) { CHECK(set_parameters_request_id_ != 0); if (r_opened_database.is_error()) { LOG(WARNING) << "Failed to open database: " << r_opened_database.error(); - send_closure(actor_id(this), &Td::send_error, set_parameters_request_id_, - Status::Error(400, r_opened_database.error().message())); + send_closure(actor_id(this), &Td::send_error, set_parameters_request_id_, r_opened_database.move_as_error()); return finish_set_parameters(); } auto events = r_opened_database.move_as_ok(); diff --git a/td/telegram/TdDb.cpp b/td/telegram/TdDb.cpp index 54467bc1c..825161be2 100644 --- a/td/telegram/TdDb.cpp +++ b/td/telegram/TdDb.cpp @@ -133,9 +133,12 @@ Status init_binlog(Binlog &binlog, string path, BinlogKeyValue &binlog_p } }; - auto binlog_info = binlog.init(std::move(path), callback, std::move(key)); - if (binlog_info.is_error()) { - return binlog_info.move_as_error(); + auto init_status = binlog.init(std::move(path), callback, std::move(key)); + if (init_status.is_error()) { + if (init_status.code() == static_cast(Binlog::Error::WrongPassword)) { + return Status::Error(401, "Wrong database encryption key"); + } + return Status::Error(400, init_status.message()); } return Status::OK(); } @@ -482,7 +485,10 @@ void TdDb::open_impl(TdParameters parameters, DbKey key, Promise db->sql_connection_->get().close(); } SqliteDb::destroy(get_sqlite_path(parameters)).ignore(); - TRY_STATUS_PROMISE(promise, db->init_sqlite(parameters, new_sqlite_key, old_sqlite_key, *binlog_pmc)); + init_sqlite_status = db->init_sqlite(parameters, new_sqlite_key, old_sqlite_key, *binlog_pmc); + if (init_sqlite_status.is_error()) { + return promise.set_error(Status::Error(400, init_sqlite_status.message())); + } } if (drop_sqlite_key) { binlog_pmc->erase("sqlite_key"); @@ -544,16 +550,16 @@ Status TdDb::check_parameters(TdParameters ¶meters) { auto r_database_directory = prepare_dir(parameters.database_directory); if (r_database_directory.is_error()) { VLOG(td_init) << "Invalid database_directory"; - return Status::Error(PSLICE() << "Can't init database in the directory \"" << parameters.database_directory - << "\": " << r_database_directory.error()); + return Status::Error(400, PSLICE() << "Can't init database in the directory \"" << parameters.database_directory + << "\": " << r_database_directory.error()); } parameters.database_directory = r_database_directory.move_as_ok(); auto r_files_directory = prepare_dir(parameters.files_directory); if (r_files_directory.is_error()) { VLOG(td_init) << "Invalid files_directory"; - return Status::Error(PSLICE() << "Can't init files directory \"" << parameters.files_directory - << "\": " << r_files_directory.error()); + return Status::Error(400, PSLICE() << "Can't init files directory \"" << parameters.files_directory + << "\": " << r_files_directory.error()); } parameters.files_directory = r_files_directory.move_as_ok(); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index c4fd8c645..efc60e60f 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -403,6 +403,7 @@ class CliClient final : public Actor { switch (authorization_state_->get_id()) { case td_api::authorizationStateWaitTdlibParameters::ID: { auto request = td_api::make_object(); + // request->database_encryption_key_ = "!"; request->use_test_dc_ = use_test_dc_; request->use_message_database_ = true; request->use_chat_info_database_ = true; diff --git a/tddb/td/db/binlog/Binlog.cpp b/tddb/td/db/binlog/Binlog.cpp index 5b14eed69..1ceba7638 100644 --- a/tddb/td/db/binlog/Binlog.cpp +++ b/tddb/td/db/binlog/Binlog.cpp @@ -212,7 +212,7 @@ Status Binlog::init(string path, const Callback &callback, DbKey db_key, DbKey o last_id_ = processor_->last_id(); if (info_.wrong_password) { close().ignore(); - return Status::Error(Error::WrongPassword, "Wrong password"); + return Status::Error(static_cast(Error::WrongPassword), "Wrong password"); } if ((!db_key_.is_empty() && !db_key_used_) || (db_key_.is_empty() && encryption_type_ != EncryptionType::None)) { diff --git a/tddb/td/db/binlog/Binlog.h b/tddb/td/db/binlog/Binlog.h index 88dbacf58..30b4b1dfe 100644 --- a/tddb/td/db/binlog/Binlog.h +++ b/tddb/td/db/binlog/Binlog.h @@ -45,7 +45,7 @@ class BinlogEventsBuffer; class Binlog { public: - enum Error : int { WrongPassword = -1 }; + enum class Error : int { WrongPassword = -1037284 }; Binlog(); Binlog(const Binlog &other) = delete; Binlog &operator=(const Binlog &other) = delete;