mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 05:51:15 +08:00
fix(agents): guard extension load error formatting
This commit is contained in:
@@ -86,4 +86,34 @@ export default async function(api) {
|
||||
expect(result.extensions).toHaveLength(1);
|
||||
expect(Array.from(result.extensions[0]?.tools.keys() ?? [])).toEqual(["healthy_lookup"]);
|
||||
});
|
||||
|
||||
it("reports hostile extension factory failures without crashing the loader", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "openclaw-extension-load-error-"));
|
||||
tempDirs.push(dir);
|
||||
const extensionPath = join(dir, "extension.ts");
|
||||
await writeFile(
|
||||
extensionPath,
|
||||
`
|
||||
export default async function() {
|
||||
const error = new Error("factory failed");
|
||||
Object.defineProperty(error, "message", {
|
||||
get() {
|
||||
throw new Error("message denied");
|
||||
},
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
`,
|
||||
);
|
||||
|
||||
const result = await loadExtensions([extensionPath], dir);
|
||||
|
||||
expect(result.extensions).toEqual([]);
|
||||
expect(result.errors).toEqual([
|
||||
{
|
||||
path: extensionPath,
|
||||
error: "Failed to load extension: Unknown extension load error",
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -487,6 +487,17 @@ function createExtension(extensionPath: string, resolvedPath: string): Extension
|
||||
};
|
||||
}
|
||||
|
||||
function describeExtensionLoadError(err: unknown): string {
|
||||
try {
|
||||
if (err instanceof Error) {
|
||||
return err.message || err.name || "Unknown extension load error";
|
||||
}
|
||||
return String(err);
|
||||
} catch {
|
||||
return "Unknown extension load error";
|
||||
}
|
||||
}
|
||||
|
||||
async function loadExtension(
|
||||
extensionPath: string,
|
||||
cwd: string,
|
||||
@@ -510,7 +521,7 @@ async function loadExtension(
|
||||
|
||||
return { extension, error: null };
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const message = describeExtensionLoadError(err);
|
||||
return { extension: null, error: `Failed to load extension: ${message}` };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user