app_lua: manage error cases properly. We don't want memory leaks.