: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(error-tracking): drop malformed assignee filters instead of 400ing (#87169)
Catalin547b91ab870b
diff --git a/products/error_tracking/frontend/components/IssueQueryOptions/issueQueryOptionsLogic.test.ts b/products/error_tracking/frontend/components/IssueQueryOptions/issueQueryOptionsLogic.test.tsindex 93abbba6d93b..63fe5e61c8b2 100644--- a/products/error_tracking/frontend/components/IssueQueryOptions/issueQueryOptionsLogic.test.ts+++ b/products/error_tracking/frontend/components/IssueQueryOptions/issueQueryOptionsLogic.test.ts@@ -5,7 +5,9 @@ import { initKeaTests } from '~/test/init'import { issueQueryOptionsLogic } from './issueQueryOptionsLogic'const LOGIC_KEY = 'test'-const PERSISTED_STATUS_KEY = `products.error_tracking.components.IssueQueryOptions.issueQueryOptionsLogic.${LOGIC_KEY}.status`+const PERSISTED_KEY_PREFIX = `products.error_tracking.components.IssueQueryOptions.issueQueryOptionsLogic.${LOGIC_KEY}`+const PERSISTED_STATUS_KEY = `${PERSISTED_KEY_PREFIX}.status`+const PERSISTED_ASSIGNEE_KEY = `${PERSISTED_KEY_PREFIX}.assignee`describe('issueQueryOptionsLogic', () => {beforeEach(() => {@@ -62,4 +64,62 @@ describe('issueQueryOptionsLogic', () => {const logic = mountLogic()expect(logic.values.status).toBe('resolved')})++ // A malformed assignee reaches the query, which the backend rejects with a 400. It renders as+ // unset, and it's persisted, so the whole issues page keeps failing until it's manually cleared.+ const MALFORMED_ASSIGNEES = [+ 'me',+ 7,+ { type: 'user' },+ { type: 'user', id: null },+ { type: 'user', id: '' },+ { type: 'user', id: '7' },+ { type: 'user', id: 1.5 },+ { type: 'role', id: 7 },+ { type: 'role', id: 'not-a-uuid' },+ { type: 'role', id: ' ' },+ { type: 'team', id: 7 },+ { id: 7 },+ ['role:01978cae-04b5-0000-17fb-0405fcb791be'],+ ]++ it.each(MALFORMED_ASSIGNEES)('falls back to no assignee when setAssignee receives %p', (assignee) => {+ const logic = mountLogic()+ logic.actions.setAssignee(assignee as any)+ expect(logic.values.assignee).toBeNull()+ })++ it.each([+ { type: 'user', id: 7 },+ { type: 'role', id: '01978cae-04b5-0000-17fb-0405fcb791be' },+ ] as const)('applies the valid assignee %p', (assignee) => {+ const logic = mountLogic()+ logic.actions.setAssignee(assignee)+ expect(logic.values.assignee).toEqual(assignee)+ })++ it('drops fields the query schema does not accept', () => {+ const logic = mountLogic()+ logic.actions.setAssignee({ type: 'user', id: 7, user: { first_name: 'Someone' } } as any)+ expect(logic.values.assignee).toEqual({ type: 'user', id: 7 })+ })++ it.each(MALFORMED_ASSIGNEES)('resets the malformed persisted assignee %p on mount', (assignee) => {+ localStorage.setItem(PERSISTED_ASSIGNEE_KEY, JSON.stringify(assignee))+ const logic = mountLogic()+ expect(logic.values.assignee).toBeNull()+ })++ it('keeps a valid persisted assignee on mount', () => {+ localStorage.setItem(PERSISTED_ASSIGNEE_KEY, JSON.stringify({ type: 'user', id: 7 }))+ const logic = mountLogic()+ expect(logic.values.assignee).toEqual({ type: 'user', id: 7 })+ })++ it('falls back to no assignee when the URL has a malformed one, overriding the persisted one', () => {+ localStorage.setItem(PERSISTED_ASSIGNEE_KEY, JSON.stringify({ type: 'user', id: 7 }))+ const logic = mountLogic()+ router.actions.push('/error_tracking', { assignee: 'me' })+ expect(logic.values.assignee).toBeNull()+ })})diff --git a/products/error_tracking/frontend/components/IssueQueryOptions/issueQueryOptionsLogic.ts b/products/error_tracking/frontend/components/IssueQueryOptions/issueQueryOptionsLogic.tsindex b57b1eeb7b3e..083423a0ee33 100644--- a/products/error_tracking/frontend/components/IssueQueryOptions/issueQueryOptionsLogic.ts+++ b/products/error_tracking/frontend/components/IssueQueryOptions/issueQueryOptionsLogic.ts@@ -3,6 +3,7 @@ import { MakeLogicType, actions, afterMount, kea, key, listeners, path, props, rimport { actionToUrl, router, urlToAction } from 'kea-router'import posthog from 'posthog-js'+import { isUUIDLike } from 'lib/utils/guards'import { Params } from 'scenes/sceneTypes'import { ErrorTrackingIssue, ErrorTrackingQuery } from '~/queries/schema/schema-general'@@ -38,6 +39,25 @@ const DEFAULT_ORDER_DIRECTION = 'DESC'const DEFAULT_ASSIGNEE = nullconst DEFAULT_STATUS = 'active'+// Like isValidOrderBy, but the assignee is an object, so it can be malformed in more ways: a bare+// string, a missing or null id, a type outside user/role, or a resolved {id, type, user} object.+// The query schema forbids unknown keys, so any of those makes every issues query fail with a 400+// while the filter button still renders as unset — and the value is persisted, so it keeps failing+// until someone finds "Remove assignee" in the dropdown. Keep only the two fields the query accepts.+export function sanitizeAssignee(assignee: unknown): ErrorTrackingIssueAssignee | null {+ if (!assignee || typeof assignee !== 'object') {+ return DEFAULT_ASSIGNEE+ }+ const { id, type } = assignee as Partial<ErrorTrackingIssueAssignee>+ if (type === 'user' && typeof id === 'number' && Number.isInteger(id)) {+ return { id, type }+ }+ if (type === 'role' && typeof id === 'string' && isUUIDLike(id)) {+ return { id, type }+ }+ return DEFAULT_ASSIGNEE+}+// The statuses the filter accepts: the query's status union minus archived/pending_release,// which are deprecated (writes rejected, legacy rows backfilled to resolved) and not offered by// the picker. Exhaustive over the rest, so removing a status from the schema fails typechecking.@@ -128,7 +148,7 @@ export const issueQueryOptionsLogic = kea<issueQueryOptionsLogicType>([DEFAULT_ASSIGNEE as ErrorTrackingQueryAssignee | null,{ persist: true },{- setAssignee: (_, { assignee }) => assignee,+ setAssignee: (_, { assignee }) => sanitizeAssignee(assignee),},],status: [@@ -195,7 +215,8 @@ export const issueQueryOptionsLogic = kea<issueQueryOptionsLogicType>([// stale link doesn't silently keep querying the previously persisted status.actions.setStatus(isValidStatus(params.status) ? params.status : DEFAULT_STATUS)}- if (params.assignee && !equal(params.assignee, values.assignee)) {+ // Presence check like status, so a link with a cleared assignee resets the persisted one.+ if ('assignee' in params && !equal(params.assignee, values.assignee)) {actions.setAssignee(params.assignee)}if (params.orderDirection && !equal(params.orderDirection, values.orderDirection)) {@@ -208,13 +229,17 @@ export const issueQueryOptionsLogic = kea<issueQueryOptionsLogicType>([}),afterMount(({ actions, values }) => {- // Persisted orderBy/status are loaded straight into state (bypassing the reducers), so an- // invalid stored value would otherwise reach the query and break the page. Reset them.+ // Persisted values are loaded straight into state (bypassing the reducers), so an invalid+ // stored value would otherwise reach the query and break the page. Reset them.if (!isValidOrderBy(values.orderBy)) {actions.setOrderBy(DEFAULT_ORDER_BY)}if (!isValidStatus(values.status)) {actions.setStatus(DEFAULT_STATUS)}+ const sanitizedAssignee = sanitizeAssignee(values.assignee)+ if (!equal(values.assignee, sanitizedAssignee)) {+ actions.setAssignee(sanitizedAssignee)+ }}),])