: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(dashboards): show serialized insight query errors (#74299)
Matt Pua889be3bf8b5d
diff --git a/frontend/src/lib/components/Cards/InsightCard/InsightCard.tsx b/frontend/src/lib/components/Cards/InsightCard/InsightCard.tsxindex 9acb7b1667bd..2bb1651dc0df 100644--- a/frontend/src/lib/components/Cards/InsightCard/InsightCard.tsx+++ b/frontend/src/lib/components/Cards/InsightCard/InsightCard.tsx@@ -371,7 +371,13 @@ function InsightCardInternal()} else if (apiError instanceof ApiError) {const isDashboardTileError = apiError.code === 'dashboard_tile_error'- return <InsightErrorState title={apiError.detail} supportOnly={isDashboardTileError} />+ return (+ <InsightErrorState+ title={apiError.detail}+ queryId={apiError.data?.queryId}+ supportOnly={isDashboardTileError}+ />+ )}return <InsightErrorState />}diff --git a/frontend/src/scenes/dashboard/DashboardItems.test.tsx b/frontend/src/scenes/dashboard/DashboardItems.test.tsxindex 422e455360d8..05eff708e6e6 100644--- a/frontend/src/scenes/dashboard/DashboardItems.test.tsx+++ b/frontend/src/scenes/dashboard/DashboardItems.test.tsx@@ -404,4 +404,80 @@ describe('DashboardItems', () => {expect(insightCard).toHaveAttribute('data-api-error-code', 'dashboard_tile_error')expect(insightCard).toHaveAttribute('data-api-error-detail', 'There is a problem loading this dashboard tile.')})++ it('shows a query status error from an initially serialized insight', () => {+ const errorTile = {+ id: 2,+ insight: {+ id: 101,+ short_id: 'abc123',+ query: { kind: 'InsightVizNode' },+ query_status: {+ id: 'failed-query-id',+ error: true,+ error_message: 'This query ran out of memory before it could finish',+ error_code: 'query_memory_limit',+ },+ },+ }+ const refreshStatus = {}+ mockedUseValues.mockImplementation((logic) => {+ if (logic === dashboardLogic) {+ return {+ dashboard: { id: 5 },+ tiles: [errorTile],+ layouts: { sm: [{ i: '2', x: 0, y: 0, w: 6, h: 5 }] },+ dashboardMode: null,+ placement: DashboardPlacement.Dashboard,+ isRefreshingQueued: () => false,+ isRefreshing: () => false,+ highlightedInsightId: null,+ refreshStatus,+ dashboardStreaming: false,+ effectiveEditBarFilters: {},+ effectiveDashboardVariableOverrides: {},+ temporaryBreakdownColors: [],+ dataColorThemeId: null,+ canEditDashboard: true,+ layoutZoom: 1,+ dashboardWidgetsEnabled: true,+ widgetResultsByTileId: {},+ widgetRefreshStatus: {},+ }+ }++ if (logic === dashboardsModel) {+ return { nameSortedDashboards: [] }+ }++ return {}+ })++ const { container, rerender } = render(<DashboardItems />)+ const insightCard = container.querySelector('[data-attr="insight-card"]')++ expect(insightCard).toHaveAttribute('data-api-errored', 'true')+ expect(insightCard).toHaveAttribute('data-api-error-status', '400')+ expect(insightCard).toHaveAttribute('data-api-error-code', 'query_memory_limit')+ expect(insightCard).toHaveAttribute(+ 'data-api-error-detail',+ 'This query ran out of memory before it could finish'+ )++ Object.assign(refreshStatus, {+ abc123: {+ errored: true,+ error: {+ status: 503,+ detail: 'The refreshed query failed',+ code: 'refresh_failed',+ },+ },+ })+ rerender(<DashboardItems />)++ expect(insightCard).toHaveAttribute('data-api-error-status', '503')+ expect(insightCard).toHaveAttribute('data-api-error-code', 'refresh_failed')+ expect(insightCard).toHaveAttribute('data-api-error-detail', 'The refreshed query failed')+ })})diff --git a/frontend/src/scenes/dashboard/DashboardItems.tsx b/frontend/src/scenes/dashboard/DashboardItems.tsxindex f6a492e9ff26..1d3640627fe0 100644--- a/frontend/src/scenes/dashboard/DashboardItems.tsx+++ b/frontend/src/scenes/dashboard/DashboardItems.tsx@@ -20,7 +20,12 @@ import { objectsEqual } from 'lib/utils/objects'import { addInsightToDashboardLogic } from 'scenes/dashboard/addInsightToDashboardModalLogic'import { getAddTileMenuItems } from 'scenes/dashboard/DashboardHeaderActions'import { dashboardLogic } from 'scenes/dashboard/dashboardLogic'-import { BREAKPOINTS, BREAKPOINT_COLUMN_COUNTS, isWidgetTileVisibleOnPlacement } from 'scenes/dashboard/dashboardUtils'+import {+ BREAKPOINTS,+ BREAKPOINT_COLUMN_COUNTS,+ getInsightQueryError,+ isWidgetTileVisibleOnPlacement,+} from 'scenes/dashboard/dashboardUtils'import { continueDragGestureInEditMode, continueResizeGestureInEditMode } from 'scenes/dashboard/editLayoutGesture'import { InsertTileOverlay } from 'scenes/dashboard/InsertTileOverlay'import { useSurveyLinkedInsights } from 'scenes/surveys/hooks/useSurveyLinkedInsights'@@ -546,13 +551,16 @@ export function DashboardItems({ showCreateAnomalyAlertButton }: DashboardItemsPif (insight) {// Check if this insight has an error from the serverconst isErrorTile = !!tile.error- const apiErrored = isErrorTile || refreshStatus[insight.short_id]?.errored || false+ const queryError = getInsightQueryError(insight)+ const apiErrored =+ isErrorTile || !!queryError || refreshStatus[insight.short_id]?.errored || false+ const refreshError = refreshStatus[insight.short_id]?.errorconst apiError = isErrorTile? new ApiError(undefined, 500, undefined, {detail: tile.error!.message,code: 'dashboard_tile_error',})- : refreshStatus[insight.short_id]?.error+ : refreshError || queryError || undefinedconst loadingQueued = isErrorTile ? false : isRefreshingQueued(insight.short_id)const loading = isErrorTile ? false : isRefreshing(insight.short_id)diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.tsindex e69f1ed6ac7f..02c10ed42402 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts@@ -1528,6 +1528,40 @@ describe('dashboardLogic', () => {getInsightWithRetrySpy.mockRestore()})+ it('shows query status errors returned in serialized insights', async () => {+ const dashboard = dashboards[5]+ const insight1 = dashboard.tiles[0].insight!+ const insight2 = dashboard.tiles[1].insight!+ const getInsightWithRetrySpy = jest.spyOn(dashboardUtils, 'getInsightWithRetry').mockResolvedValue({+ ...insight1,+ query_status: {+ id: 'failed-query-id',+ query_async: true,+ team_id: MOCK_TEAM_ID,+ error: true,+ complete: true,+ error_message: 'This query ran out of memory before it could finish',+ error_code: 'query_memory_limit',+ },+ })++ await expectLogic(logic, () => {+ logic.actions.triggerDashboardRefresh()+ }).toFinishAllListeners()++ for (const insight of [insight1, insight2]) {+ expect(logic.values.refreshStatus[insight.short_id]).toMatchObject({+ errored: true,+ error: expect.objectContaining({+ detail: 'This query ran out of memory before it could finish',+ code: 'query_memory_limit',+ }),+ })+ }++ getInsightWithRetrySpy.mockRestore()+ })+it('automatic refresh reloads stale insights (but not fresh ones)', async () => {const dashboard = dashboards[5]const staleInsight = {diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsxindex 84b8e043d23e..3cdfc8f12748 100644--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx@@ -134,6 +134,7 @@ import {encodeURLFilters,encodeURLVariables,getDashboardWidgetType,+ getInsightQueryError,getInsightWithRetry,isLayoutEditEventSource,layoutsByTile,@@ -3526,8 +3527,13 @@ export const dashboardLogic = kea<dashboardLogicType>([)if (refreshedInsight) {- dashboardsModel.actions.updateDashboardInsight(refreshedInsight, undefined, dashboardId)- actions.setRefreshStatus(insight.short_id)+ const queryError = getInsightQueryError(refreshedInsight)+ if (queryError) {+ actions.setRefreshError(insight.short_id, queryError)+ } else {+ dashboardsModel.actions.updateDashboardInsight(refreshedInsight, undefined, dashboardId)+ actions.setRefreshStatus(insight.short_id)+ }} else {actions.setRefreshError(insight.short_id)}@@ -3622,21 +3628,27 @@ export const dashboardLogic = kea<dashboardLogicType>([)if (refreshedInsight) {- dashboardsModel.actions.updateDashboardInsight(refreshedInsight, undefined, dashboardId)- actions.setRefreshStatus(insight.short_id)- tilesRefreshedCount++- if (refreshedInsight.is_cached) {- tilesRefreshedCachedCount++- }+ const queryError = getInsightQueryError(refreshedInsight)+ if (queryError) {+ actions.setRefreshError(insight.short_id, queryError)+ tilesErroredCount+++ } else {+ dashboardsModel.actions.updateDashboardInsight(refreshedInsight, undefined, dashboardId)+ actions.setRefreshStatus(insight.short_id)+ tilesRefreshedCount+++ if (refreshedInsight.is_cached) {+ tilesRefreshedCachedCount+++ }- eventUsageLogic.actions.reportDashboardTileRefreshed(- dashboardId,- tile,- urlFilters,- urlVariables,- Math.floor(performance.now() - insightRefreshStartTime),- false- )+ eventUsageLogic.actions.reportDashboardTileRefreshed(+ dashboardId,+ tile,+ urlFilters,+ urlVariables,+ Math.floor(performance.now() - insightRefreshStartTime),+ false+ )+ }} else {actions.setRefreshError(insight.short_id)tilesErroredCount++diff --git a/frontend/src/scenes/dashboard/dashboardUtils.ts b/frontend/src/scenes/dashboard/dashboardUtils.tsindex 98f6aa379862..23050cc539ec 100644--- a/frontend/src/scenes/dashboard/dashboardUtils.ts+++ b/frontend/src/scenes/dashboard/dashboardUtils.ts@@ -4,6 +4,7 @@ import { lemonToast } from '@posthog/lemon-ui'import { getDashboardWidgetCatalogEntry } from '@posthog/products-dashboards/frontend/widget_types/catalog'import api, { ApiMethodOptions, getJSONOrNull } from 'lib/api'+import { ApiError } from 'lib/api-error'import type { Dayjs } from 'lib/dayjs'import { currentSessionId } from 'lib/internalMetrics'import { accessLevelSatisfied } from 'lib/utils/accessControlUtils'@@ -13,7 +14,7 @@ import { shouldCancelQuery } from 'lib/utils/requests'import { toParams } from 'lib/utils/url'import { getQueryBasedInsightModel } from '~/queries/nodes/InsightViz/utils'-import { pollForResults } from '~/queries/query'+import { parseErrorMessage, pollForResults } from '~/queries/query'import { DashboardFilter, HogQLVariable, TileFilters } from '~/queries/schema/schema-general'import {AccessControlLevel,@@ -31,6 +32,20 @@ import {import { SHARED_DASHBOARD_AUTO_FORCE_IF_STALE_MINUTES } from './dashboardConstants'+export function getInsightQueryError(insight: QueryBasedInsightModel): ApiError | null {+ const queryStatus = insight.query_status+ if (!queryStatus?.error) {+ return null+ }++ const parsedError = parseErrorMessage(queryStatus.error_message ?? undefined)+ return new ApiError(undefined, 400, undefined, {+ detail: parsedError.message,+ code: queryStatus.error_code ?? parsedError.code,+ queryId: queryStatus.id,+ })+}+/** Shape used for staff JSON export, customer save-as-template, and API `create_from_template_json`. */export function dashboardToSaveableTemplate(dashboard: DashboardType<InsightModel> | null | undefined