5

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
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142

1 Answers1

6

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.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56