fix: try/catch

This commit is contained in:
Richard Ramos 2022-01-12 09:14:36 -04:00
parent 4183069a9f
commit ec330b2ac8
3 changed files with 91 additions and 65 deletions

View File

@ -52,6 +52,9 @@ Accounts::AccountDto Controller::getSelectedAccount()
return acc;
}
}
// TODO: For situations like this, should be better to return a std::optional instead?
return Accounts::AccountDto();
}
void Controller::setSelectedAccountKeyUid(QString keyUid)

View File

@ -68,7 +68,7 @@ void Module::load()
m_view->setModelItems(items);
// set the first account as slected one
// set the first account as selected one
m_controller->setSelectedAccountKeyUid(items[0].getKeyUid());
Module::setSelectedAccount(items[0]);
}

View File

@ -40,7 +40,10 @@ void Service::init()
QVector<AccountDto> Service::openedAccounts()
{
// try
// TODO: if there's an exception, should we return an empty result? or should we look into using
// std::expected or std::optional or boost outcome https://www.boost.org/doc/libs/1_75_0/libs/outcome/doc/html/index.html
try
{
auto response = Backend::Accounts::openAccounts(Constants::applicationPath(Constants::DataDir));
QJsonArray multiAccounts = response.m_result;
QVector<AccountDto> result;
@ -49,8 +52,12 @@ QVector<AccountDto> Service::openedAccounts()
result << toAccountDto(value);
}
return result;
//} catch(const std::exception& e){
// error "error: ", methodName="openedAccounts", errName = e.name, errDesription = e.msg
}
catch(Backend::RpcException& e)
{
qWarning() << "error: methodName=openedAccounts, errDescription=" << e.what();
return QVector<AccountDto>();
}
}
QVector<GeneratedAccountDto> Service::generatedAccounts()
@ -66,30 +73,29 @@ QVector<GeneratedAccountDto> Service::generatedAccounts()
bool Service::setupAccount(QString accountId, QString password)
{
//try:
// TODO: would it make sense to use std::expected or std::optional or boost outcome https://www.boost.org/doc/libs/1_75_0/libs/outcome/doc/html/index.html
try
{
QString installationId(QUuid::createUuid().toString(QUuid::WithoutBraces));
QJsonObject accountData(Service::getAccountDataForAccountId(accountId));
QJsonArray subAccountData(Service::getSubaccountDataForAccountId(accountId));
QJsonObject settings(Service::getAccountSettings(accountId, installationId));
QJsonObject nodeConfig(Service::getDefaultNodeConfig(installationId));
// if(accountDataJson.isNil or subaccountDataJson.isNil or settingsJson.isNil or
// nodeConfigJson.isNil):
//let description = "at least one json object is not prepared well"
//error "error: ", methodName="setupAccount", errDesription = description
//return false
QString hashedPassword(Backend::Utils::hashString(password));
Service::storeDerivedAccounts(accountId, hashedPassword, PATHS);
m_loggedInAccount = Service::saveAccountAndLogin(hashedPassword, accountData, subAccountData, settings, nodeConfig);
m_loggedInAccount =
Service::saveAccountAndLogin(hashedPassword, accountData, subAccountData, settings, nodeConfig);
return Service::getLoggedInAccount().isValid();
//except Exception as e:
// error "error: ", methodName="setupAccount", errName = e.name, errDesription = e.msg
//return false*/
}
catch(exception& e)
{
qWarning() << "error: methodName=setupAccount, errDescription=" << e.what();
return false;
}
}
AccountDto Service::getLoggedInAccount()
@ -121,7 +127,9 @@ bool Service::importMnemonic(QString mnemonic)
QString Service::login(AccountDto account, QString password)
{
//try:
// TODO: would it make sense to use std::expected or std::optional or boost outcome https://www.boost.org/doc/libs/1_75_0/libs/outcome/doc/html/index.html
try
{
QString hashedPassword(Backend::Utils::hashString(password));
QString thumbnailImage;
@ -148,10 +156,12 @@ QString Service::login(AccountDto account, QString password)
m_loggedInAccount = account;
return "";
//except Exception as e:
//error "error: ", methodName="setupAccount", errName = e.name, errDesription = e.msg
//return e.msg
}
catch(exception& e)
{
qWarning() << "error: methodName=login, errDescription=" << e.what();
return e.what();
}
}
void Service::clear()
@ -195,14 +205,19 @@ DerivedAccounts Service::storeDerivedAccounts(QString accountId, QString hashedP
Accounts::AccountDto Service::saveAccountAndLogin(
QString hashedPassword, QJsonObject account, QJsonArray subaccounts, QJsonObject settings, QJsonObject config)
{
//try:
// TODO: would it make sense to use std::expected or std::optional or boost outcome https://www.boost.org/doc/libs/1_75_0/libs/outcome/doc/html/index.html
try
{
auto response = Backend::Accounts::saveAccountAndLogin(hashedPassword, account, subaccounts, settings, config);
m_isFirstTimeAccountLogin = true;
return toAccountDto(account);
// except Exception as e:
// error "error: ", methodName="saveAccountAndLogin", errName = e.name, errDesription = e.msg
}
catch(exception& e)
{
qWarning() << "error: methodName=saveAccountAndLogin, errDescription=" << e.what();
return Accounts::AccountDto();
}
}
QJsonObject Service::prepareAccountJsonObject(const GeneratedAccountDto account)
@ -233,6 +248,9 @@ QJsonObject Service::getAccountDataForAccountId(QString accountId)
return Service::prepareAccountJsonObject(m_importedAccount);
}
}
// TODO: Should we use instead a std::optional?
throw std::runtime_error("account not found");
}
QJsonArray Service::prepareSubaccountJsonObject(GeneratedAccountDto account)
@ -255,7 +273,6 @@ QJsonArray Service::getSubaccountDataForAccountId(QString accountId)
{
foreach(const GeneratedAccountDto& acc, m_generatedAccounts)
{
if(acc.id == accountId)
{
return prepareSubaccountJsonObject(acc);
@ -268,6 +285,9 @@ QJsonArray Service::getSubaccountDataForAccountId(QString accountId)
return prepareSubaccountJsonObject(m_importedAccount);
}
}
// TODO: Should we use instead a std::optional?
throw std::runtime_error("account not found");
}
QString generateSigningPhrase(int count)
@ -326,6 +346,9 @@ QJsonObject Service::getAccountSettings(QString accountId, QString installationI
return Service::prepareAccountSettingsJsonObject(m_importedAccount, installationId);
}
}
// TODO: Should we use instead a std::optional?
throw std::runtime_error("account not found");
}
QJsonArray getNodes(const QJsonObject fleet, QString nodeType)