Improved JSON parsing error reporting, see #147

This commit is contained in:
Andrea Bondavalli 2024-01-21 10:27:17 +01:00
parent 67a3b0a2a6
commit 2cad593f3d

View File

@ -346,8 +346,12 @@ Config json_to_config_(std::istream& js, Config& config) {
} catch (boost::property_tree::json_parser::json_parser_error& je) { } catch (boost::property_tree::json_parser::json_parser_error& je) {
throw std::runtime_error("error parsing JSON at line " + throw std::runtime_error("error parsing JSON at line " +
std::to_string(je.line()) + " :" + je.message()); std::to_string(je.line()) + " :" + je.message());
} catch (...) { } catch (std::invalid_argument& e) {
throw std::runtime_error("failed to convert a number"); throw std::runtime_error("error parsing JSON: cannot perform number conversion");
} catch (std::out_of_range& e) {
throw std::runtime_error("error parsing JSON: number conversion out of range");
} catch (std::exception& e) {
throw std::runtime_error("error parsing JSON: " + std::string(e.what()));
} }
return config; return config;
} }
@ -412,8 +416,12 @@ StreamSource json_to_source(const std::string& id, const std::string& json) {
} catch (boost::property_tree::json_parser::json_parser_error& je) { } catch (boost::property_tree::json_parser::json_parser_error& je) {
throw std::runtime_error("error parsing JSON at line " + throw std::runtime_error("error parsing JSON at line " +
std::to_string(je.line()) + " :" + je.message()); std::to_string(je.line()) + " :" + je.message());
} catch (...) { } catch (std::invalid_argument& e) {
throw std::runtime_error("failed to convert a number"); throw std::runtime_error("error parsing JSON: cannot perform number conversion");
} catch (std::out_of_range& e) {
throw std::runtime_error("error parsing JSON: number conversion out of range");
} catch (std::exception& e) {
throw std::runtime_error("error parsing JSON: " + std::string(e.what()));
} }
return source; return source;
} }
@ -452,8 +460,12 @@ StreamSink json_to_sink(const std::string& id, const std::string& json) {
} catch (boost::property_tree::json_parser::json_parser_error& je) { } catch (boost::property_tree::json_parser::json_parser_error& je) {
throw std::runtime_error("error parsing JSON at line " + throw std::runtime_error("error parsing JSON at line " +
std::to_string(je.line()) + " :" + je.message()); std::to_string(je.line()) + " :" + je.message());
} catch (...) { } catch (std::invalid_argument& e) {
throw std::runtime_error("failed to convert a number"); throw std::runtime_error("error parsing JSON: cannot perform number conversion");
} catch (std::out_of_range& e) {
throw std::runtime_error("error parsing JSON: number conversion out of range");
} catch (std::exception& e) {
throw std::runtime_error("error parsing JSON: " + std::string(e.what()));
} }
return sink; return sink;
} }