Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): leave the trace empty when a plugin error has no error

Yudhi Armyndharis18444612941f
diff --git a/src/core/plugins/plugin-capability-context.ts b/src/core/plugins/plugin-capability-context.ts
index 3a38d49c5..70785532e 100644
--- a/src/core/plugins/plugin-capability-context.ts
+++ b/src/core/plugins/plugin-capability-context.ts
@@ -56,6 +56,9 @@ export function dispatchConversationMedia(
}
}
+/** The trace for a plugin's ctx.logger.error: an Error's message, or the text of whatever else was passed. */
+const errorText = (error: unknown): string => (error instanceof Error ? error.message : String(error));
+
/**
* The capability surface a plugin actually touches: ctx.messages, ctx.engine, ctx.net, ctx.storage,
* ctx.conversations, ctx.handover, ctx.mappings — and the gates in front of every one of them.
@@ -229,11 +232,10 @@ export class PluginCapabilityContext {
warn: (message, meta) =>
this.logger.warn(`[${plugin.manifest.id}] ${message}`, { ...meta, pluginId: plugin.manifest.id }),
error: (message, error, meta) =>
- this.logger.error(
- `[${plugin.manifest.id}] ${message}`,
- error instanceof Error ? error.message : String(error),
- { ...meta, pluginId: plugin.manifest.id },
- ),
+ this.logger.error(`[${plugin.manifest.id}] ${message}`, error === undefined ? undefined : errorText(error), {
+ ...meta,
+ pluginId: plugin.manifest.id,
+ }),
};
}
diff --git a/src/core/plugins/plugin-capability-logger.spec.ts b/src/core/plugins/plugin-capability-logger.spec.ts
new file mode 100644
index 000000000..02296559d
--- /dev/null
+++ b/src/core/plugins/plugin-capability-logger.spec.ts
@@ -0,0 +1,54 @@
+import { HookManager } from '../hooks';
+import { PluginCapabilityContext } from './plugin-capability-context';
+import { PluginHostServices } from './plugin-host-services';
+import { PluginStorageService } from './plugin-storage.service';
+import { PluginInstance, PluginManifest, PluginStatus, PluginType } from './plugin.interfaces';
+import { createLogger } from '../../common/services/logger.service';
+
+describe('the logger a plugin gets', () => {
+ const manifest: PluginManifest = {
+ id: 'p1',
+ name: 'P1',
+ version: '1.0.0',
+ type: PluginType.EXTENSION,
+ main: 'index.js',
+ };
+
+ const setup = () => {
+ const host = { error: jest.fn(), log: jest.fn(), warn: jest.fn(), debug: jest.fn() };
+ const context = new PluginCapabilityContext(
+ host as unknown as ReturnType<typeof createLogger>,
+ {} as PluginHostServices,
+ new HookManager(),
+ { createPluginStorage: () => ({}) } as unknown as PluginStorageService,
+ );
+ const plugin = {
+ manifest,
+ status: PluginStatus.ENABLED,
+ config: {},
+ sessionConfig: {},
+ } as unknown as PluginInstance;
+ return { host, logger: context.createPluginContext(plugin).logger };
+ };
+
+ it('passes no trace when an error line has no error, so the line does not carry the text "undefined"', () => {
+ const { host, logger } = setup();
+
+ logger.error('upload failed');
+ logger.error('upload failed', undefined, { sessionId: 's1' });
+
+ expect(host.error).toHaveBeenNthCalledWith(1, '[p1] upload failed', undefined, { pluginId: 'p1' });
+ expect(host.error).toHaveBeenNthCalledWith(2, '[p1] upload failed', undefined, { sessionId: 's1', pluginId: 'p1' });
+ });
+
+ it('passes the message of an Error, and the text of anything else, as the trace', () => {
+ const { host, logger } = setup();
+
+ logger.error('upload failed', new Error('boom'));
+ logger.error('upload failed', 'plain text');
+ logger.error('upload failed', 404);
+ logger.error('upload failed', null);
+
+ expect(host.error.mock.calls.map((call: unknown[]) => call[1])).toEqual(['boom', 'plain text', '404', 'null']);
+ });
+});
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
index 5ff94785b..1e5ce8240 100644
--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts
@@ -756,17 +756,17 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {
loader.capturedOnLog!('error', 'long', { error: 'e'.repeat(10000) });
loader.capturedOnLog!('error', 'nullmeta', null as unknown as Record<string, unknown>);
- expect(errorSpy).toHaveBeenNthCalledWith(1, '[p1] big', 'undefined', {
+ expect(errorSpy).toHaveBeenNthCalledWith(1, '[p1] big', undefined, {
error: 'boom',
metaTruncated: true,
metaLength: 20026,
pluginId: 'p1',
});
- expect(errorSpy).toHaveBeenNthCalledWith(2, '[p1] bigint', 'undefined', { error: 'boom', pluginId: 'p1' });
+ expect(errorSpy).toHaveBeenNthCalledWith(2, '[p1] bigint', undefined, { error: 'boom', pluginId: 'p1' });
const longMeta = errorSpy.mock.calls[2][2] as { error: string; metaTruncated: boolean };
expect(longMeta.metaTruncated).toBe(true);
expect(longMeta.error).toBe(`${'e'.repeat(8192)}…[truncated]`);
- expect(errorSpy).toHaveBeenNthCalledWith(4, '[p1] nullmeta', 'undefined', { pluginId: 'p1' });
+ expect(errorSpy).toHaveBeenNthCalledWith(4, '[p1] nullmeta', undefined, { pluginId: 'p1' });
});
// postMessage keeps shared references shared, so a meta that is small to hold writes out, at every use, as JSON
@@ -806,7 +806,7 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {
loader.capturedOnLog!('error', 'fan-out', { error: 'boom', fan: fanOut(8) });
expect(Date.now() - started).toBeLessThan(1000);
- expect(errorSpy).toHaveBeenCalledWith('[p1] fan-out', 'undefined', {
+ expect(errorSpy).toHaveBeenCalledWith('[p1] fan-out', undefined, {
error: 'boom',
metaTruncated: true,
pluginId: 'p1',