Improve setTdlibParameters errors.

This commit is contained in:
levlam 2022-12-30 23:13:40 +03:00
parent 691941ea10
commit b0bfdd2b35
6 changed files with 19 additions and 13 deletions

View File

@ -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

View File

@ -3542,8 +3542,7 @@ void Td::init(Result<TdDb::OpenedDatabase> 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();

View File

@ -133,9 +133,12 @@ Status init_binlog(Binlog &binlog, string path, BinlogKeyValue<Binlog> &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<int>(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<OpenedDatabase>
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 &parameters) {
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();

View File

@ -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<td_api::setTdlibParameters>();
// request->database_encryption_key_ = "!";
request->use_test_dc_ = use_test_dc_;
request->use_message_database_ = true;
request->use_chat_info_database_ = true;

View File

@ -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<int>(Error::WrongPassword), "Wrong password");
}
if ((!db_key_.is_empty() && !db_key_used_) || (db_key_.is_empty() && encryption_type_ != EncryptionType::None)) {

View File

@ -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;