Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yofukashino committed Dec 6, 2024
1 parent 76b88b9 commit ae1caee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
36 changes: 21 additions & 15 deletions src/main/ipc/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,31 @@ ipcMain.handle(
const plugins = await listPlugins();

return plugins.reduce((acc: Record<string, Record<string, string>>, plugin) => {
if (!plugin.manifest.native || disabled.includes(plugin.manifest.id)) return acc;
const nativePath = join(PLUGINS_DIR, plugin.path, plugin.manifest.native);
if (!nativePath.startsWith(`${PLUGINS_DIR}${sep}`)) {
// Ensure file changes are restricted to the base path
throw new Error("Invalid plugin name");
}
try {
if (!plugin.manifest.native || disabled.includes(plugin.manifest.id)) return acc;
const nativePath = join(PLUGINS_DIR, plugin.path, plugin.manifest.native);
if (!nativePath.startsWith(`${PLUGINS_DIR}${sep}`)) {
// Ensure file changes are restricted to the base path
throw new Error("Invalid plugin name");
}

const entries = Object.entries<(...args: unknown[]) => unknown>(require(nativePath));
if (!entries.length) return acc;
const entries = Object.entries<(...args: unknown[]) => unknown>(require(nativePath));
if (!entries.length) return acc;

const mappings: Record<string, string> = {};
const mappings: Record<string, string> = {};

for (const [methodName, method] of entries) {
const key = `Replugged_Plugin_Native_[${plugin.manifest.id}]_${methodName}`;
ipcMain.handle(key, (_, ...args) => method(...args) /* For easy type when importing */);
mappings[methodName] = key;
for (const [methodName, method] of entries) {
const key = `Replugged_Plugin_Native_[${plugin.manifest.id}]_${methodName}`;
ipcMain.handle(key, (_, ...args) => method(...args) /* For easy type when importing */);
mappings[methodName] = key;
}
acc[plugin.manifest.id] = mappings;
return acc;
} catch (e) {
console.error(`Error Loading Plugin Native`, plugin.manifest);
console.error(e);
return acc;
}
acc[plugin.manifest.id] = mappings;
return acc;
}, {});
},
);
Expand Down
5 changes: 4 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ const mapNative = (
const methods = nativeList[pluginId];
const map = {} as Record<string, (...args: unknown[]) => Promise<unknown>>;
for (const methodName in methods) {
map[methodName] = (...args: unknown[]) => ipcRenderer.invoke(methods[methodName], ...args);
map[methodName] = (...args: unknown[]) =>
ipcRenderer.invoke(methods[methodName], ...args).catch((err) => {
throw new Error(err.message.split(": Error: ")[1]);
});
}
pluginNatives[pluginId] = map;
}
Expand Down

0 comments on commit ae1caee

Please sign in to comment.