Skip to content

Commit df10eb7

Browse files
committed
fix(posthog): fail loudly instead of silently dropping data on bad JSON params
Several tools caught JSON.parse failures on user-supplied filter/query/ parameters strings and silently substituted {} or null, which would wipe the corresponding field on the PATCH/create request instead of surfacing an error (Cursor Bugbot flagged this for update_cohort, update_experiment, update_insight; the same pattern existed in their create_* / update_feature_flag / evaluate_flags counterparts, fixed for consistency). Now throws a descriptive error, matching the existing convention in tools/notion/query_database.ts. Also fixes batch_events: the request body silently sent an empty batch on invalid JSON, and transformResponse always reported "captured successfully" even when PostHog's response indicated failure (data.status !== 1).
1 parent 7ebcc19 commit df10eb7

8 files changed

Lines changed: 49 additions & 39 deletions

File tree

apps/sim/tools/posthog/batch_events.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@sim/utils/errors'
12
import { getPostHogIngestBaseUrl } from '@/tools/posthog/utils'
23
import type { ToolConfig } from '@/tools/types'
34

@@ -63,33 +64,35 @@ export const batchEventsTool: ToolConfig<PostHogBatchEventsParams, PostHogBatchE
6364
'Content-Type': 'application/json',
6465
}),
6566
body: (params) => {
66-
let batch: any[]
67+
let batch: unknown
6768
try {
6869
batch = JSON.parse(params.batch)
69-
} catch (e) {
70-
batch = []
70+
} catch (error) {
71+
throw new Error(`Invalid batch JSON: ${getErrorMessage(error)}`)
72+
}
73+
if (!Array.isArray(batch)) {
74+
throw new Error('batch must be a JSON array of events')
7175
}
7276

7377
return {
7478
api_key: params.projectApiKey,
75-
batch: batch,
79+
batch,
7680
}
7781
},
7882
},
7983

8084
transformResponse: async (response: Response, params) => {
8185
const data = await response.json()
82-
let eventsProcessed = 0
83-
try {
84-
eventsProcessed = params ? JSON.parse(params.batch).length : 0
85-
} catch {
86-
eventsProcessed = 0
87-
}
86+
const eventsProcessed = params ? (JSON.parse(params.batch) as unknown[]).length : 0
87+
const success = data.status === 1
88+
8889
return {
89-
success: true,
90+
success,
9091
output: {
91-
status: 'Batch events captured successfully',
92-
events_processed: data.status === 1 ? eventsProcessed : 0,
92+
status: success
93+
? 'Batch events captured successfully'
94+
: `Batch events capture failed (status: ${data.status})`,
95+
events_processed: success ? eventsProcessed : 0,
9396
},
9497
}
9598
},

apps/sim/tools/posthog/create_experiment.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@sim/utils/errors'
12
import { getPostHogAppBaseUrl } from '@/tools/posthog/utils'
23
import type { ToolConfig } from '@/tools/types'
34

@@ -134,16 +135,16 @@ export const createExperimentTool: ToolConfig<CreateExperimentParams, CreateExpe
134135
if (params.parameters) {
135136
try {
136137
body.parameters = JSON.parse(params.parameters)
137-
} catch {
138-
body.parameters = {}
138+
} catch (error) {
139+
throw new Error(`Invalid parameters JSON: ${getErrorMessage(error)}`)
139140
}
140141
}
141142

142143
if (params.filters) {
143144
try {
144145
body.filters = JSON.parse(params.filters)
145-
} catch {
146-
body.filters = {}
146+
} catch (error) {
147+
throw new Error(`Invalid filters JSON: ${getErrorMessage(error)}`)
147148
}
148149
}
149150

apps/sim/tools/posthog/create_feature_flag.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@sim/utils/errors'
12
import { getPostHogAppBaseUrl } from '@/tools/posthog/utils'
23
import type { ToolConfig } from '@/tools/types'
34

@@ -125,8 +126,8 @@ export const createFeatureFlagTool: ToolConfig<CreateFeatureFlagParams, CreateFe
125126
if (params.filters) {
126127
try {
127128
body.filters = JSON.parse(params.filters)
128-
} catch {
129-
body.filters = {}
129+
} catch (error) {
130+
throw new Error(`Invalid filters JSON: ${getErrorMessage(error)}`)
130131
}
131132
}
132133

apps/sim/tools/posthog/evaluate_flags.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@sim/utils/errors'
12
import { getPostHogIngestBaseUrl } from '@/tools/posthog/utils'
23
import type { ToolConfig } from '@/tools/types'
34

@@ -92,24 +93,24 @@ export const evaluateFlagsTool: ToolConfig<EvaluateFlagsParams, EvaluateFlagsRes
9293
if (params.groups) {
9394
try {
9495
body.groups = JSON.parse(params.groups)
95-
} catch {
96-
body.groups = {}
96+
} catch (error) {
97+
throw new Error(`Invalid groups JSON: ${getErrorMessage(error)}`)
9798
}
9899
}
99100

100101
if (params.personProperties) {
101102
try {
102103
body.person_properties = JSON.parse(params.personProperties)
103-
} catch {
104-
body.person_properties = {}
104+
} catch (error) {
105+
throw new Error(`Invalid personProperties JSON: ${getErrorMessage(error)}`)
105106
}
106107
}
107108

108109
if (params.groupProperties) {
109110
try {
110111
body.group_properties = JSON.parse(params.groupProperties)
111-
} catch {
112-
body.group_properties = {}
112+
} catch (error) {
113+
throw new Error(`Invalid groupProperties JSON: ${getErrorMessage(error)}`)
113114
}
114115
}
115116

apps/sim/tools/posthog/update_cohort.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@sim/utils/errors'
12
import { getPostHogAppBaseUrl } from '@/tools/posthog/utils'
23
import type { ToolConfig } from '@/tools/types'
34

@@ -139,16 +140,16 @@ export const updateCohortTool: ToolConfig<PostHogUpdateCohortParams, PostHogUpda
139140
if (params.filters) {
140141
try {
141142
body.filters = JSON.parse(params.filters)
142-
} catch {
143-
body.filters = {}
143+
} catch (error) {
144+
throw new Error(`Invalid filters JSON: ${getErrorMessage(error)}`)
144145
}
145146
}
146147

147148
if (params.query) {
148149
try {
149150
body.query = JSON.parse(params.query)
150-
} catch {
151-
body.query = null
151+
} catch (error) {
152+
throw new Error(`Invalid query JSON: ${getErrorMessage(error)}`)
152153
}
153154
}
154155

@@ -157,8 +158,8 @@ export const updateCohortTool: ToolConfig<PostHogUpdateCohortParams, PostHogUpda
157158
if (params.groups) {
158159
try {
159160
body.groups = JSON.parse(params.groups)
160-
} catch {
161-
body.groups = []
161+
} catch (error) {
162+
throw new Error(`Invalid groups JSON: ${getErrorMessage(error)}`)
162163
}
163164
}
164165

apps/sim/tools/posthog/update_experiment.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@sim/utils/errors'
12
import { getPostHogAppBaseUrl } from '@/tools/posthog/utils'
23
import type { ToolConfig } from '@/tools/types'
34

@@ -138,16 +139,16 @@ export const updateExperimentTool: ToolConfig<UpdateExperimentParams, UpdateExpe
138139
if (params.parameters) {
139140
try {
140141
body.parameters = JSON.parse(params.parameters)
141-
} catch {
142-
body.parameters = {}
142+
} catch (error) {
143+
throw new Error(`Invalid parameters JSON: ${getErrorMessage(error)}`)
143144
}
144145
}
145146

146147
if (params.filters) {
147148
try {
148149
body.filters = JSON.parse(params.filters)
149-
} catch {
150-
body.filters = {}
150+
} catch (error) {
151+
throw new Error(`Invalid filters JSON: ${getErrorMessage(error)}`)
151152
}
152153
}
153154

apps/sim/tools/posthog/update_feature_flag.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@sim/utils/errors'
12
import { getPostHogAppBaseUrl } from '@/tools/posthog/utils'
23
import type { ToolConfig } from '@/tools/types'
34

@@ -135,8 +136,8 @@ export const updateFeatureFlagTool: ToolConfig<UpdateFeatureFlagParams, UpdateFe
135136
if (params.filters) {
136137
try {
137138
body.filters = JSON.parse(params.filters)
138-
} catch {
139-
body.filters = {}
139+
} catch (error) {
140+
throw new Error(`Invalid filters JSON: ${getErrorMessage(error)}`)
140141
}
141142
}
142143

apps/sim/tools/posthog/update_insight.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getErrorMessage } from '@sim/utils/errors'
12
import { getPostHogAppBaseUrl } from '@/tools/posthog/utils'
23
import type { ToolConfig } from '@/tools/types'
34

@@ -131,8 +132,8 @@ export const updateInsightTool: ToolConfig<
131132
if (params.query) {
132133
try {
133134
body.query = JSON.parse(params.query)
134-
} catch {
135-
body.query = null
135+
} catch (error) {
136+
throw new Error(`Invalid query JSON: ${getErrorMessage(error)}`)
136137
}
137138
}
138139

0 commit comments

Comments
 (0)