Skip to content

Commit

Permalink
fix: improve error handling for invalid config files #1571
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jan 12, 2025
1 parent b027fcb commit fb7836f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/common/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "except.h"
#include "log.h"
#include "os/filesystem.h"
#include "ptree.h"

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
Expand Down Expand Up @@ -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");
Expand Down
14 changes: 14 additions & 0 deletions src/shell/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <common/env.h>
#include <common/except.h>
#include <common/log.h>
#include <common/ptree.h>

#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
Expand Down Expand Up @@ -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<caspar::msg_info_t>(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.";
Expand Down

0 comments on commit fb7836f

Please sign in to comment.