Skip to content

Commit

Permalink
Report missing file errors with their own exception type
Browse files Browse the repository at this point in the history
Reinstantiate the file_not_found_error exception type removed during the
separation of utils::lua from Kyua.  We need this to provide better error
messages to the user when he asks the library to load a non-existent
file.

This change was r3 in Subversion.
  • Loading branch information
jmmv authored and Julio Merino committed Feb 26, 2012
1 parent 34140e9 commit 04bb9d3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
27 changes: 27 additions & 0 deletions exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,30 @@ lutok::api_error::api_function(void) const
{
return _api_function;
}


/// Constructs a new error.
///
/// \param filename_ The file that count not be found.
lutok::file_not_found_error::file_not_found_error(
const std::string& filename_) :
error("File '" + filename_ + "' not found"),
_filename(filename_)
{
}


/// Destructor for the error.
lutok::file_not_found_error::~file_not_found_error(void) throw()
{
}


/// Gets the name of the file that could not be found.
///
/// \return The name of the file.
const std::string&
lutok::file_not_found_error::filename(void) const
{
return _filename;
}
12 changes: 12 additions & 0 deletions exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ class api_error : public error {
};


/// File not found error.
class file_not_found_error : public error {
std::string _filename;

public:
explicit file_not_found_error(const std::string&);
virtual ~file_not_found_error(void) throw();

const std::string& filename(void) const;
};


} // namespace lutok


Expand Down
11 changes: 11 additions & 0 deletions exceptions_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,21 @@ ATF_TEST_CASE_BODY(api_error__from_stack)
}


ATF_TEST_CASE_WITHOUT_HEAD(file_not_found_error);
ATF_TEST_CASE_BODY(file_not_found_error)
{
const lutok::file_not_found_error e("missing-file");
ATF_REQUIRE(std::strcmp("File 'missing-file' not found", e.what()) == 0);
ATF_REQUIRE_EQ("missing-file", e.filename());
}


ATF_INIT_TEST_CASES(tcs)
{
ATF_ADD_TEST_CASE(tcs, error);

ATF_ADD_TEST_CASE(tcs, api_error__explicit);
ATF_ADD_TEST_CASE(tcs, api_error__from_stack);

ATF_ADD_TEST_CASE(tcs, file_not_found_error);
}
2 changes: 1 addition & 1 deletion operations_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ ATF_TEST_CASE_BODY(do_file__not_found)
{
lutok::state state;
stack_balance_checker checker(state);
ATF_REQUIRE_THROW_RE(lutok::error, "Failed to load Lua file 'missing.lua'",
ATF_REQUIRE_THROW_RE(lutok::file_not_found_error, "missing.lua",
lutok::do_file(state, "missing.lua"));
}

Expand Down
7 changes: 7 additions & 0 deletions wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

extern "C" {
#include <unistd.h>
}

#include <cassert>
#include <cstring>

Expand Down Expand Up @@ -360,11 +364,14 @@ lutok::state::is_userdata(const int index)
/// \param file The second parameter to luaL_loadfile.
///
/// \throw api_error If luaL_loadfile returns an error.
/// \throw file_not_found_error If the file cannot be accessed.
///
/// \warning Terminates execution if there is not enough memory.
void
lutok::state::load_file(const std::string& file)
{
if (!::access(file.c_str(), R_OK) == 0)
throw lutok::file_not_found_error(file);
if (luaL_loadfile(_pimpl->lua_state, file.c_str()) != 0)
throw lutok::api_error::from_stack(_pimpl->lua_state, "luaL_loadfile");
}
Expand Down
9 changes: 5 additions & 4 deletions wrap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,12 @@ ATF_TEST_CASE_BODY(state__load_file__api_error)
}


ATF_TEST_CASE_WITHOUT_HEAD(state__load_file__missing);
ATF_TEST_CASE_BODY(state__load_file__missing)
ATF_TEST_CASE_WITHOUT_HEAD(state__load_file__file_not_found_error);
ATF_TEST_CASE_BODY(state__load_file__file_not_found_error)
{
lutok::state state;
REQUIRE_API_ERROR("luaL_loadfile", state.load_file("missing.lua"));
ATF_REQUIRE_THROW_RE(lutok::file_not_found_error, "missing.lua",
state.load_file("missing.lua"));
}


Expand Down Expand Up @@ -1278,7 +1279,7 @@ ATF_INIT_TEST_CASES(tcs)
ATF_ADD_TEST_CASE(tcs, state__is_userdata__explicit);
ATF_ADD_TEST_CASE(tcs, state__load_file__ok);
ATF_ADD_TEST_CASE(tcs, state__load_file__api_error);
ATF_ADD_TEST_CASE(tcs, state__load_file__missing);
ATF_ADD_TEST_CASE(tcs, state__load_file__file_not_found_error);
ATF_ADD_TEST_CASE(tcs, state__load_string__ok);
ATF_ADD_TEST_CASE(tcs, state__load_string__fail);
ATF_ADD_TEST_CASE(tcs, state__new_table);
Expand Down

0 comments on commit 04bb9d3

Please sign in to comment.