{"record":{"id":"d652ce223ff1f8d5","repo":"tonhowtf/omniget","slug":"omnidisc-dispatch-handler-failed","errorCode":null,"errorMessage":"[omnidisc] dispatch handler failed","messagePattern":"\\[omnidisc\\] dispatch handler failed","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/lib/stores/omnidisc-store.svelte.ts","lineNumber":1430,"sourceCode":"  if (mapped.status === \"connected\") settleReady(ev.url, null);\n  else if (mapped.status === \"signed_out\" || mapped.status === \"error\") settleReady(ev.url, mapped.error ?? \"ERR_UNREACHABLE\");\n}\n\nlet unlisteners: UnlistenFn[] = [];\n\nexport async function initOmnidisc(): Promise<void> {\n  ensureLoaded();\n  if (initialized) return;\n  initialized = true;\n  try {\n    const offDispatch = await listen<GatewayDispatchEvent>(\"omnidisc://dispatch\", (event) => {\n      const payload = event.payload;\n      const instance = instanceByUrl(payload.url);\n      if (!instance) return;\n      try {\n        handleDispatch(instance, payload.t, payload.d);\n      } catch (e) {\n        console.warn(\"[omnidisc] dispatch handler failed\", payload.t, errorText(e));\n      }\n    });\n    const offStatus = await listen<GatewayStatusEvent>(\"omnidisc://status\", (event) => handleStatus(event.payload));\n    const offUpload = await listen<unknown>(\"omnidisc://upload\", (event) => handleUploadProgress(event.payload));\n    const offDecrypted = await listen<unknown>(\"omnidisc://decrypted\", (event) =>\n      handleDecryptedEvent(event.payload),\n    );\n    unlisteners = [offDispatch, offStatus, offUpload, offDecrypted];\n  } catch (e) {\n    initialized = false;\n    console.warn(\"[omnidisc] could not subscribe to gateway events\", errorText(e));\n    return;\n  }\n  for (const instance of instances) {\n    if (isDemo(instance.id)) continue;\n    void connectGateway(instance);\n  }\n}","sourceCodeStart":1412,"sourceCodeEnd":1448,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src/lib/stores/omnidisc-store.svelte.ts#L1412-L1448","documentation":"In the store's initialization, each `omnidisc://dispatch` event is wrapped in try/catch: if the user-supplied dispatch handling (handleDispatch) throws — e.g. a malformed payload from the backend or a bug in a message/state handler — the exception is contained and logged with the event type `payload.t`, so one bad event cannot crash the listener or the app.","triggerScenarios":"A dispatch event arrives whose handler (handleDispatch → message/guild/voice-state apply functions) throws: unexpected payload shape, missing instance fields, or a throw inside ensureUser/applyVoiceState-style state updates while processing MESSAGE_CREATE, GUILD_*, VOICE_STATE_UPDATE, etc.","commonSituations":"Backend/frontend version skew where the payload schema changed; corrupted or partially migrated persisted state the handler reads; race conditions where a dispatch arrives before instances state is hydrated.","solutions":["Look at the logged payload.t and errorText(e) to identify which event handler threw","Compare the dispatch payload shape against what handleDispatch expects (version skew between backend emitter and frontend parser)","Add narrowing/validation inside the specific failing handler instead of relying on this outer catch","Restart/reconnect the gateway to replay state after fixing the handler"],"exampleFix":"// before\nhandleDispatch(instance, payload.t, payload.d);\n// after\ntry {\n  handleDispatch(instance, payload.t, payload.d);\n} catch (e) {\n  console.warn(\"[omnidisc] dispatch handler failed\", payload.t, errorText(e));\n}","handlingStrategy":"try-catch","validationCode":"if (!instanceByUrl(payload.url)) return; // drop events for unknown instances early\nif (typeof payload.t !== \"string\" || payload.d == null) return;\n","typeGuard":"function isDispatchPayload(p: unknown): p is { url: string; t: string; d: unknown } {\n  return typeof p === \"object\" && p !== null &&\n    typeof (p as any).url === \"string\" && typeof (p as any).t === \"string\";\n}\n","tryCatchPattern":"try {\n  handleDispatch(instance, payload.t, payload.d);\n} catch (e) {\n  console.warn(\"[omnidisc] dispatch handler failed\", payload.t, errorText(e));\n}","preventionTips":["Validate dispatch payloads inside each handler rather than trusting backend shape","Keep backend event emitters and frontend handlers in version lockstep","Never let one handler's throw escape the listener loop"],"tags":["svelte","event-handling","defensive-programming","tauri"],"backgroundTag":"unexpected-response-shape","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}