Skip to content

Commit

Permalink
Show error message if we fail to parse the config JSON (#534)
Browse files Browse the repository at this point in the history
Closes #516
  • Loading branch information
vktr committed Mar 4, 2018
1 parent 3467ee6 commit 89ff713
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lang/1033.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@
"active_downloads": "Active downloads",
"active_seeds": "Active seeds",
"added_on": "Added on",
"completed_on": "Completed on"
"completed_on": "Completed on",
"config_error_s": "Failed to parse configuration file. Default configuration has been loaded.\n---\n%s",
"config_error_title": "Config error"
}
}
17 changes: 16 additions & 1 deletion src/picotorrent/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ bool Application::OnInit()
return false;
}

std::string configurationError;

auto env = std::make_shared<Environment>();
auto cfg = Configuration::Load(env);
auto cfg = Configuration::Load(env, configurationError);
auto translator = Translator::Load(GetModuleHandle(NULL), cfg);

MainFrame* mainFrame = new MainFrame(
Expand Down Expand Up @@ -122,6 +124,19 @@ bool Application::OnInit()
break;
}

if (!configurationError.empty())
{
wxString text = wxString::Format(
i18n(translator, "config_error_s"),
configurationError.c_str());

wxMessageBox(
text,
i18n(translator, "config_error_title"),
wxOK | wxICON_ERROR,
mainFrame);
}

mainFrame->HandleOptions(m_options);

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/picotorrent/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using pt::Configuration;

std::shared_ptr<Configuration> Configuration::Load(std::shared_ptr<pt::Environment> env)
std::shared_ptr<Configuration> Configuration::Load(std::shared_ptr<pt::Environment> env, std::string& error)
{
fs::path path = env->GetApplicationDataPath() / "PicoTorrent.json";

Expand All @@ -21,7 +21,7 @@ std::shared_ptr<Configuration> Configuration::Load(std::shared_ptr<pt::Environme
std::ifstream cfg_stream(path, std::ios::binary);

picojson::value val;
picojson::parse(val, cfg_stream);
error = picojson::parse(val, cfg_stream);

if (!val.is<picojson::object>())
{
Expand Down
2 changes: 1 addition & 1 deletion src/picotorrent/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace pt
using Section::Section;
};

static std::shared_ptr<Configuration> Load(std::shared_ptr<Environment> env);
static std::shared_ptr<Configuration> Load(std::shared_ptr<Environment> env, std::string& error);
static void Save(std::shared_ptr<Environment> env, std::shared_ptr<Configuration> config);

std::shared_ptr<SessionSection> Session();
Expand Down

0 comments on commit 89ff713

Please sign in to comment.