From ce0ff0bea99da92c95caddd662df109436f8954f Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 5 Feb 2025 00:23:10 +0100 Subject: [PATCH] catch and print exception string in ui on main screen creation --- src/start_screen.cpp | 20 ++++++++++++++++++-- src/start_screen.hpp | 2 ++ src/tox_client.cpp | 6 +++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/start_screen.cpp b/src/start_screen.cpp index 6163045..d5d30f1 100644 --- a/src/start_screen.cpp +++ b/src/start_screen.cpp @@ -13,6 +13,8 @@ #include #include #include +#include +#include StartScreen::StartScreen(const std::vector& args, SDL_Renderer* renderer, Theme& theme) : _renderer(renderer), _theme(theme) { bool config_loaded {false}; @@ -301,9 +303,23 @@ Screen* StartScreen::render(float, bool&) { } } else { if (ImGui::Button("proceed", {TEXT_PROCEED_WIDTH*1.5f, TEXT_BASE_HEIGHT*1.5f})) { + _error_string.clear(); - auto new_screen = std::make_unique(std::move(_conf), _renderer, _theme, _tox_profile_path, _password, _user_name, queued_plugin_paths); - return new_screen.release(); + try { + auto new_screen = std::make_unique(std::move(_conf), _renderer, _theme, _tox_profile_path, _password, _user_name, queued_plugin_paths); + if (!new_screen) { + throw std::runtime_error("failed to init main screen."); + } + return new_screen.release(); + } catch (const std::exception& e) { + _error_string = std::string{"ToxCore/MainScreen creation failed with: "} + e.what(); + } catch (...) { + _error_string = "ToxCore/MainScreen creation failed with unknown error"; + } + } + + if (!_error_string.empty()) { + ImGui::TextColored({1.f, 0.5f, 0.5f, 1.f}, "%s", _error_string.c_str()); } } diff --git a/src/start_screen.hpp b/src/start_screen.hpp index f427de8..7ccd965 100644 --- a/src/start_screen.hpp +++ b/src/start_screen.hpp @@ -30,6 +30,8 @@ struct StartScreen final : public Screen { std::string _tox_profile_path {"unnamed-tomato.tox"}; std::vector queued_plugin_paths; + std::string _error_string; + StartScreen(void) = delete; StartScreen(const std::vector& args, SDL_Renderer* renderer, Theme& theme); ~StartScreen(void) = default; diff --git a/src/tox_client.cpp b/src/tox_client.cpp index 1d74fa9..48e2ed4 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -55,7 +55,7 @@ ToxClient::ToxClient(std::string_view save_path, std::string_view save_password) profile_data.data(), nullptr // TODO: error checking )) { - throw std::runtime_error("FAILED to decrypt save file!!!!"); + throw std::runtime_error("failed to decrypt save file!"); } eee(_tox_profile_password); } @@ -69,12 +69,12 @@ ToxClient::ToxClient(std::string_view save_path, std::string_view save_password) tox_options_set_experimental_groups_persistence(options, true); - TOX_ERR_NEW err_new; + Tox_Err_New err_new; _tox = tox_new(options, &err_new); tox_options_free(options); if (err_new != TOX_ERR_NEW_OK) { std::cerr << "tox_new failed with error code " << err_new << "\n"; - throw std::runtime_error{"tox failed"}; + throw std::runtime_error{std::string{"toxcore creation failed with '"} + tox_err_new_to_string(err_new) + "'"}; } // no callbacks, use events