: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
import { describe, expect, it, vi } from 'vitest'
import { handleToolError, PostHogApiError, PostHogValidationError, wrapError } from '@/lib/errors'
import { getToolRecoveryHint } from '@/lib/tool-error-hints'
vi.mock('@/lib/posthog', () => ({
getPostHogClient: () => ({ captureException: vi.fn() }),
}))
const LOGS_QUERY_URL = 'https://us.posthog.com/api/projects/2/logs/query/'
describe('getToolRecoveryHint', () => {
it('returns the narrow-and-retry hint for a 5xx on a logs query endpoint', () => {
const hint = getToolRecoveryHint({ url: LOGS_QUERY_URL, status: 500 })
expect(hint).not.toBeUndefined()
expect(hint).toContain('scans too much data')
expect(hint).toContain('logs-count')
expect(hint).toContain('logs-count-ranges')
expect(hint).toContain('serviceNames')
})
it.each([
'https://us.posthog.com/api/projects/2/logs/count/',
'https://us.posthog.com/api/projects/2/logs/count-ranges/',
'https://us.posthog.com/api/projects/2/logs/services/',
'https://us.posthog.com/api/projects/2/logs/sparkline/',
])('also fires for the sibling logs query endpoint %s', (url: string) => {
expect(getToolRecoveryHint({ url, status: 503 })).not.toBeUndefined()
})
it.each([400, 404])('does not fire for %i — those carry an actionable detail already', (status) => {
expect(getToolRecoveryHint({ url: LOGS_QUERY_URL, status })).toBeUndefined()
})
it('does not fire for unrelated endpoints', () => {
expect(
getToolRecoveryHint({ url: 'https://us.posthog.com/api/projects/2/insights/', status: 500 })
).toBeUndefined()
})
it('fires when status is unknown but the URL is a logs query endpoint', () => {
expect(getToolRecoveryHint({ url: LOGS_QUERY_URL })).not.toBeUndefined()
})
it.each([
'https://us.posthog.com/api/projects/2/tracing/spans/query/',
'https://us.posthog.com/api/projects/2/tracing/spans/count/',
'https://us.posthog.com/api/projects/2/tracing/spans/aggregate/',
'https://us.posthog.com/api/projects/2/tracing/spans/service-health/',
])('returns the tracing narrow-and-retry hint for a 5xx on %s', (url: string) => {
const hint = getToolRecoveryHint({ url, status: 500 })
expect(hint).not.toBeUndefined()
expect(hint).toContain('serviceNames')
expect(hint).toContain('apm-service-health')
expect(hint).not.toContain('logs-count-ranges')
})
it('does not fire the tracing hint for a 400 on a tracing endpoint', () => {
expect(
getToolRecoveryHint({ url: 'https://us.posthog.com/api/projects/2/tracing/spans/count/', status: 400 })
).toBeUndefined()
})
it('also fires the logs hint for the new logs aggregate endpoints', () => {
for (const path of ['group-by', 'context', 'environments']) {
expect(
getToolRecoveryHint({ url: `https://us.posthog.com/api/projects/2/logs/${path}/`, status: 500 })
).not.toBeUndefined()
}
})
it.each([
'https://us.posthog.com/api/projects/2/surveys/',
'https://us.posthog.com/api/projects/2/surveys/018f-abc/',
])('returns the structured-param hint for a 5xx on the survey write endpoint %s', (url: string) => {
const hint = getToolRecoveryHint({ url, status: 500 })
expect(hint).not.toBeUndefined()
expect(hint).toContain('JSON-encoded string')
expect(hint).toContain('targeting_flag_filters')
})
it('does not fire for a survey sub-action endpoint — its 5xx is a query problem', () => {
expect(
getToolRecoveryHint({ url: 'https://us.posthog.com/api/projects/2/surveys/018f-abc/stats/', status: 500 })
).toBeUndefined()
})
})
describe('handleToolError recovery hints', () => {
it('appends the recovery hint to a 5xx logs query failure', () => {
const error = new PostHogApiError({
status: 500,
statusText: 'Internal Server Error',
body: '{"detail":"timeout"}',
url: LOGS_QUERY_URL,
method: 'POST',
})
const result = handleToolError(error, 'query-logs')
const [content] = result.content as Array<{ type: string; text: string }>
expect(content?.text).toContain('[query-logs]')
expect(content?.text).toContain('Status Code: 500')
expect(content?.text).toContain('Narrow the query and retry')
})
it('reports the request path without the upstream host', () => {
const error = new PostHogApiError({
status: 404,
statusText: 'Not Found',
body: '{"detail":"Not found."}',
url: 'https://internal.example.com/api/projects/2/insights/?limit=1',
method: 'GET',
})
const result = handleToolError(error, 'insight-get')
const [content] = result.content as Array<{ type: string; text: string }>
expect(content?.text).toContain('Path: GET /api/projects/2/insights/?limit=1')
expect(content?.text).not.toContain('internal.example.com')
})
it('appends the hint even when the typed error is hidden behind Error.cause', () => {
const original = new PostHogApiError({
status: 503,
statusText: 'Service Unavailable',
body: 'upstream timeout',
url: LOGS_QUERY_URL,
method: 'POST',
})
const wrapped = wrapError('Failed to query logs', original)
const result = handleToolError(wrapped, 'query-logs')
const [content] = result.content as Array<{ type: string; text: string }>
expect(content?.text).toContain('logs-count-ranges')
})
it('does not append a hint for a 4xx (no noise on recoverable input errors)', () => {
const error = new PostHogValidationError({
detail: 'invalid filter',
attr: 'filterGroup',
code: 'invalid',
extra: undefined,
url: LOGS_QUERY_URL,
method: 'POST',
})
const result = handleToolError(error, 'query-logs')
const [content] = result.content as Array<{ type: string; text: string }>
expect(content?.text).not.toContain('Narrow the query and retry')
})
it('does not append a hint for a 5xx on an unrelated tool', () => {
const error = new PostHogApiError({
status: 500,
statusText: 'Internal Server Error',
body: 'boom',
url: 'https://us.posthog.com/api/projects/2/insights/',
method: 'GET',
})
const result = handleToolError(error, 'insights-list')
const [content] = result.content as Array<{ type: string; text: string }>
expect(content?.text).not.toContain('Narrow the query and retry')
})
})