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(projects): show the duplicate name error when creating a project (#129810)

Rafael Audibertfa28706c5bef
diff --git a/frontend/src/initKea.ts b/frontend/src/initKea.ts
index 4e5b0877dd66..325472369263 100644
--- a/frontend/src/initKea.ts
+++ b/frontend/src/initKea.ts
@@ -231,6 +231,19 @@ const CONTENT_EXPIRED_SELF_HANDLED_BY_LOGIC_PATH: Record<string, string> = {
generateSummary: 'products.ai_observability.frontend.summary-view.summaryViewLogic',
}
+// projectLogic toasts a duplicate project name 409 from these actions.
+const CONFLICT_SELF_HANDLED_BY_LOGIC_PATH: Record<string, string> = {
+ createProject: 'scenes.projectLogic',
+ updateCurrentProject: 'scenes.projectLogic',
+}
+
+function isExpectedFailureOwnedByLogic(error: any, actionKey: string, logicPath: string): boolean {
+ if (error?.status === 404 && error?.code === 'content_expired') {
+ return isOwnedByLogic(CONTENT_EXPIRED_SELF_HANDLED_BY_LOGIC_PATH, actionKey, logicPath)
+ }
+ return error?.status === 409 && isOwnedByLogic(CONFLICT_SELF_HANDLED_BY_LOGIC_PATH, actionKey, logicPath)
+}
+
interface InitKeaProps {
state?: Record<string, any>
routerHistory?: any
@@ -384,15 +397,11 @@ export function initKea({
NOT_FOUND_SELF_HANDLED.has(String(actionKey)) && isUnavailableEndpointError(error)
const isSelfHandledExistingMember =
error?.code === 'existing_member' && EXISTING_MEMBER_SELF_HANDLED.has(String(actionKey))
- const isSelfHandledContentExpired =
- error?.status === 404 &&
- error?.code === 'content_expired' &&
- isOwnedByLogic(CONTENT_EXPIRED_SELF_HANDLED_BY_LOGIC_PATH, actionKey, logic.pathString)
if (
shouldReportApiFailure(error) &&
!isSelfHandledNotFound &&
!isSelfHandledExistingMember &&
- !isSelfHandledContentExpired &&
+ !isExpectedFailureOwnedByLogic(error, actionKey, logic.pathString) &&
!isSelfHandledValidationError
) {
posthog.captureException(error)
diff --git a/frontend/src/scenes/projectLogic.test.ts b/frontend/src/scenes/projectLogic.test.ts
index cf1cc0f04ec7..14149a7d0699 100644
--- a/frontend/src/scenes/projectLogic.test.ts
+++ b/frontend/src/scenes/projectLogic.test.ts
@@ -4,6 +4,8 @@ import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import posthog from 'posthog-js'
+import { lemonToast } from 'lib/lemon-ui/LemonToast'
+
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
@@ -101,4 +103,36 @@ describe('projectLogic', () => {
expect(router.values.location.pathname === path).toBe(staysOpen)
})
+
+ it.each([
+ ['createProject', 'createProjectFailure'],
+ ['updateCurrentProject', 'updateCurrentProjectFailure'],
+ ] as const)('%s toasts a duplicate name 409 and does not report it', async (actionName, failureAction) => {
+ const detail = 'There is already a project called "Taken" in this organization. Choose a different name.'
+ useMocks({
+ post: { '/api/projects/': () => [409, { type: 'validation_error', code: 'conflict', detail }] },
+ patch: { '/api/projects/:id': () => [409, { type: 'validation_error', code: 'conflict', detail }] },
+ })
+ initKeaTests()
+ const logic = projectLogic()
+ logic.mount()
+ logic.actions.loadCurrentProjectSuccess(MOCK_DEFAULT_PROJECT)
+ const toastError = jest.spyOn(lemonToast, 'error').mockReturnValue('' as any)
+ const captureException = jest.spyOn(posthog, 'captureException').mockImplementation(() => undefined as any)
+ try {
+ await expectLogic(logic, () => {
+ if (actionName === 'createProject') {
+ logic.actions.createProject({ name: 'Taken' })
+ } else {
+ logic.actions.updateCurrentProject({ name: 'Taken' })
+ }
+ }).toDispatchActions([failureAction])
+
+ expect(toastError).toHaveBeenCalledWith(detail)
+ expect(captureException).not.toHaveBeenCalled()
+ } finally {
+ toastError.mockRestore()
+ captureException.mockRestore()
+ }
+ })
})
diff --git a/frontend/src/scenes/projectLogic.ts b/frontend/src/scenes/projectLogic.ts
index 75f76c543d8b..c91c5accfef8 100644
--- a/frontend/src/scenes/projectLogic.ts
+++ b/frontend/src/scenes/projectLogic.ts
@@ -22,6 +22,13 @@ import { userLogic } from './userLogic'
const CREATE_PROJECT_TIMEOUT_MS = 60_000
+function toastDuplicateNameConflict(error: string, errorObject: unknown): void {
+ const apiError = errorObject as ApiError | undefined
+ if (apiError?.status === 409) {
+ lemonToast.error(apiError.detail || error)
+ }
+}
+
export function isPathOpenWhileProjectPendingDeletion(pathname: string): boolean {
// projectBased scenes are served under a /project/:id prefix, so match the lockout path by suffix.
// Tickets load from PostHog's support project, not this one, and reply emails link to them.
@@ -350,10 +357,10 @@ export const projectLogic = kea<projectLogicType>([
actions.guardPendingDeletion(pathname, values.currentProject?.is_pending_deletion)
},
updateCurrentProjectFailure: ({ error, errorObject }) => {
- const apiError = errorObject as ApiError | undefined
- if (apiError?.status === 409) {
- lemonToast.error(apiError.detail || error)
- }
+ toastDuplicateNameConflict(error, errorObject)
+ },
+ createProjectFailure: ({ error, errorObject }) => {
+ toastDuplicateNameConflict(error, errorObject)
},
guardPendingDeletion: ({ pathname, isPendingDeletion }) => {
const onLockoutScreen = pathname.endsWith(urls.projectPendingDeletion())