From 2b7131cfea335caf292a38195020f558b6cc1fd6 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 19 Jun 2024 18:25:54 +0000 Subject: [PATCH] Show error message if vcmi unable to access data directory instead of silent crash --- client/CMT.cpp | 9 ++++++++- lib/filesystem/CFileInputStream.cpp | 4 +++- lib/filesystem/CFilesystemLoader.cpp | 10 +++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/client/CMT.cpp b/client/CMT.cpp index fc6885136..c54d27e2d 100644 --- a/client/CMT.cpp +++ b/client/CMT.cpp @@ -208,7 +208,14 @@ int main(int argc, char * argv[]) logGlobal->info("The log file will be saved to %s", logPath); // Init filesystem and settings - preinitDLL(::console, false); + try + { + preinitDLL(::console, false); + } + catch (const DataLoadingException & e) + { + handleFatalError(e.what(), true); + } Settings session = settings.write["session"]; auto setSettingBool = [&](std::string key, std::string arg) { diff --git a/lib/filesystem/CFileInputStream.cpp b/lib/filesystem/CFileInputStream.cpp index 8f10d0d36..f777669de 100644 --- a/lib/filesystem/CFileInputStream.cpp +++ b/lib/filesystem/CFileInputStream.cpp @@ -10,6 +10,8 @@ #include "StdInc.h" #include "CFileInputStream.h" +#include "../ExceptionsCommon.h" + VCMI_LIB_NAMESPACE_BEGIN CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 start, si64 size) @@ -18,7 +20,7 @@ CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 st fileStream{file.c_str(), std::ios::in | std::ios::binary} { if (fileStream.fail()) - throw std::runtime_error("File " + file.string() + " isn't available."); + throw DataLoadingException("Failed to open file '" + file.string() + "'. Reason: " + strerror(errno) ); if (dataSize == 0) { diff --git a/lib/filesystem/CFilesystemLoader.cpp b/lib/filesystem/CFilesystemLoader.cpp index 09073660c..8086989e3 100644 --- a/lib/filesystem/CFilesystemLoader.cpp +++ b/lib/filesystem/CFilesystemLoader.cpp @@ -12,14 +12,22 @@ #include "CFileInputStream.h" +#include "../ExceptionsCommon.h" + VCMI_LIB_NAMESPACE_BEGIN CFilesystemLoader::CFilesystemLoader(std::string _mountPoint, boost::filesystem::path baseDirectory, size_t depth, bool initial): baseDirectory(std::move(baseDirectory)), mountPoint(std::move(_mountPoint)), - fileList(listFiles(mountPoint, depth, initial)), recursiveDepth(depth) { + try { + fileList = listFiles(mountPoint, depth, initial); + } + catch (const boost::filesystem::filesystem_error & e) { + throw DataLoadingException("Failed to load content of '" + baseDirectory.string() + "'. Reason: " + e.what()); + } + logGlobal->trace("File system loaded, %d files found", fileList.size()); }