mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
plugin(fix): catch plugin crash at init time
This commit is contained in:
@@ -326,10 +326,15 @@ function FileManager:init()
|
||||
self.loaded_modules = {}
|
||||
-- koreader plugins
|
||||
for _,plugin_module in ipairs(PluginLoader:loadPlugins()) do
|
||||
logger.info("FM loaded plugin", plugin_module.name, "at", plugin_module.path)
|
||||
if not plugin_module.is_doc_only then
|
||||
local ok, plugin_or_err = PluginLoader:createPluginInstance(
|
||||
plugin_module, { ui = self, })
|
||||
-- Keep references to the modules which do not register into menu.
|
||||
table.insert(self.loaded_modules, plugin_module:new{ ui = self, })
|
||||
if ok then
|
||||
table.insert(self.loaded_modules, plugin_or_err)
|
||||
logger.info("FM loaded plugin", plugin_module.name,
|
||||
"at", plugin_module.path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -319,14 +319,20 @@ function ReaderUI:init()
|
||||
ui = self,
|
||||
})
|
||||
-- koreader plugins
|
||||
for _,plugin_module in ipairs(PluginLoader:loadPlugins()) do
|
||||
logger.info("RD loaded plugin", plugin_module.name, "at", plugin_module.path)
|
||||
self:registerModule(plugin_module.name, plugin_module:new{
|
||||
dialog = self.dialog,
|
||||
view = self.view,
|
||||
ui = self,
|
||||
document = self.document,
|
||||
})
|
||||
for _, plugin_module in ipairs(PluginLoader:loadPlugins()) do
|
||||
local ok, plugin_or_err = PluginLoader:createPluginInstance(
|
||||
plugin_module,
|
||||
{
|
||||
dialog = self.dialog,
|
||||
view = self.view,
|
||||
ui = self,
|
||||
document = self.document,
|
||||
})
|
||||
if ok then
|
||||
self:registerModule(plugin_module.name, plugin_or_err)
|
||||
logger.info("RD loaded plugin", plugin_module.name,
|
||||
"at", plugin_module.path)
|
||||
end
|
||||
end
|
||||
|
||||
-- we only read settings after all the widgets are initialized
|
||||
|
||||
@@ -68,4 +68,14 @@ function PluginLoader:loadPlugins()
|
||||
return self.plugins
|
||||
end
|
||||
|
||||
function PluginLoader:createPluginInstance(plugin, attr)
|
||||
local ok, re = pcall(plugin.new, plugin, attr)
|
||||
if ok then -- re is a plugin instance
|
||||
return ok, re
|
||||
else -- re is the error message
|
||||
logger.err('Failed to initialize', plugin.name, 'plugin: ', re)
|
||||
return nil, re
|
||||
end
|
||||
end
|
||||
|
||||
return PluginLoader
|
||||
|
||||
Reference in New Issue
Block a user