Don't swallow errors when parsing json and make agi::json_util::file actually work

Originally committed to SVN as r5507.
This commit is contained in:
Thomas Goyne 2011-07-26 19:52:47 +00:00
parent dabcdb2e8b
commit dd38e1f07f
3 changed files with 13 additions and 34 deletions

View file

@ -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++) {

View file

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

View file

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