Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): keep the error text when a sandboxed log meta cannot cross
Yudhi Armyndharisdf76fbbf33f4
diff --git a/src/core/plugins/sandbox/plugin-worker.integration.spec.ts b/src/core/plugins/sandbox/plugin-worker.integration.spec.tsindex 3d0a81c2b..353dafe4e 100644--- a/src/core/plugins/sandbox/plugin-worker.integration.spec.ts+++ b/src/core/plugins/sandbox/plugin-worker.integration.spec.ts@@ -333,13 +333,18 @@ describe('plugin worker — real worker_threads round-trip (B1)', () => {logs.push({ level, message, meta }),);- await host.load(UNCLONEABLE_LOG_FIXTURE, { pluginId: 'uncloneable-log', config: {} });- await host.runLifecycle('onEnable');+ try {+ await host.load(UNCLONEABLE_LOG_FIXTURE, { pluginId: 'uncloneable-log', config: {} });+ await host.runLifecycle('onEnable');- // A DataCloneError thrown from the timer used to be uncaught in the worker, so the worker exited.- await expect(host.healthCheck(3000)).resolves.toEqual({ healthy: true, message: 'alive' });- expect(logs).toContainEqual({ level: 'warn', message: 'timer log', meta: undefined });- await host.terminate();+ // A DataCloneError thrown from the timer used to be uncaught in the worker, so the worker exited.+ await expect(host.healthCheck(3000)).resolves.toEqual({ healthy: true, message: 'alive' });+ expect(logs).toContainEqual({ level: 'warn', message: 'timer log', meta: undefined });+ // The error reason is a string, so it still crosses when the rest of the meta cannot.+ expect(logs).toContainEqual({ level: 'error', message: 'timer error', meta: { error: 'upstream down' } });+ } finally {+ await host.terminate();+ }});it('delivers healthCheck and onConfigChange to a sandboxed plugin (and refreshes ctx.config)', async () => {diff --git a/src/core/plugins/sandbox/worker-bootstrap.ts b/src/core/plugins/sandbox/worker-bootstrap.tsindex 543a5fc02..6b5d9e41d 100644--- a/src/core/plugins/sandbox/worker-bootstrap.ts+++ b/src/core/plugins/sandbox/worker-bootstrap.ts@@ -43,12 +43,14 @@ const webhookRegistry = new WebhookRegistry(send);const searchRegistry = new WorkerSearchRegistry(send);// A meta the structured clone cannot copy (a function, a fetch Response) makes postMessage throw. From-// a timer that throw is uncaught and kills the worker, so the line goes out again without its meta.+// a timer that throw is uncaught and kills the worker, so the line goes out again without its meta,+// keeping only logger.error's reason, which is always a string.const sendLog = (level: PluginLogLevel, message: string, meta?: Record<string, unknown>): void => {try {send({ kind: 'log', level, message, meta });} catch {- send({ kind: 'log', level, message: String(message) });+ const error = meta?.error;+ send({ kind: 'log', level, message: String(message), meta: typeof error === 'string' ? { error } : undefined });}};diff --git a/test/fixtures/sandbox/uncloneable-log-plugin.cjs b/test/fixtures/sandbox/uncloneable-log-plugin.cjsindex 5ac0af5e5..eb3fb085c 100644--- a/test/fixtures/sandbox/uncloneable-log-plugin.cjs+++ b/test/fixtures/sandbox/uncloneable-log-plugin.cjs@@ -6,6 +6,7 @@ module.exports = class UncloneableLogPlugin {await new Promise(resolve =>setTimeout(() => {ctx.logger.warn('timer log', { body: () => 'not cloneable' });+ ctx.logger.error('timer error', new Error('upstream down'), { body: () => 'not cloneable' });resolve();}, 0),);