diff --git a/apps/viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts b/apps/viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts index dc32e2dce..8bfa515ce 100644 --- a/apps/viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts +++ b/apps/viewer/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts @@ -142,11 +142,11 @@ export const executeWebhook = ...basicAuth, json: contentType !== 'x-www-form-urlencoded' && body - ? JSON.parse(parseVariables(variables)(body)) + ? safeJsonParse(parseVariables(variables)(body)) : undefined, form: contentType === 'x-www-form-urlencoded' && body - ? JSON.parse(parseVariables(variables)(body)) + ? safeJsonParse(parseVariables(variables)(body)) : undefined, } try { @@ -240,4 +240,12 @@ const convertKeyValueTableToObject = ( }, {}) } +const safeJsonParse = (json: string): any => { + try { + return JSON.parse(json) + } catch (err) { + return json + } +} + export default withSentry(handler) diff --git a/apps/viewer/services/api/utils.ts b/apps/viewer/services/api/utils.ts index 68e5f6dd1..f85a9f802 100644 --- a/apps/viewer/services/api/utils.ts +++ b/apps/viewer/services/api/utils.ts @@ -37,7 +37,7 @@ const saveLog = ( message: string, details?: any ) => { - if (!resultId) return + if (!resultId || resultId === 'undefined') return return prisma.log.create({ data: { resultId, diff --git a/apps/viewer/services/api/webhooks.ts b/apps/viewer/services/api/webhooks.ts index 256c02952..dc860eb35 100644 --- a/apps/viewer/services/api/webhooks.ts +++ b/apps/viewer/services/api/webhooks.ts @@ -105,7 +105,7 @@ const getSampleValue = (step: InputStep) => { case InputStepType.CHOICE: return step.options.isMultipleChoice ? step.items.map((i) => i.content).join(', ') - : step.items[0].content ?? 'Item' + : step.items[0]?.content ?? 'Item' case InputStepType.DATE: return new Date().toUTCString() case InputStepType.EMAIL: diff --git a/apps/viewer/services/result.ts b/apps/viewer/services/result.ts index d7532da7a..8e5bcb5de 100644 --- a/apps/viewer/services/result.ts +++ b/apps/viewer/services/result.ts @@ -1,4 +1,4 @@ -import { Log, Result } from 'db' +import { Result } from 'db' import { sendRequest } from 'utils' export const createResult = async (typebotId: string) => {