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(settings): drop a personal api key row when its delete returns 404 (#129557)

Rafael Audibert711b5d2d8add
diff --git a/frontend/src/scenes/settings/user/personalAPIKeysLogic.test.ts b/frontend/src/scenes/settings/user/personalAPIKeysLogic.test.ts
index 3912b8e070d4..69b822122b01 100644
--- a/frontend/src/scenes/settings/user/personalAPIKeysLogic.test.ts
+++ b/frontend/src/scenes/settings/user/personalAPIKeysLogic.test.ts
@@ -13,6 +13,7 @@ import { userLogic } from 'scenes/userLogic'
import { themeLogic } from '~/layout/navigation-3000/themeLogic'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
+import { PersonalAPIKeyType } from '~/types'
import { expiresAtForOption, personalAPIKeysLogic } from './personalAPIKeysLogic'
@@ -141,6 +142,24 @@ describe('personalAPIKeysLogic', () => {
expect(logic.values.editingKeyId).toBe('new')
})
+ test.each([
+ { status: 404, expectedKeyIds: [] },
+ { status: 500, expectedKeyIds: ['stale-key'] },
+ ])('a delete that returns $status leaves keys $expectedKeyIds', async ({ status, expectedKeyIds }) => {
+ jest.spyOn(lemonToast, 'error').mockImplementation(() => '')
+ useMocks({
+ delete: {
+ '/api/personal_api_keys/:id/': () => [status, { detail: 'Not found.' }],
+ },
+ })
+ logic.actions.loadKeysSuccess([{ id: 'stale-key', label: 'Stale key' } as PersonalAPIKeyType])
+
+ logic.actions.deleteKey('stale-key')
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.keys.map((key) => key.id)).toEqual(expectedKeyIds)
+ })
+
it('preserves llm_gateway scopes from create payload when GATEWAY_PERSONAL_API_KEY flag is enabled', async () => {
featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.GATEWAY_PERSONAL_API_KEY], {
[FEATURE_FLAGS.GATEWAY_PERSONAL_API_KEY]: true,
diff --git a/frontend/src/scenes/settings/user/personalAPIKeysLogic.tsx b/frontend/src/scenes/settings/user/personalAPIKeysLogic.tsx
index a97521f7b9d8..79c9a6362116 100644
--- a/frontend/src/scenes/settings/user/personalAPIKeysLogic.tsx
+++ b/frontend/src/scenes/settings/user/personalAPIKeysLogic.tsx
@@ -403,7 +403,17 @@ export const personalAPIKeysLogic = kea<personalAPIKeysLogicType>([
return await api.personalApiKeys.list()
},
deleteKey: async ({ id }) => {
- await api.personalApiKeys.delete(id)
+ try {
+ await api.personalApiKeys.delete(id)
+ } catch (error) {
+ // A 404 means the signed-in user has no key with this id, usually because another tab
+ // already deleted it. The row is stale, so remove it. Otherwise every later delete click
+ // on the row fails with the same error. A list reload here could finish after a later
+ // delete and put that key back, so only this row changes.
+ if (!(error instanceof ApiError && error.status === 404)) {
+ throw error
+ }
+ }
return values.keys.filter((filteredKey) => filteredKey.id != id)
},
rollKey: async ({ id, expiresAt }) => {