From fb7836f43e7f699053385fee8eff0cf3444f6d2a Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Sun, 12 Jan 2025 17:00:36 +0000 Subject: [PATCH] fix: improve error handling for invalid config files #1571 --- src/common/env.cpp | 21 ++++++++++++++------- src/shell/main.cpp | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/common/env.cpp b/src/common/env.cpp index 1f9c870bbc..4ab50e4910 100644 --- a/src/common/env.cpp +++ b/src/common/env.cpp @@ -25,6 +25,7 @@ #include "except.h" #include "log.h" #include "os/filesystem.h" +#include "ptree.h" #include #include @@ -85,21 +86,27 @@ void ensure_writable(const std::wstring& folder) void configure(const std::wstring& filename) { - try { - initial = clean_path(boost::filesystem::initial_path().wstring()); + initial = clean_path(boost::filesystem::initial_path().wstring()); - std::wstring fullpath = filename; - if (!boost::filesystem::exists(fullpath)) { - fullpath = initial + L"/" + filename; - } + std::wstring fullpath = filename; + if (!boost::filesystem::exists(fullpath)) { + fullpath = initial + L"/" + filename; + } + if (!boost::filesystem::exists(fullpath)) { + CASPAR_LOG(fatal) << L"### Configuration file " + filename + L" was not found. ###"; + CASPAR_THROW_EXCEPTION(expected_user_error() + << msg_info(L"Configuration file " + fullpath + L" was not found.")); + } + + try { boost::filesystem::wifstream file(fullpath); boost::property_tree::read_xml(file, pt, boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_comments); - auto paths = pt.get_child(L"configuration.paths"); + auto paths = ptree_get_child(pt, L"configuration.paths"); media = clean_path(paths.get(L"media-path", initial + L"/media/")); auto log_path_node = paths.get_child(L"log-path"); diff --git a/src/shell/main.cpp b/src/shell/main.cpp index 5c9168e90a..4abe3ed709 100644 --- a/src/shell/main.cpp +++ b/src/shell/main.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -265,10 +266,23 @@ int main(int argc, char** argv) if (should_wait_for_keypress) wait_for_keypress(); + } catch (caspar::ptree_exception& e) { + auto info = boost::get_error_info(e); + if (info) { + CASPAR_LOG(fatal) << *info << ". Please check the configuration file (" << u8(config_file_name) + << ") for errors."; + } else { + CASPAR_LOG(fatal) << "Please check the configuration file (" << u8(config_file_name) << ") for errors."; + CASPAR_LOG_CURRENT_EXCEPTION(); + } + wait_for_keypress(); } catch (boost::property_tree::file_parser_error& e) { CASPAR_LOG(fatal) << "At " << u8(config_file_name) << ":" << e.line() << ": " << e.message() << ". Please check the configuration file (" << u8(config_file_name) << ") for errors."; wait_for_keypress(); + } catch (expected_user_error&) { + CASPAR_LOG(fatal) << " Please check the configuration file (" << u8(config_file_name) << ") for errors."; + wait_for_keypress(); } catch (user_error&) { CASPAR_LOG_CURRENT_EXCEPTION(); CASPAR_LOG(fatal) << " Please check the configuration file (" << u8(config_file_name) << ") for errors.";