Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): keep the rejection backstop from throwing on odd reasons
Yudhi Armyndharis69563ab6c3e7
diff --git a/src/config/process-error-monitor.spec.ts b/src/config/process-error-monitor.spec.tsindex a9b39098d..d4d937216 100644--- a/src/config/process-error-monitor.spec.ts+++ b/src/config/process-error-monitor.spec.ts@@ -141,4 +141,12 @@ describe('registerUnhandledRejectionHandler', () => {expect(() => handler('a bare string')).not.toThrow();expect(errors[0][1]).toContain('a bare string');});++ // String() throws on a null-prototype object; a throw inside this listener would become an uncaught+ // exception and take the process down, which is exactly what the handler exists to prevent.+ it('logs a rejection value that String() cannot convert instead of throwing', () => {+ const { handler, errors } = register();+ expect(() => handler(Object.create(null))).not.toThrow();+ expect(errors).toEqual([['Unhandled promise rejection', '[object Object]']]);+ });});diff --git a/src/config/process-error-monitor.ts b/src/config/process-error-monitor.tsindex d15d2a63b..922b920c4 100644--- a/src/config/process-error-monitor.ts+++ b/src/config/process-error-monitor.ts@@ -35,6 +35,18 @@ interface RejectionLogger extends FatalLogger {*/const PAGE_CONTEXT_LOST_REJECTION = /execution context was destroyed|window\.require is not a function/i;+/**+ * String() throws on a null-prototype object or one whose toString/valueOf throws. Inside a process+ * listener that throw becomes a fresh uncaught exception, so fall back to the `[object Tag]` form.+ */+function safeString(value: unknown): string {+ try {+ return String(value);+ } catch {+ return Object.prototype.toString.call(value);+ }+}+/*** Register an `uncaughtExceptionMonitor` that routes an otherwise-fatal uncaught exception through the* structured logger BEFORE Node's default handling.@@ -81,8 +93,8 @@ export function registerUncaughtExceptionMonitor(logger: FatalLogger): void {*/export function registerUnhandledRejectionHandler(logger: RejectionLogger): void {process.on('unhandledRejection', (reason: unknown) => {- const message = reason instanceof Error ? reason.message : String(reason);- const detail = reason instanceof Error ? reason.stack : String(reason);+ const message = reason instanceof Error ? reason.message : safeString(reason);+ const detail = reason instanceof Error ? reason.stack : safeString(reason);const pageContextLost = PAGE_CONTEXT_LOST_REJECTION.test(message);incrementUnhandledRejections(pageContextLost ? 'page_context_lost' : 'other');if (pageContextLost) {