fix: log plugin load errors in gateway
This commit is contained in:
58
src/gateway/server-plugins.test.ts
Normal file
58
src/gateway/server-plugins.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
import type { PluginRegistry } from "../plugins/registry.js";
|
||||
import type { PluginDiagnostic } from "../plugins/types.js";
|
||||
import { loadGatewayPlugins } from "./server-plugins.js";
|
||||
|
||||
const loadClawdbotPlugins = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("../plugins/loader.js", () => ({
|
||||
loadClawdbotPlugins,
|
||||
}));
|
||||
|
||||
const createRegistry = (diagnostics: PluginDiagnostic[]): PluginRegistry => ({
|
||||
plugins: [],
|
||||
tools: [],
|
||||
hooks: [],
|
||||
typedHooks: [],
|
||||
channels: [],
|
||||
providers: [],
|
||||
gatewayHandlers: {},
|
||||
httpHandlers: [],
|
||||
cliRegistrars: [],
|
||||
services: [],
|
||||
diagnostics,
|
||||
});
|
||||
|
||||
describe("loadGatewayPlugins", () => {
|
||||
test("logs plugin errors with details", () => {
|
||||
const diagnostics: PluginDiagnostic[] = [
|
||||
{
|
||||
level: "error",
|
||||
pluginId: "telegram",
|
||||
source: "/tmp/telegram/index.ts",
|
||||
message: "failed to load plugin: boom",
|
||||
},
|
||||
];
|
||||
loadClawdbotPlugins.mockReturnValue(createRegistry(diagnostics));
|
||||
|
||||
const log = {
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
error: vi.fn(),
|
||||
debug: vi.fn(),
|
||||
};
|
||||
|
||||
loadGatewayPlugins({
|
||||
cfg: {},
|
||||
workspaceDir: "/tmp",
|
||||
log,
|
||||
coreGatewayHandlers: {},
|
||||
baseMethods: [],
|
||||
});
|
||||
|
||||
expect(log.error).toHaveBeenCalledWith(
|
||||
"[plugins] failed to load plugin: boom (plugin=telegram, source=/tmp/telegram/index.ts)",
|
||||
);
|
||||
expect(log.warn).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -29,10 +29,19 @@ export function loadGatewayPlugins(params: {
|
||||
const gatewayMethods = Array.from(new Set([...params.baseMethods, ...pluginMethods]));
|
||||
if (pluginRegistry.diagnostics.length > 0) {
|
||||
for (const diag of pluginRegistry.diagnostics) {
|
||||
const details = [
|
||||
diag.pluginId ? `plugin=${diag.pluginId}` : null,
|
||||
diag.source ? `source=${diag.source}` : null,
|
||||
]
|
||||
.filter((entry): entry is string => Boolean(entry))
|
||||
.join(", ");
|
||||
const message = details
|
||||
? `[plugins] ${diag.message} (${details})`
|
||||
: `[plugins] ${diag.message}`;
|
||||
if (diag.level === "error") {
|
||||
params.log.warn(`[plugins] ${diag.message}`);
|
||||
params.log.error(message);
|
||||
} else {
|
||||
params.log.info(`[plugins] ${diag.message}`);
|
||||
params.log.info(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user