:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): capture the real canvas playback error, not the mutation (#112454)
posthog[bot]f81e801dc754
diff --git a/common/replay-shared/src/canvas/canvas-plugin.test.ts b/common/replay-shared/src/canvas/canvas-plugin.test.tsindex 2844b7322fec..0cc2963fb60a 100644--- a/common/replay-shared/src/canvas/canvas-plugin.test.ts+++ b/common/replay-shared/src/canvas/canvas-plugin.test.ts@@ -4,7 +4,7 @@import { canvasMutation } from 'posthog-js/rrweb'import { EventType, IncrementalSource, eventWithTime } from 'posthog-js/rrweb-types'-import { CanvasReplayerPlugin } from './canvas-plugin'+import { CanvasMutationError, CanvasReplayerPlugin } from './canvas-plugin'// Mock rrweb canvasMutation functionjest.mock('posthog-js/rrweb', () => ({@@ -560,4 +560,57 @@ describe('CanvasReplayerPlugin', () => {expect(call.target.height).toBe(400)})})++ describe('canvas mutation error reporting', () => {+ const reportCanvasMutationFailure = async (thrown: unknown): Promise<jest.Mock> => {+ const canvas = document.createElement('canvas')+ const event = {+ type: EventType.IncrementalSnapshot as const,+ data: {+ source: IncrementalSource.CanvasMutation as const,+ id: 7,+ type: 1,+ commands: [{ property: 'drawArrays', args: [0, 0, 3] }],+ },+ timestamp: 1000,+ }+ const onError = jest.fn()+ // rrweb calls errorHandler from a catch after an await, never synchronously.+ const handled = new Promise<void>((resolve) => {+ ;(canvasMutation as jest.Mock).mockImplementationOnce(async ({ mutation, errorHandler }) => {+ await Promise.resolve()+ errorHandler(mutation, thrown)+ resolve()+ })+ })++ const plugin = CanvasReplayerPlugin([event], onError)+ const replayer = { getMirror: () => ({ getNode: (id: number) => (id === 7 ? canvas : null) }) }+ plugin.onBuild?.(canvas, { id: 7, replayer } as any)+ plugin.handler!(event, false, { replayer } as any)+ await handled+ return onError+ }++ it('reports the error rrweb passes, not the mutation', async () => {+ const thrown = new TypeError('drawImage failed')++ const onError = await reportCanvasMutationFailure(thrown)++ expect(onError).toHaveBeenCalledTimes(1)+ const [reported, context] = onError.mock.calls[0]+ expect(reported).toBe(thrown)+ expect(context).toEqual({ canvas_node_id: 7, canvas_context: 'WebGL' })+ })++ it('wraps a non-Error value with the canvas context', async () => {+ const onError = await reportCanvasMutationFailure('context lost')++ expect(onError).toHaveBeenCalledTimes(1)+ const [reported, context] = onError.mock.calls[0]+ expect(reported).toBeInstanceOf(CanvasMutationError)+ expect(reported.message).toBe('Canvas mutation failed in WebGL context: context lost')+ expect(context).toEqual({ canvas_node_id: 7, canvas_context: 'WebGL' })+ })+ })})diff --git a/common/replay-shared/src/canvas/canvas-plugin.ts b/common/replay-shared/src/canvas/canvas-plugin.tsindex 1facecda9e84..d5f1e86fc426 100644--- a/common/replay-shared/src/canvas/canvas-plugin.ts+++ b/common/replay-shared/src/canvas/canvas-plugin.ts@@ -2,6 +2,7 @@ import { Replayer, canvasMutation } from 'posthog-js/rrweb'import { ReplayPlugin } from 'posthog-js/rrweb'import {CanvasArg,+ CanvasContext,EventType,IncrementalSource,canvasMutationData,@@ -55,10 +56,30 @@ const PRELOAD_BUFFER_SIZE = 20const BUFFER_TIME = 30000 // 30 secondsconst DEBOUNCE_MILLIS = 250 // currently using 4fps for all recordings-export type CanvasPluginErrorHandler = (error: unknown) => void+export type CanvasPluginErrorContext = {+ canvas_node_id: number+ canvas_context: string+}++export type CanvasPluginErrorHandler = (error: unknown, context?: CanvasPluginErrorContext) => voidconst noOpErrorHandler: CanvasPluginErrorHandler = () => {}+export class CanvasMutationError extends Error {+ constructor(message: string) {+ super(message)+ this.name = 'CanvasMutationError'+ }+}++// The node id stays out of the message so that error tracking groups failures by context, not by canvas.+function toCanvasMutationError(error: unknown, context: CanvasPluginErrorContext): unknown {+ if (error instanceof Error) {+ return error+ }+ return new CanvasMutationError(`Canvas mutation failed in ${context.canvas_context} context: ${String(error)}`)+}+export const CanvasReplayerPlugin = (events: eventWithTime[],onError: CanvasPluginErrorHandler = noOpErrorHandler@@ -227,8 +248,13 @@ export const CanvasReplayerPlugin = (target: target,imageMap,canvasEventMap,- errorHandler: (error: unknown) => {- onError(error)+ // rrweb types this handler as `any` and calls it with (mutation, error), so the compiler cannot catch a swapped order.+ errorHandler: (_failedMutation: unknown, error: unknown) => {+ const context = {+ canvas_node_id: data.id,+ canvas_context: CanvasContext[data.type] ?? String(data.type),+ }+ onError(toCanvasMutationError(error, context), context)},})diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.tsindex f593be2d3d67..5d66059ee93a 100644--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts@@ -2745,8 +2745,9 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(plugins.push(createCorsPlugin(() => values.replayProxyToken))}- const canvasPlugin = CanvasReplayerPlugin(values.sessionPlayerData.snapshotsByWindowId[windowId], (error) =>- posthog.captureException(error)+ const canvasPlugin = CanvasReplayerPlugin(+ values.sessionPlayerData.snapshotsByWindowId[windowId],+ (error, context) => posthog.captureException(error, context))plugins.push(canvasPlugin)plugins.push(AudioMuteReplayerPlugin(values.isMuted))