From dd38e1f07f65fd63c979430bbcd4fa3fcabb4fe3 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 26 Jul 2011 19:52:47 +0000 Subject: [PATCH] Don't swallow errors when parsing json and make agi::json_util::file actually work Originally committed to SVN as r5507. --- aegisub/libaegisub/common/hotkey.cpp | 20 +---------------- aegisub/libaegisub/common/json.cpp | 23 +++++++++----------- aegisub/libaegisub/include/libaegisub/json.h | 4 ++-- 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/aegisub/libaegisub/common/hotkey.cpp b/aegisub/libaegisub/common/hotkey.cpp index 071225660..6e9c90b7c 100644 --- a/aegisub/libaegisub/common/hotkey.cpp +++ b/aegisub/libaegisub/common/hotkey.cpp @@ -74,25 +74,7 @@ Hotkey::Hotkey(const std::string &file, const std::string &default_config) { LOG_D("hotkey/init") << "Generating hotkeys."; - std::istream *stream; - - try { - stream = agi::io::Open(config_file); - } catch (const acs::AcsNotFound&) { - stream = new std::istringstream(config_default); - } - - - json::UnknownElement hotkey_root; - try { - hotkey_root = agi::json_util::parse(stream); - } catch (...) { - // There's definitely a better way to do this. - delete stream; - stream = new std::istringstream(config_default); - hotkey_root = agi::json_util::parse(stream); - } - + json::UnknownElement hotkey_root = agi::json_util::file(config_file, config_default); json::Object object = hotkey_root; for (json::Object::const_iterator index(object.Begin()); index != object.End(); index++) { diff --git a/aegisub/libaegisub/common/json.cpp b/aegisub/libaegisub/common/json.cpp index c78574ec1..a81145aa1 100644 --- a/aegisub/libaegisub/common/json.cpp +++ b/aegisub/libaegisub/common/json.cpp @@ -27,6 +27,7 @@ #include "libaegisub/access.h" #include "libaegisub/io.h" #include "libaegisub/json.h" +#include "libaegisub/log.h" namespace agi { @@ -38,36 +39,32 @@ json::UnknownElement parse(std::istream *stream) { try { json::Reader::Read(root, *stream); } catch (json::Reader::ParseException& e) { - std::cout << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1 << std::endl << std::endl; + LOG_E("json/parse") << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1; + delete stream; + throw; } catch (json::Exception& e) { - /// @todo Do something better here, maybe print the exact error - std::cout << "json::Exception: " << e.what() << std::endl; + LOG_E("json/parse") << "json::Exception: " << e.what(); + delete stream; + throw; } delete stream; return root; } - -json::UnknownElement file(const std::string file) { +json::UnknownElement file(const std::string &file) { return parse(io::Open(file)); } - -json::UnknownElement file(const std::string file, const std::string &default_config) { - +json::UnknownElement file(const std::string &file, const std::string &default_config) { try { return parse(io::Open(file)); - // We only want to catch this single error as anything else could // reflect a deeper problem. ie, failed i/o, wrong permissions etc. } catch (const acs::AcsNotFound&) { - - std::istringstream stream(default_config); - return parse(&stream); + return parse(new std::istringstream(default_config)); } } - } // namespace json_util } // namespace agi diff --git a/aegisub/libaegisub/include/libaegisub/json.h b/aegisub/libaegisub/include/libaegisub/json.h index 52dcbd172..54224270a 100644 --- a/aegisub/libaegisub/include/libaegisub/json.h +++ b/aegisub/libaegisub/include/libaegisub/json.h @@ -32,13 +32,13 @@ json::UnknownElement parse(std::istream *stream); /// Parse a JSON file. /// @param file Path JSON to file /// @return json::UnknownElement -json::UnknownElement file(const std::string file); +json::UnknownElement file(const std::string &file); /// Parse a json stream, with default handler. /// @param file Path to JSON file. /// @param Default config file to load incase of nonexistent file /// @return json::UnknownElement -json::UnknownElement file(const std::string file, const std::string &default_config); +json::UnknownElement file(const std::string &file, const std::string &default_config); } // namespace json_util