LUA - Handling of io-errors
I want to open a file within lua.
To handle possible errors I use:
file = assert(io.open(name, "r"))
This seems not to work, because the script crashes when an error (e.g. missing file) occurs.
#1 Updated by Jérémy Rosen about 5 years ago
- % Done changed from 0 to 20
- Status changed from New to Incomplete
I can't reproduce on master, could you try with master ?
if you manage to reproduce in master, this means that the bug is only triggered when a lua exception is raised in a certain way. I would probably need your complete luarc file...
#2 Updated by Rudolf Martin about 5 years ago
Here the questionable part of my script:
file = assert(io.open("/home/rudolf/.config/darktable/tagfile.tx", "r")) if not file then dt.print("No file found.") return end
That's the debug output:
LUA ERROR : /home/rudolf/.config/darktable/lua/readtagfromfile.lua:10: /home/rudolf/.config/darktable/tagfile.tx: Datei oder Verzeichnis nicht gefunden stack traceback: [C]: in function 'assert' /home/rudolf/.config/darktable/lua/readtagfromfile.lua:10: in function </home/rudolf/.config/darktable/lua/readtagfromfile.lua:3>
DT didn't crash, only the lua-script aborted. No problems occurs, when the inputfile exist.
Sorry, I don't know what's the master version.
#3 Updated by Jérémy Rosen about 5 years ago
- % Done changed from 20 to 0
- Status changed from Incomplete to Closed: invalid
This is how it's supposed to work.
assert is meant to make lua forcibly crash on error. in your case it checks that the file is not nil and crash if it is nil.
if you don't want to crash, just remove the call to assert and check for nil as you do in the following lines...