Petr Skocik
Petr Skocik

Reputation: 60163

Lua error handling

I'm new to lua.

I've tried to use http://keplerproject.github.io/luafilesystem/examples.html and it throws an error on inaccessible directories.

This appears to be caused by luaL_error https://github.com/keplerproject/luafilesystem/blob/master/src/lfs.c#L563

How can I catch this error? http://www.tutorialspoint.com/lua/lua_error_handling.htm suggests pcall, however that doesn't stop the script from dying:

pcall(lfs.dir('/etc/passwd')) #this fails to handle the not a directory error

Upvotes: 5

Views: 419

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26794

pcall(lfs.dir('/etc/passwd')) fails because the error is triggered outside of pcall (when the parameter for pcall is calculated). You need to use

local ok, res = pcall(lfs.dir, '/etc/passwd')

Note that the parameters passed to lfs.dir are given to pcall, not to lfs.dir.

Upvotes: 6

Related Questions