Skip to content

Commit d69054c

Browse files
committed
fix: handle plugin loading failures gracefully
Prevent app from crashing when plugins fail to load and show user feedback for each failed plugin while continuing to load other plugins.
1 parent 9669600 commit d69054c

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/lib/loadPlugin.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import actionStack from "./actionStack";
77
export default async function loadPlugin(pluginId, justInstalled = false) {
88
const baseUrl = await helpers.toInternalUri(Url.join(PLUGIN_DIR, pluginId));
99
const cacheFile = Url.join(CACHE_STORAGE, pluginId);
10-
const $script = <script src={Url.join(baseUrl, "main.js")}></script>;
11-
document.head.append($script);
12-
return new Promise((resolve) => {
10+
11+
return new Promise((resolve, reject) => {
12+
const $script = <script src={Url.join(baseUrl, "main.js")}></script>;
13+
14+
$script.onerror = (error) => {
15+
reject(new Error(`Failed to load script for plugin ${pluginId}`));
16+
};
17+
1318
$script.onload = async () => {
1419
const $page = Page("Plugin");
1520
$page.show = () => {
@@ -25,19 +30,23 @@ export default async function loadPlugin(pluginId, justInstalled = false) {
2530
actionStack.remove(pluginId);
2631
};
2732

28-
if (!(await fsOperation(cacheFile).exists())) {
29-
await fsOperation(CACHE_STORAGE).createFile(pluginId);
30-
}
3133
try {
34+
if (!(await fsOperation(cacheFile).exists())) {
35+
await fsOperation(CACHE_STORAGE).createFile(pluginId);
36+
}
37+
3238
await acode.initPlugin(pluginId, baseUrl, $page, {
3339
cacheFileUrl: await helpers.toInternalUri(cacheFile),
3440
cacheFile: fsOperation(cacheFile),
3541
firstInit: justInstalled,
3642
});
43+
44+
resolve();
3745
} catch (error) {
38-
toast(`Error loading plugin ${pluginId}: ${error.message}`);
46+
reject(error);
3947
}
40-
resolve();
4148
};
49+
50+
document.head.append($script);
4251
});
4352
}

src/lib/loadPlugins.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ import loadPlugin from "./loadPlugin";
44

55
export default async function loadPlugins() {
66
const plugins = await fsOperation(PLUGIN_DIR).lsDir();
7-
const promises = [];
7+
const results = [];
88

99
if (plugins.length > 0) {
1010
toast(strings["loading plugins"]);
1111
}
1212

13-
plugins.forEach((pluginDir) => {
14-
promises.push(loadPlugin(Url.basename(pluginDir.url)));
15-
});
16-
const results = await Promise.all(promises);
17-
return results.length;
13+
// Load plugins sequentially to better handle errors
14+
for (const pluginDir of plugins) {
15+
const pluginId = Url.basename(pluginDir.url);
16+
try {
17+
await loadPlugin(pluginId);
18+
results.push(true);
19+
} catch (error) {
20+
window.log("error", `Failed to load plugin: ${pluginId}`);
21+
window.log("error", error);
22+
toast(`Failed to load plugin: ${pluginId}`);
23+
results.push(false);
24+
}
25+
}
26+
27+
return results.filter(Boolean).length;
1828
}

0 commit comments

Comments
 (0)