: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(data-warehouse): stop reporting rejected self-managed csvs as exceptions (#116105)
posthog[bot]5bbf347cfa2b
diff --git a/frontend/src/initKea.ts b/frontend/src/initKea.tsindex 9945124a0e77..0df82334744d 100644--- a/frontend/src/initKea.ts+++ b/frontend/src/initKea.ts@@ -110,11 +110,15 @@ const ERROR_FILTER_ALLOW_LIST_BY_LOGIC_PATH: Record<string, string> = {}// A keyed logic's path ends with its key, so match the owner path as a prefix.-function isErrorSelfHandledByLogic(actionKey: string, logicPath: string): boolean {- const ownerPath = ERROR_FILTER_ALLOW_LIST_BY_LOGIC_PATH[actionKey]+function isOwnedByLogic(owners: Record<string, string>, actionKey: string, logicPath: string): boolean {+ const ownerPath = owners[actionKey]return !!ownerPath && (logicPath === ownerPath || logicPath.startsWith(`${ownerPath}.`))}+function isErrorSelfHandledByLogic(actionKey: string, logicPath: string): boolean {+ return isOwnedByLogic(ERROR_FILTER_ALLOW_LIST_BY_LOGIC_PATH, actionKey, logicPath)+}+/*Write actions that show their own friendly message for access-denied 403s(code `permission_denied`), so the generic toast would be a duplicate.@@ -150,6 +154,16 @@ const FIELD_ERROR_SELF_HANDLED: Record<string, Set<string>> = {const HAS_DEPENDENTS_SELF_HANDLED = new Set(['deleteDataWarehouseSavedQuery'])+/*+Write actions whose owning logic toasts the server message of a validation 400. The 400 is a+response to user input, such as a malformed CSV, so it is not reported as an exception.+Every other failure on these actions still toasts and reports.+*/+const VALIDATION_ERROR_SELF_HANDLED_BY_LOGIC_PATH: Record<string, string> = {+ createTable: 'products.dataWarehouse.selfManagedSourceLogic',+ updateTable: 'products.dataWarehouse.selfManagedSourceLogic',+}+/*Write actions whose own logic marks the form row when the backend rejects an existing member(code `existing_member`). It is a validation result, so it is not reported as an exception.@@ -242,6 +256,9 @@ export function initKea({// when a click, an Enter key press, or a form submit started the request.const isAccessDenied =isAccessDeniedError(error) && (isLoadAction || ACCESS_DENIED_SELF_HANDLED.has(String(actionKey)))+ const isSelfHandledValidationError =+ error?.status === 400 &&+ isOwnedByLogic(VALIDATION_ERROR_SELF_HANDLED_BY_LOGIC_PATH, actionKey, logic.pathString)if (!ERROR_FILTER_ALLOW_LIST.includes(actionKey) &&!isErrorSelfHandledByLogic(actionKey, logic.pathString) &&@@ -286,7 +303,8 @@ export function initKea({isReadOnlyImpersonationError ||isFeatureFlagDuplicateKey ||isHasDependentsError ||- isSelfHandledFieldError+ isSelfHandledFieldError ||+ isSelfHandledValidationError) {// These are handled by their own dedicated toasts elsewhere.errorMessage = null@@ -312,7 +330,12 @@ 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))- if (shouldReportApiFailure(error) && !isSelfHandledNotFound && !isSelfHandledExistingMember) {+ if (+ shouldReportApiFailure(error) &&+ !isSelfHandledNotFound &&+ !isSelfHandledExistingMember &&+ !isSelfHandledValidationError+ ) {posthog.captureException(error)}},diff --git a/products/data_warehouse/frontend/scenes/NewSourceScene/selfManagedSourceLogic.test.ts b/products/data_warehouse/frontend/scenes/NewSourceScene/selfManagedSourceLogic.test.tsindex 535f174bd7c7..748d256ab498 100644--- a/products/data_warehouse/frontend/scenes/NewSourceScene/selfManagedSourceLogic.test.ts+++ b/products/data_warehouse/frontend/scenes/NewSourceScene/selfManagedSourceLogic.test.ts@@ -1,6 +1,17 @@+import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'++import { lemonToast } from '@posthog/lemon-ui'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'import { DataWarehouseTable } from '~/types'-import { selfManagedTableFormErrors, selfManagedTableUpdatePayload } from './selfManagedSourceLogic'+import {+ selfManagedSourceLogic,+ selfManagedTableFormErrors,+ selfManagedTableUpdatePayload,+} from './selfManagedSourceLogic'const validTable: Partial<DataWarehouseTable> = {name: 'stripe_invoices',@@ -69,4 +80,45 @@ describe('selfManagedSourceLogic', () => {})})})++ describe('table save failures', () => {+ beforeEach(() => {+ initKeaTests()+ jest.spyOn(lemonToast, 'error').mockImplementation(() => ({ id: 'x' }) as any)+ })++ afterEach(() => {+ jest.restoreAllMocks()+ })++ // A rejected CSV is a validation 400. It must toast the server message without filing an exception.+ it.each([+ { id: 'new', method: 'post', action: 'createTable', status: 400, reported: false },+ { id: 'some-id', method: 'patch', action: 'updateTable', status: 400, reported: false },+ { id: 'new', method: 'post', action: 'createTable', status: 500, reported: true },+ ] as const)(+ '$action with a $status toasts the server message, reported: $reported',+ async ({ id, method, action, status, reported }) => {+ useMocks({+ get: { '/api/projects/:team_id/warehouse_tables/:id/': () => [200, validTable] },+ [method]: {+ '/api/projects/:team_id/warehouse_tables/': () => [status, { detail: 'Check the delimiter.' }],+ '/api/projects/:team_id/warehouse_tables/:id/': () => [+ status,+ { detail: 'Check the delimiter.' },+ ],+ },+ })+ const logic = selfManagedSourceLogic({ id })+ logic.mount()++ await expectLogic(logic, () => {+ logic.actions[action]({ ...validTable, format: 'CSVWithNames' })+ }).toDispatchActions([`${action}Failure`])++ expect(lemonToast.error).toHaveBeenCalledWith(expect.stringContaining('Check the delimiter.'))+ expect(posthog.captureException).toHaveBeenCalledTimes(reported ? 1 : 0)+ }+ )+ })})diff --git a/products/data_warehouse/frontend/scenes/NewSourceScene/selfManagedSourceLogic.tsx b/products/data_warehouse/frontend/scenes/NewSourceScene/selfManagedSourceLogic.tsxindex 871cdb9bd839..af977277b75f 100644--- a/products/data_warehouse/frontend/scenes/NewSourceScene/selfManagedSourceLogic.tsx+++ b/products/data_warehouse/frontend/scenes/NewSourceScene/selfManagedSourceLogic.tsx@@ -7,6 +7,7 @@ import { router } from 'kea-router'import { lemonToast } from '@posthog/lemon-ui'import api from 'lib/api'+import { readableErrorMessage } from 'lib/api-error'import { databaseTableListLogic } from 'scenes/data-management/database/databaseTableListLogic'import { urls } from 'scenes/urls'@@ -288,6 +289,17 @@ export const selfManagedSourceLogic = kea<selfManagedSourceLogicType>([},})),listeners(({ actions, props }) => ({+ // initKea leaves a validation 400 from these loaders to this logic, so toast the server message here.+ createTableFailure: ({ errorObject }) => {+ if (errorObject?.status === 400) {+ lemonToast.error(readableErrorMessage(errorObject) ?? 'Could not create the table.')+ }+ },+ updateTableFailure: ({ errorObject }) => {+ if (errorObject?.status === 400) {+ lemonToast.error(readableErrorMessage(errorObject) ?? 'Could not update the table.')+ }+ },createTableSuccess: async ({ table }) => {lemonToast.success(<>Table {table.name} created</>)actions.loadDatabase()@@ -324,14 +336,10 @@ export const selfManagedSourceLogic = kea<selfManagedSourceLogicType>([defaults: { ...NEW_WAREHOUSE_TABLE } as DataWarehouseTable,errors: selfManagedTableFormErrors,submit: async (tablePayload) => {- try {- if (props.id && props.id !== 'new') {- actions.updateTable(tablePayload)- } else {- actions.createTable(tablePayload)- }- } catch (e: any) {- lemonToast.error(e.data?.message ?? e.message)+ if (props.id && props.id !== 'new') {+ actions.updateTable(tablePayload)+ } else {+ actions.createTable(tablePayload)}},},