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(approvals): stop reporting approval-required 409s as exceptions (#78702)

posthog[bot]9bd745df1244
diff --git a/frontend/src/initKea.ts b/frontend/src/initKea.ts
index 223df111efd8..93acd16128e3 100644
--- a/frontend/src/initKea.ts
+++ b/frontend/src/initKea.ts
@@ -8,7 +8,7 @@ import { waitForPlugin } from 'kea-waitfor'
import { windowValuesPlugin } from 'kea-window-values'
import posthog from 'posthog-js'
-import { isAccessDeniedError } from 'lib/api-error'
+import { isAccessDeniedError, isApprovalRequiredError } from 'lib/api-error'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import {
addProjectIdIfMissing,
@@ -147,7 +147,7 @@ export function initKea({
if (
!ERROR_FILTER_ALLOW_LIST.includes(actionKey) &&
error?.status !== undefined &&
- ![200, 201, 204, 401, 409].includes(error.status) && // 401 is handled by api.ts and the userLogic, 409 is handled by approval workflow
+ ![200, 201, 204, 401, 409].includes(error.status) && // 401 is handled by api.ts and the userLogic; 409 conflict flows surface their own UI
!(isLoadAction && error.status === 403) && // 403 access denied is handled by sceneLogic gates
!isAccessDenied
) {
@@ -192,7 +192,9 @@ export function initKea({
if (!errorsSilenced) {
console.error({ error, reducerKey, actionKey })
}
- if (!TRANSIENT_GATEWAY_STATUSES.includes(error?.status)) {
+ // An approvals 409 is expected control flow (a change request was created, or one
+ // is already pending) surfaced to the user by the approvals UI, not a failure.
+ if (!TRANSIENT_GATEWAY_STATUSES.includes(error?.status) && !isApprovalRequiredError(error)) {
posthog.captureException(error)
}
},
diff --git a/frontend/src/lib/api-error.ts b/frontend/src/lib/api-error.ts
index df602409c98d..4f769531808b 100644
--- a/frontend/src/lib/api-error.ts
+++ b/frontend/src/lib/api-error.ts
@@ -6,6 +6,15 @@ export function isAccessDeniedError(error: { status?: number; code?: string | nu
return error.status === 403 && error.code === 'permission_denied'
}
+/**
+ * A 409 from the approvals gate: the change was policy-gated and a change request was created,
+ * or one is already pending. Approval 409 bodies always carry `change_request_id`
+ * (see products/approvals/backend/decorators.py).
+ */
+export function isApprovalRequiredError(error: { status?: number; data?: any } | null | undefined): boolean {
+ return error?.status === 409 && Boolean(error?.data?.change_request_id)
+}
+
export class ApiError extends Error {
/** Django REST Framework `detail` - used in downstream error handling. */
detail: string | null
diff --git a/frontend/src/scenes/approvals/ApprovalRequiredBanner.test.tsx b/frontend/src/scenes/approvals/ApprovalRequiredBanner.test.tsx
new file mode 100644
index 000000000000..00fff6ed1957
--- /dev/null
+++ b/frontend/src/scenes/approvals/ApprovalRequiredBanner.test.tsx
@@ -0,0 +1,26 @@
+import { render, within } from '@testing-library/react'
+
+import { lemonToast } from '@posthog/lemon-ui'
+
+import { CHANGE_REQUEST_PENDING_CODE, showApprovalRequiredToast } from './ApprovalRequiredBanner'
+
+describe('showApprovalRequiredToast', () => {
+ let infoSpy: jest.SpyInstance
+
+ beforeEach(() => {
+ infoSpy = jest.spyOn(lemonToast, 'info').mockImplementation()
+ })
+
+ it.each([
+ { code: undefined, expected: 'Your request to enable this flag has been submitted for approval.' },
+ { code: 'approval_required', expected: 'Your request to enable this flag has been submitted for approval.' },
+ { code: CHANGE_REQUEST_PENDING_CODE, expected: 'A request to enable this flag is already pending approval.' },
+ ])('renders "$expected" for code=$code', ({ code, expected }) => {
+ showApprovalRequiredToast('cr-1', 'enable this flag', code)
+
+ const [message, options] = infoSpy.mock.calls[0]
+ expect(options).toEqual({ toastId: 'approval-required-cr-1' })
+ const { container } = render(message)
+ expect(within(container).getByText(expected)).toBeTruthy()
+ })
+})
diff --git a/frontend/src/scenes/approvals/ApprovalRequiredBanner.tsx b/frontend/src/scenes/approvals/ApprovalRequiredBanner.tsx
index d692209209cd..0f98155ed5a2 100644
--- a/frontend/src/scenes/approvals/ApprovalRequiredBanner.tsx
+++ b/frontend/src/scenes/approvals/ApprovalRequiredBanner.tsx
@@ -33,25 +33,36 @@ export function ApprovalRequiredBanner({
>
<div>
<strong>Approval required</strong>
- <div className="text-sm mt-1">
- {actionDescription
- ? `Your request to ${actionDescription} has been submitted for approval.`
- : 'Your change request has been submitted for approval.'}
- </div>
+ <div className="text-sm mt-1">{approvalRequiredMessage(actionDescription)}</div>
</div>
</LemonBanner>
)
}
-export function showApprovalRequiredToast(changeRequestId: string, actionDescription?: string): void {
+/** Backend `code` on an approvals 409 when a matching change request already exists (see products/approvals/backend/decorators.py). */
+export const CHANGE_REQUEST_PENDING_CODE = 'change_request_pending'
+
+function approvalRequiredMessage(actionDescription?: string, code?: string): string {
+ if (code === CHANGE_REQUEST_PENDING_CODE) {
+ return actionDescription
+ ? `A request to ${actionDescription} is already pending approval.`
+ : 'This change is already pending approval.'
+ }
+ return actionDescription
+ ? `Your request to ${actionDescription} has been submitted for approval.`
+ : 'Your change request has been submitted for approval.'
+}
+
+export function showApprovalRequiredToast(
+ changeRequestId: string,
+ actionDescription?: string,
+ /** `code` from the approval 409 body. `change_request_pending` means a request already existed and nothing new was submitted. */
+ code?: string
+): void {
lemonToast.info(
<div>
<strong>Approval required</strong>
- <div className="text-sm mt-1">
- {actionDescription
- ? `Your request to ${actionDescription} has been submitted for approval.`
- : 'Your change request has been submitted for approval.'}
- </div>
+ <div className="text-sm mt-1">{approvalRequiredMessage(actionDescription, code)}</div>
<LemonButton
type="secondary"
size="small"
@@ -62,7 +73,8 @@ export function showApprovalRequiredToast(changeRequestId: string, actionDescrip
>
View approval
</LemonButton>
- </div>
+ </div>,
+ { toastId: `approval-required-${changeRequestId}` }
)
}
diff --git a/frontend/src/scenes/experiments/experimentLogic.test.ts b/frontend/src/scenes/experiments/experimentLogic.test.ts
index 6ffe1aa3fbf8..ceaafc03e215 100644
--- a/frontend/src/scenes/experiments/experimentLogic.test.ts
+++ b/frontend/src/scenes/experiments/experimentLogic.test.ts
@@ -1926,7 +1926,7 @@ describe('experimentLogic', () => {
it('shows approval toast and suppresses error toast on 409', async () => {
const createSpy = jest.spyOn(api, 'create').mockRejectedValue({
status: 409,
- data: { change_request_id: 'cr-123' },
+ data: { change_request_id: 'cr-123', code: 'approval_required' },
})
const errorMock = lemonToast.error as jest.Mock
errorMock.mockClear()
@@ -1945,7 +1945,8 @@ describe('experimentLogic', () => {
// Should show approval required toast with change request ID
expect(mockShowApprovalRequiredToast).toHaveBeenCalledWith(
'cr-123',
- 'end this experiment and roll out the winning variant'
+ 'end this experiment and roll out the winning variant',
+ 'approval_required'
)
// Should NOT show the generic error toast
expect(errorMock).not.toHaveBeenCalled()
diff --git a/frontend/src/scenes/experiments/experimentLogic.tsx b/frontend/src/scenes/experiments/experimentLogic.tsx
index 0cb035e0a7c8..5f1a639946e7 100644
--- a/frontend/src/scenes/experiments/experimentLogic.tsx
+++ b/frontend/src/scenes/experiments/experimentLogic.tsx
@@ -17,6 +17,7 @@ import { loaders } from 'kea-loaders'
import { router } from 'kea-router'
import api from 'lib/api'
+import { isApprovalRequiredError } from 'lib/api-error'
import { tryShowMCPHint } from 'lib/components/MCPHint/mcpHintLogic'
import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'
import { FEATURE_FLAGS } from 'lib/constants'
@@ -2833,10 +2834,11 @@ export const experimentLogic = kea<experimentLogicType>([
}
} catch (error: any) {
actions.closeFinishExperimentModal()
- if (error.status === 409 && error.data?.change_request_id) {
+ if (isApprovalRequiredError(error)) {
showApprovalRequiredToast(
error.data.change_request_id,
- 'end this experiment and roll out the winning variant'
+ 'end this experiment and roll out the winning variant',
+ error.data.code
)
dispatchChangeRequestCreated({
resourceType: 'feature_flag',
diff --git a/frontend/src/scenes/feature-flags/featureFlagsLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagsLogic.test.ts
index 7ce63227cede..96f40c9137a0 100644
--- a/frontend/src/scenes/feature-flags/featureFlagsLogic.test.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagsLogic.test.ts
@@ -339,13 +339,14 @@ describe('updateFeatureFlag 409 handling', () => {
])(
'shows approval toast with "$expected" when toggling active=$active gets a 409',
async ({ active, expected }) => {
- const error = { status: 409, data: { change_request_id: 'cr-123' } }
+ const error = { status: 409, data: { change_request_id: 'cr-123', code: 'approval_required' } }
jest.spyOn(api, 'update').mockRejectedValueOnce(error)
logic.actions.updateFeatureFlag({ id: 1, payload: { active } })
await expectLogic(logic).toFinishAllListeners()
- expect(showApprovalRequiredToast).toHaveBeenCalledWith('cr-123', expected)
+ expect(showApprovalRequiredToast).toHaveBeenCalledWith('cr-123', expected, 'approval_required')
+ expect(posthog.captureException).not.toHaveBeenCalled()
}
)
@@ -367,6 +368,8 @@ describe('updateFeatureFlag 409 handling', () => {
await expectLogic(logic).toFinishAllListeners()
expect(showApprovalRequiredToast).not.toHaveBeenCalled()
+ // A non-approval 409 stays visible to error tracking; only approval-shaped ones are suppressed
+ expect(posthog.captureException).toHaveBeenCalledWith(error)
})
})
diff --git a/frontend/src/scenes/feature-flags/flagSelectionLogic.ts b/frontend/src/scenes/feature-flags/flagSelectionLogic.ts
index 2d5b57b4108b..a77a5afeb4de 100644
--- a/frontend/src/scenes/feature-flags/flagSelectionLogic.ts
+++ b/frontend/src/scenes/feature-flags/flagSelectionLogic.ts
@@ -5,7 +5,7 @@ import { beforeUnload } from 'kea-router'
import { lemonToast } from '@posthog/lemon-ui'
import api from 'lib/api'
-import { ApiError } from 'lib/api-error'
+import { ApiError, isApprovalRequiredError } from 'lib/api-error'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { pluralize } from 'lib/utils/strings'
import { dispatchChangeRequestCreated } from 'scenes/approvals/utils'
@@ -17,7 +17,6 @@ import type { CopyFlagsResponseApi } from 'products/feature_flags/frontend/gener
import type { OrganizationType } from '../../types'
import { featureFlagsLogic } from './featureFlagsLogic'
-import { isFlagApprovalRequiredError } from './updateFlagActiveInProject'
export type FlagRolloutState = 'fully_rolled_out' | 'not_rolled_out' | 'partial'
@@ -607,8 +606,8 @@ export const flagSelectionLogic = kea<flagSelectionLogicType>([
try {
await featureFlagsPartialUpdate(String(projectId), id, { archived: true, active: false })
archivedIds.push(id)
- } catch (error) {
- const approvalPending = isFlagApprovalRequiredError(error)
+ } catch (error: any) {
+ const approvalPending = isApprovalRequiredError(error)
if (approvalPending) {
dispatchChangeRequestCreated({ resourceType: 'feature_flag', resourceId: id })
}
diff --git a/frontend/src/scenes/feature-flags/updateFlagActiveInProject.test.ts b/frontend/src/scenes/feature-flags/updateFlagActiveInProject.test.ts
index cdb70184bc87..ffda78223a59 100644
--- a/frontend/src/scenes/feature-flags/updateFlagActiveInProject.test.ts
+++ b/frontend/src/scenes/feature-flags/updateFlagActiveInProject.test.ts
@@ -14,17 +14,24 @@ jest.mock('scenes/approvals/utils', () => ({
}))
describe('updateFlagActiveInProject', () => {
- it('shows the approval toast and announces the change request on an approval-required 409', async () => {
+ it('shows the approval toast with the response code and announces the change request on a 409', async () => {
useMocks({
patch: {
- '/api/projects/:team_id/feature_flags/:id/': () => [409, { change_request_id: 'cr-1' }],
+ '/api/projects/:team_id/feature_flags/:id/': () => [
+ 409,
+ { change_request_id: 'cr-1', code: 'change_request_pending' },
+ ],
},
})
const result = await updateFlagActiveInProject({ teamId: 2, flagId: 42, active: true })
expect(result).toBeNull()
- expect(showApprovalRequiredToast).toHaveBeenCalledWith('cr-1', 'enable this feature flag')
+ expect(showApprovalRequiredToast).toHaveBeenCalledWith(
+ 'cr-1',
+ 'enable this feature flag',
+ 'change_request_pending'
+ )
expect(dispatchChangeRequestCreated).toHaveBeenCalledWith({ resourceType: 'feature_flag', resourceId: 42 })
})
})
diff --git a/frontend/src/scenes/feature-flags/updateFlagActiveInProject.ts b/frontend/src/scenes/feature-flags/updateFlagActiveInProject.ts
index 19c619c7a2d4..ba110d929ea9 100644
--- a/frontend/src/scenes/feature-flags/updateFlagActiveInProject.ts
+++ b/frontend/src/scenes/feature-flags/updateFlagActiveInProject.ts
@@ -1,3 +1,4 @@
+import { isApprovalRequiredError } from 'lib/api-error'
import { LemonDialog } from 'lib/lemon-ui/LemonDialog'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { showApprovalRequiredToast } from 'scenes/approvals/ApprovalRequiredBanner'
@@ -41,22 +42,13 @@ export function confirmFlagActiveToggleInProject({
})
}
-/**
- * Whether a flag update failed because it needs approval, meaning the change was recorded as a
- * change request instead of being applied. Split out from the toast handling below so bulk callers
- * can count these without showing a toast per flag.
- */
-export function isFlagApprovalRequiredError(e: any): boolean {
- return e?.status === 409 && !!e?.data?.change_request_id
-}
-
/**
* Handle an approval-required 409 from a flag update: show the approval toast and announce
* the created change request. Returns whether the error was an approval-required response.
*/
export function handleFlagApprovalRequired(e: any, flagId: number, actionDescription: string): boolean {
- if (isFlagApprovalRequiredError(e)) {
- showApprovalRequiredToast(e.data.change_request_id, actionDescription)
+ if (isApprovalRequiredError(e)) {
+ showApprovalRequiredToast(e.data.change_request_id, actionDescription, e.data.code)
dispatchChangeRequestCreated({ resourceType: 'feature_flag', resourceId: flagId })
return true
}