Properly report which file failed to be loaded

closes #437
This commit is contained in:
Jason Turner 2018-05-29 07:45:43 -06:00
parent 145acd378b
commit 98c362d038
4 changed files with 25 additions and 4 deletions

View File

@ -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;
};
}

View File

@ -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<parser::ChaiScript_Parser_Base> &&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<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
@ -411,7 +411,11 @@ explicit ChaiScript_Basic(std::unique_ptr<parser::ChaiScript_Parser_Base> &&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
}
}

View File

@ -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");
}

View File

@ -0,0 +1,3 @@
use("totally_missing_file.inc");