Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): fold Safari and 504 fetch failures into one toast
Yudhi Armyndharis7d68f3851a58
diff --git a/dashboard/src/components/Toast.test.ts b/dashboard/src/components/Toast.test.tsnew file mode 100644index 000000000..0a6c6f439--- /dev/null+++ b/dashboard/src/components/Toast.test.ts@@ -0,0 +1,82 @@+// An unreachable backend fails every in-flight call at once, so the provider collapses those errors+// into one "connection lost" toast. Each browser words a failed fetch differently, and an error that+// merely resembles one must keep its own toast.+import '../test-helpers/register-hooks.ts';+import { test, before, afterEach } from 'node:test';+import assert from 'node:assert/strict';+import { createElement } from 'react';+import type { ToastContextValue } from '../hooks/useToast.ts';++let rtl: typeof import('@testing-library/react');+let ToastProvider: (typeof import('./Toast.tsx'))['ToastProvider'];+let useToast: (typeof import('../hooks/useToast.ts'))['useToast'];++before(async () => {+ const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');+ await installJsdomGlobals();+ const { i18nReady } = await import('../i18n/index.ts');+ await i18nReady;+ rtl = await import('@testing-library/react');+ ({ ToastProvider } = await import('./Toast.tsx'));+ ({ useToast } = await import('../hooks/useToast.ts'));+});++afterEach(() => rtl.cleanup());++/** Raise each [title, message] as an error toast and return the titles on screen. */+function raise(errors: [string, string?][]): string[] {+ let toast!: ToastContextValue;+ function Probe() {+ toast = useToast();+ return null;+ }+ const { container } = rtl.render(createElement(ToastProvider, null, createElement(Probe)));+ rtl.act(() => {+ for (const [title, message] of errors) toast.error(title, message);+ });+ return Array.from(container.querySelectorAll('.toast-title'), el => el.textContent ?? '');+}++const CONNECTION_LOST = 'Server Connection Lost';++test("WebKit's 'Load failed' collapses into one connection-lost toast", () => {+ assert.deepEqual(+ raise([+ ['Could not load chats', 'Load failed'],+ ['Could not load sessions', 'Load failed'],+ ]),+ [CONNECTION_LOST],+ );+ rtl.cleanup();+ assert.deepEqual(raise([['Load failed'], ['Load failed']]), [CONNECTION_LOST]);+});++test('a proxy 504 collapses into one connection-lost toast like 502 and 503', () => {+ assert.deepEqual(+ raise([+ ['Save failed', 'HTTP 504'],+ ['Refresh failed', 'HTTP 504'],+ ]),+ [CONNECTION_LOST],+ );+ rtl.cleanup();+ assert.deepEqual(+ raise([+ ['A', 'HTTP 502'],+ ['B', 'HTTP 503'],+ ]),+ [CONNECTION_LOST],+ );+});++test('an error that only mentions a failed load or a gateway status keeps its own toast', () => {+ const download = 'Failed to download plugin from URL: download failed with status 404';+ const upstream = 'Upstream answered HTTP 502 for the media URL';+ assert.deepEqual(+ raise([+ ['Install failed', download],+ ['Send failed', upstream],+ ]),+ ['Install failed', 'Send failed'],+ );+});diff --git a/dashboard/src/components/Toast.tsx b/dashboard/src/components/Toast.tsxindex e7656aab4..8e66b9c47 100644--- a/dashboard/src/components/Toast.tsx+++ b/dashboard/src/components/Toast.tsx@@ -47,13 +47,19 @@ export function ToastProvider({ children }: ToastProviderProps) {const error = useCallback((title: string, message?: string) => {+ // Browser fetch rejections: Chromium 'Failed to fetch', Firefox 'NetworkError ...', WebKit exactly+ // 'Load failed' (compared whole, so 'download failed ...' keeps its own toast). A proxy answering+ // 502/503/504 with a non-JSON body surfaces as exactly 'HTTP <status>' (see handleErrorResponse).+ const m = message?.toLowerCase().trim() ?? '';+ const tl = title.toLowerCase().trim();const isConnectionError =- message?.toLowerCase().includes('failed to fetch') ||- message?.toLowerCase().includes('networkerror') ||- message?.toLowerCase().includes('http 502') ||- message?.toLowerCase().includes('http 503') ||- title.toLowerCase().includes('failed to fetch') ||- title.toLowerCase().includes('networkerror');+ m.includes('failed to fetch') ||+ m.includes('networkerror') ||+ m === 'load failed' ||+ /^http 50[234]$/.test(m) ||+ tl.includes('failed to fetch') ||+ tl.includes('networkerror') ||+ tl === 'load failed';if (isConnectionError) {// De-dupe on the stable key (not the translated title) so a downed backend shows one toast.diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.tsindex b2ed20777..c8b0d603d 100644--- a/dashboard/src/services/api.ts+++ b/dashboard/src/services/api.ts@@ -705,7 +705,7 @@ async function handleErrorResponse<T>(response: Response): Promise<T> {}}- // On a non-JSON body (e.g. a reverse-proxy 502/503 HTML page) fall through to `HTTP <status>`+ // On a non-JSON body (e.g. a reverse-proxy 502/503/504 HTML page) fall through to `HTTP <status>`// rather than statusText: the status code is what the toast connection-lost de-dup matches on,// and statusText is empty over HTTP/2 anyway.const error = await response.json().catch(() => ({}));