plugin loading error handling

This commit is contained in:
2025-10-27 15:53:54 +11:00
parent 2da07762a4
commit 06de2bd52b
3 changed files with 28 additions and 12 deletions

View File

@@ -13,5 +13,6 @@ theme_mappings = {
"sh": "bash", "sh": "bash",
"yaml": "yaml", "yaml": "yaml",
"md": "markdown", "md": "markdown",
"gitignore": "markdown" "gitignore": "markdown",
"lua": "lua"
} }

View File

@@ -14,13 +14,16 @@ class PluginLoader(Window):
icon="⚙️", icon="⚙️",
starting_horizontal="right", starting_horizontal="right",
starting_vertical="bottom", starting_vertical="bottom",
start_open=True start_open=True,
allow_maximize=True
) )
@work @work
async def find_plugins(self): async def find_plugins(self):
log = self.query_one(RichLog) log = self.query_one(RichLog)
no_errors = True
log.write("[b]Setting up LUA runtime..[/]") log.write("[b]Setting up LUA runtime..[/]")
self.lua_runtime = lua51.LuaRuntime() self.lua_runtime = lua51.LuaRuntime()
lua_runtime_stuff = { lua_runtime_stuff = {
@@ -47,14 +50,17 @@ class PluginLoader(Window):
if not os.path.isdir(plugin_folder): if not os.path.isdir(plugin_folder):
log.write(f"[d]Ignoring {plugin_folder} because it is not a folder.[/]") log.write(f"[d]Ignoring {plugin_folder} because it is not a folder.[/]")
no_errors = False
continue continue
if not os.path.isdir(os.path.join(plugin_folder, "lua")): if not os.path.isdir(os.path.join(plugin_folder, "lua")):
log.write(f"[d]Ignoring {plugin_folder} because it has no \"lua\" folder.[/]") log.write(f"[d]Ignoring {plugin_folder} because it has no \"lua\" folder.[/]")
no_errors = False
continue continue
if not os.path.isfile(os.path.join(plugin_folder, "plugin.json")): if not os.path.isfile(os.path.join(plugin_folder, "plugin.json")):
log.write(f"[d]Ignoring {plugin_folder} because it has no plugin.json file.[/]") log.write(f"[d]Ignoring {plugin_folder} because it has no plugin.json file.[/]")
no_errors = False
continue continue
with open(os.path.join(plugin_folder, "plugin.json"), "r") as f: with open(os.path.join(plugin_folder, "plugin.json"), "r") as f:
@@ -66,15 +72,19 @@ class PluginLoader(Window):
plugin_json["dependencies"] plugin_json["dependencies"]
except UnicodeDecodeError: except UnicodeDecodeError:
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is unreadable.[/]") log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is unreadable.[/]")
no_errors = False
continue continue
except json.JSONDecodeError: except json.JSONDecodeError:
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is malformed.[/]") log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is malformed.[/]")
no_errors = False
continue continue
except KeyError as e: except KeyError as e:
log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is missing the field {e}.") log.write(f"[d]Ignoring {plugin_folder} because its plugin.json file is missing the field {e}.")
no_errors = False
continue continue
except Exception as e: except Exception as e:
log.write(f"[d]Ignoring {plugin_folder} because of error: {e}.[/]") log.write(f"[d]Ignoring {plugin_folder} because of error: {e}.[/]")
no_errors = False
continue continue
@@ -93,7 +103,13 @@ class PluginLoader(Window):
sandbox.berry = lua_runtime_stuff sandbox.berry = lua_runtime_stuff
setfenv(0, sandbox) setfenv(0, sandbox)
executed_code = self.lua_runtime.execute(code) try:
executed_code = self.lua_runtime.execute(code)
except lua51.LuaError as e:
log.write(f"[b red]Error in {lua_file_path}: {e}[/]")
self.notify("There was Lua error while loading one your installed plugins. Check the Plugin Loader window for more details.", title="Lua Error", severity="error")
no_errors = False
continue
if executed_code.init: if executed_code.init:
executed_code.init() executed_code.init()
@@ -102,13 +118,14 @@ class PluginLoader(Window):
plugin_paths.append(plugin_folder) plugin_paths.append(plugin_folder)
log.write("\n[d]Window will automatically close in 5 seconds.[/]") if no_errors:
await asyncio.sleep(5.0) log.write("\n[d]Window will automatically close in 5 seconds.[/]")
self.close_window() await asyncio.sleep(5.0)
self.close_window()
async def on_mount(self): async def on_mount(self):
self.find_plugins() self.find_plugins()
def compose(self): def compose(self):
yield RichLog(markup=True, id="plugins-log") yield RichLog(markup=True, id="plugins-log", wrap=True)

View File

@@ -1,10 +1,8 @@
local plugin = {} local plugin = {}
function plugin.init() function plugin.init()
berry.ui.notify("hiiii") berry.ui.notify(tostring(math.random(10)))
end end
function plugin.main() return plugin
end 123
return plugin