Skip to content

: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.

fix(dashboards): show not found for a non-numeric dashboard id (#91854)

posthog[bot]128fa6d2076d
diff --git a/frontend/src/scenes/dashboard/Dashboard.tsx b/frontend/src/scenes/dashboard/Dashboard.tsx
index e07a1a00b10c..e4b1b2b6054f 100644
--- a/frontend/src/scenes/dashboard/Dashboard.tsx
+++ b/frontend/src/scenes/dashboard/Dashboard.tsx
@@ -50,7 +50,14 @@ interface DashboardProps {
showCreateAnomalyAlertButton?: boolean
}
-const parseDashboardId = (id: string | undefined): number => (typeof id === 'string' ? parseInt(id, 10) : NaN)
+export const parseDashboardId = (id: string | undefined): number => {
+ if (!id || !/^\d+$/.test(id)) {
+ return NaN
+ }
+ // Reject "0" and all-zero variants: id 0 is the reserved internal sentinel, never a real dashboard.
+ const dashboardId = Number(id)
+ return dashboardId > 0 ? dashboardId : NaN
+}
// Wrapper needed because SceneComponent<DashboardLogicProps> requires the component to accept
// DashboardLogicProps, but DashboardScene takes { backTo? } (logic props are bound separately).
@@ -100,6 +107,7 @@ function DashboardScene({
dashboardFailedToLoad,
accessDeniedToDashboard,
error404,
+ hasInvalidDashboardId,
} = useValues(dashboardLogic)
const { layoutZoom } = useValues(dashboardLogic)
const { currentTeamId } = useValues(teamLogic)
@@ -130,7 +138,9 @@ function DashboardScene({
object="dashboard"
caption={
<>
- It may have been deleted, or the link is out of date.{' '}
+ {hasInvalidDashboardId
+ ? 'This dashboard link is not valid.'
+ : 'It may have been deleted, or the link is out of date.'}{' '}
<Link to={urls.dashboards()}>Go to your dashboards</Link>.
</>
}
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
index ea3cc2b0b4aa..239b6771c730 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
@@ -16,6 +16,7 @@ import * as featureFlagLib from 'lib/logic/featureFlagLogic'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { DashboardEventSource, eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { addInsightToDashboardLogic } from 'scenes/dashboard/addInsightToDashboardModalLogic'
+import { parseDashboardId } from 'scenes/dashboard/Dashboard'
import { dashboardInsightColorsModalLogic } from 'scenes/dashboard/dashboardInsightColorsModalLogic'
import { DashboardLoadAction, dashboardLogic } from 'scenes/dashboard/dashboardLogic'
import * as dashboardUtils from 'scenes/dashboard/dashboardUtils'
@@ -312,18 +313,44 @@ describe('dashboardLogic', () => {
insightsModel.mount()
})
- describe('key() guard', () => {
+ describe('malformed dashboard id', () => {
it.each([
['NaN', NaN],
['undefined', undefined as unknown as number],
['Infinity', Infinity],
- ])('throws when id is %s', (_label, id) => {
- expect(() => dashboardLogic({ id })).toThrow(/non-finite id/)
+ ])('mounts and reports not found instead of throwing when id is %s', async (_label, id) => {
+ const invalidLogic = dashboardLogic({ id })
+ invalidLogic.mount()
+ await expectLogic(invalidLogic).toMatchValues({ error404: true, hasInvalidDashboardId: true })
})
it('accepts a finite numeric id', () => {
expect(() => dashboardLogic({ id: 42 })).not.toThrow()
})
+
+ it('reports not found when dashboard data is supplied for an invalid id', async () => {
+ const invalidLogic = dashboardLogic({ id: NaN, dashboard: dashboards[5] })
+ invalidLogic.mount()
+
+ await expectLogic(invalidLogic).toMatchValues({ error404: true, hasInvalidDashboardId: true })
+ })
+
+ it('does not load the zero dashboard sentinel', async () => {
+ const zeroLogic = dashboardLogic({ id: 0 })
+ zeroLogic.mount()
+
+ await expectLogic(zeroLogic)
+ .toNotHaveDispatchedActions(['loadDashboard', 'loadDashboardStreaming', 'dashboardNotFound'])
+ .toMatchValues({ error404: false, hasInvalidDashboardId: false })
+ })
+
+ it.each(['12abc', '12.5', '+12', '-12', '0', '00', '000'])('rejects invalid route id %s', (id) => {
+ expect(parseDashboardId(id)).toBeNaN()
+ })
+
+ it('parses a full numeric route id', () => {
+ expect(parseDashboardId('12')).toBe(12)
+ })
})
describe('tile layouts', () => {
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index ec8992727152..e6e91b658748 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -313,6 +313,7 @@ export interface dashboardLogicValues {
externalFilters: DashboardFilter
filtersOverrideForLoad: DashboardFilter
hasIntermittentFilters: boolean
+ hasInvalidDashboardId: boolean
hasUnsavedColorChanges: boolean
hasUnsavedLayoutChanges: boolean
hasUrlFilters: boolean
@@ -978,7 +979,7 @@ export interface dashboardLogicActions {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dashboardLogicMeta {
- key: number
+ key: number | 'invalid'
sharedListeners: {
reportRefreshTiming: (
payload: any,
@@ -1152,6 +1153,7 @@ export interface dashboardLogicMeta {
searchParams: Record<string, any>
) => Breadcrumb[]
projectTreeRef: (arg: number) => ProjectTreeRef
+ hasInvalidDashboardId: (arg: number) => boolean
sidePanelContext: (
dashboard: DashboardType<QueryBasedInsightModel<Node<Record<string, any>>>> | null
) => SidePanelSceneContext | null
@@ -1219,10 +1221,8 @@ export const dashboardLogic = kea<dashboardLogicType>([
props({} as DashboardLogicProps),
key((props) => {
- // `typeof NaN === 'number'` — check finiteness explicitly so a NaN id surfaces loudly
- // instead of mounting a stuck-NotFound logic instance.
- if (typeof props.id !== 'number' || !Number.isFinite(props.id)) {
- throw Error(`dashboardLogic key() received non-finite id: ${String(props.id)}`)
+ if (!Number.isFinite(props.id)) {
+ return 'invalid'
}
return props.id
}),
@@ -3048,6 +3048,10 @@ export const dashboardLogic = kea<dashboardLogicType>([
return { type: 'dashboard', ref: String(id) }
},
],
+ hasInvalidDashboardId: [
+ () => [(_, props: DashboardLogicProps) => props.id],
+ (id: number): boolean => !Number.isFinite(id),
+ ],
[SIDE_PANEL_CONTEXT_KEY]: [
(s) => [s.dashboard],
(dashboard: DashboardType<QueryBasedInsightModel> | null): SidePanelSceneContext | null => {
@@ -3176,6 +3180,10 @@ export const dashboardLogic = kea<dashboardLogicType>([
events(({ actions, props, values, cache }) => ({
afterMount: () => {
// NOTE: initial dashboard load is done after variables are loaded in initialVariablesLoaded
+ if (!Number.isFinite(props.id)) {
+ actions.dashboardNotFound()
+ return
+ }
if (props.id) {
if (props.dashboard) {
// If we already have dashboard data, use it. Should the data turn out to be stale,
diff --git a/frontend/src/scenes/sceneLogic.tsx b/frontend/src/scenes/sceneLogic.tsx
index c8472a03a7e1..d125fb477f2e 100644
--- a/frontend/src/scenes/sceneLogic.tsx
+++ b/frontend/src/scenes/sceneLogic.tsx
@@ -746,9 +746,6 @@ export const sceneLogic = kea<sceneLogicType>([
}
}
} catch (error) {
- // Scene logic builders (e.g. dashboardLogic.key()) can throw on malformed
- // route params like `/dashboard/abc`. Capture so regressions surface, then
- // route to Error404 so the user sees a proper 404 instead of a blank crash.
posthog.captureException(error, { extra: { sceneId, sceneKey } })
newLogicErrored = true
}
diff --git a/frontend/src/scenes/scenes.ts b/frontend/src/scenes/scenes.ts
index e836dc263c10..55fc4e65b53f 100644
--- a/frontend/src/scenes/scenes.ts
+++ b/frontend/src/scenes/scenes.ts
@@ -648,6 +648,9 @@ export const redirects: Record<
'/code_review': (_params, searchParams, hashParams) => combineUrl(urls.codeReview(), searchParams, hashParams).url,
'/comments': () => urls.comments(),
'/dashboards': urls.dashboards(),
+ // New dashboards open in a modal on the list page. `/dashboard/new` is a guessed URL,
+ // so send it there with the modal open instead of matching `/dashboard/:id`.
+ '/dashboard/new': `${urls.dashboards()}#newDashboard=modal`,
'/data-management': urls.eventDefinitions(),
'/data-management/database': urls.sources(),
'/data-management/data-warehouse': urls.sources(),