From 2cad593f3df533d8ac03e2f321b04312ddc99e92 Mon Sep 17 00:00:00 2001 From: Andrea Bondavalli Date: Sun, 21 Jan 2024 10:27:17 +0100 Subject: [PATCH] Improved JSON parsing error reporting, see #147 --- daemon/json.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/daemon/json.cpp b/daemon/json.cpp index 3f724db..f4fbde9 100644 --- a/daemon/json.cpp +++ b/daemon/json.cpp @@ -346,8 +346,12 @@ Config json_to_config_(std::istream& js, Config& config) { } catch (boost::property_tree::json_parser::json_parser_error& je) { throw std::runtime_error("error parsing JSON at line " + std::to_string(je.line()) + " :" + je.message()); - } catch (...) { - throw std::runtime_error("failed to convert a number"); + } catch (std::invalid_argument& e) { + 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; } @@ -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) { throw std::runtime_error("error parsing JSON at line " + std::to_string(je.line()) + " :" + je.message()); - } catch (...) { - throw std::runtime_error("failed to convert a number"); + } catch (std::invalid_argument& e) { + 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; } @@ -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) { throw std::runtime_error("error parsing JSON at line " + std::to_string(je.line()) + " :" + je.message()); - } catch (...) { - throw std::runtime_error("failed to convert a number"); + } catch (std::invalid_argument& e) { + 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; }