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(surveys): show the api error reason when the guided editor fails to save (#117288)

Ben Lea2610320e936a
diff --git a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.test.ts b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.test.ts
index a951ecd26a0b..2f3190e63cd2 100644
--- a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.test.ts
+++ b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.test.ts
@@ -1,6 +1,8 @@
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
+import { lemonToast } from '@posthog/lemon-ui'
+
import { urls } from 'scenes/urls'
import { useMocks } from '~/mocks/jest'
@@ -174,6 +176,49 @@ describe('surveyWizardLogic', () => {
})
})
+ describe('save errors', () => {
+ const validationDetail =
+ "Cohort 'Chat users' has an event-based condition on 'chat_message_sent' (performed_event) and cannot be used in surveys."
+
+ beforeEach(() => {
+ initKeaTests()
+ useMocks({
+ get: {
+ '/api/projects/:team/surveys/': () => [200, { count: 0, results: [], next: null, previous: null }],
+ '/api/projects/:team/surveys/responses_count': () => [200, {}],
+ },
+ post: {
+ '/api/projects/:team/surveys/': () => [
+ 400,
+ {
+ type: 'validation_error',
+ code: 'behavioral_cohort_found',
+ detail: validationDetail,
+ attr: null,
+ },
+ ],
+ },
+ })
+ })
+
+ test.each([['launchSurvey' as const], ['saveDraft' as const]])(
+ '%s shows the validation detail from the API',
+ async (action) => {
+ const toastError = jest.spyOn(lemonToast, 'error')
+ const logic = surveyWizardLogic({ id: 'new' })
+ logic.mount()
+
+ await expectLogic(logic, () => {
+ logic.actions.selectTemplate(createMockTemplate())
+ logic.actions[action]()
+ }).toFinishAllListeners()
+
+ expect(toastError).toHaveBeenCalledWith(validationDetail)
+ toastError.mockRestore()
+ }
+ )
+ })
+
describe('wizard step navigation', () => {
beforeEach(() => {
initKeaTests()
diff --git a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
index 7bcf1851b4f3..06f899351d30 100644
--- a/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
+++ b/frontend/src/scenes/surveys/wizard/surveyWizardLogic.ts
@@ -18,6 +18,7 @@ import { router, urlToAction } from 'kea-router'
import { lemonToast } from '@posthog/lemon-ui'
import api from 'lib/api'
+import { ApiError } from 'lib/api-error'
import { dayjs } from 'lib/dayjs'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { Scene } from 'scenes/sceneTypes'
@@ -65,6 +66,10 @@ function templateMatchesMode(template: SurveyTemplate, mode: SurveyTemplateMode)
return !template.modes || template.modes.includes(mode)
}
+function errorDetailOr(error: unknown, fallback: string): string {
+ return (error instanceof ApiError && error.detail) || fallback
+}
+
export interface SurveyWizardLogicProps {
id: string // 'new' for new surveys, or a UUID for editing
}
@@ -508,7 +513,7 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([
actions.launchSurveySuccess(createdSurvey)
} catch (e) {
actions.launchSurveyFailure(String(e))
- lemonToast.error('Failed to create survey')
+ lemonToast.error(errorDetailOr(e, 'Failed to create survey'))
}
},
launchSurveySuccess: ({ survey }) => {
@@ -539,7 +544,7 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([
actions.saveDraftSuccess(createdSurvey)
} catch (e) {
actions.saveDraftFailure(String(e))
- lemonToast.error('Failed to save draft')
+ lemonToast.error(errorDetailOr(e, 'Failed to save draft'))
}
},
saveDraftSuccess: ({ survey }) => {
@@ -562,7 +567,7 @@ export const surveyWizardLogic = kea<surveyWizardLogicType>([
actions.updateSurveySuccess(updatedSurvey)
} catch (e) {
actions.updateSurveyFailure(String(e))
- lemonToast.error('Failed to update survey')
+ lemonToast.error(errorDetailOr(e, 'Failed to update survey'))
}
},
updateSurveySuccess: ({ survey }) => {