diff --git a/src/utils/query.ts b/src/utils/query.ts index d09e3ab2..d6753108 100644 --- a/src/utils/query.ts +++ b/src/utils/query.ts @@ -3,6 +3,8 @@ import type { ZodSchema } from 'zod'; import { zodToJsonSchema } from 'zod-to-json-schema'; import type { AIModel, CloudRegion } from './types'; import { getCloudUrlFromRegion } from './urls'; +import { analytics } from './analytics'; +import { AxiosError } from 'axios'; export const query = async ({ message, @@ -19,19 +21,33 @@ export const query = async ({ }): Promise => { const jsonSchema = zodToJsonSchema(schema, 'schema').definitions; - const response = await axios.post<{ data: unknown }>( - `${getCloudUrlFromRegion(region)}/api/wizard/query`, - { - message, - model, - json_schema: { ...jsonSchema, name: 'schema', strict: true }, - }, - { - headers: { - 'X-PostHog-Wizard-Hash': wizardHash, + const response = await axios + .post<{ data: unknown }>( + `${getCloudUrlFromRegion(region)}/api/wizard/query`, + { + message, + model, + json_schema: { ...jsonSchema, name: 'schema', strict: true }, }, - }, - ); + { + headers: { + 'X-PostHog-Wizard-Hash': wizardHash, + }, + }, + ) + .catch((error) => { + if (error instanceof AxiosError) { + analytics.captureException(error, { + response_status_code: error.response?.status, + message, + model, + json_schema: jsonSchema, + type: 'wizard_query_error', + }); + } + + throw error; + }); const validation = schema.safeParse(response.data.data);