From 98c362d0382dee8b5ab05f17f27464aa09434273 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 29 May 2018 07:45:43 -0600 Subject: [PATCH] Properly report which file failed to be loaded closes #437 --- include/chaiscript/language/chaiscript_common.hpp | 5 ++++- include/chaiscript/language/chaiscript_engine.hpp | 10 +++++++--- unittests/failed_deep_include.chai | 11 +++++++++++ unittests/failed_deep_include.inc | 3 +++ 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 unittests/failed_deep_include.chai create mode 100644 unittests/failed_deep_include.inc diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index 5d66bd94..73e5f027 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -480,11 +480,14 @@ namespace chaiscript /// Errors generated when loading a file struct file_not_found_error : std::runtime_error { explicit file_not_found_error(const std::string &t_filename) noexcept - : std::runtime_error("File Not Found: " + t_filename) + : std::runtime_error("File Not Found: " + t_filename), + filename(t_filename) { } file_not_found_error(const file_not_found_error &) = default; ~file_not_found_error() noexcept override = default; + + std::string filename; }; } diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 889955c2..3e6d0d84 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -241,7 +241,7 @@ namespace chaiscript if (skip_bom(infile)) { size-=3; // decrement the BOM size from file size, otherwise we'll get parsing errors - assert(size >=0 ); //and check if there's more text + assert(size >= 0); //and check if there's more text } if (size == std::streampos(0)) @@ -394,8 +394,8 @@ explicit ChaiScript_Basic(std::unique_ptr &&pars { for (const auto &path : m_use_paths) { + const auto appendedpath = path + t_filename; try { - const auto appendedpath = path + t_filename; chaiscript::detail::threading::unique_lock l(m_use_mutex); chaiscript::detail::threading::unique_lock l2(m_mutex); @@ -411,7 +411,11 @@ explicit ChaiScript_Basic(std::unique_ptr &&pars } return retval; // return, we loaded it, or it was already loaded - } catch (const exception::file_not_found_error &) { + } catch (const exception::file_not_found_error &e) { + if (e.filename != appendedpath) { + // a nested file include failed + throw; + } // failed to load, try the next path } } diff --git a/unittests/failed_deep_include.chai b/unittests/failed_deep_include.chai new file mode 100644 index 00000000..a9449bf5 --- /dev/null +++ b/unittests/failed_deep_include.chai @@ -0,0 +1,11 @@ + + +try { + use("failed_deep_include.inc") + assert_true(false); +} catch (e) { + puts("Caught exception while trying to use file"); + assert_equal(e.what(), "File Not Found: totally_missing_file.inc"); +} + + diff --git a/unittests/failed_deep_include.inc b/unittests/failed_deep_include.inc new file mode 100644 index 00000000..50cf7e77 --- /dev/null +++ b/unittests/failed_deep_include.inc @@ -0,0 +1,3 @@ +use("totally_missing_file.inc"); + +