2
0

🚸 (chatnode) Add proper error message handling

This commit is contained in:
Baptiste Arnaud
2024-02-24 13:22:50 +01:00
parent 7fc8bc9c23
commit fe98f2a9b6

View File

@@ -1,6 +1,6 @@
import { createAction, option } from '@typebot.io/forge' import { createAction, option } from '@typebot.io/forge'
import { isDefined, isEmpty } from '@typebot.io/lib' import { isDefined, isEmpty } from '@typebot.io/lib'
import { got } from 'got' import { HTTPError, got } from 'got'
import { apiBaseUrl } from '../constants' import { apiBaseUrl } from '../constants'
import { auth } from '../auth' import { auth } from '../auth'
import { ChatNodeResponse } from '../types' import { ChatNodeResponse } from '../types'
@@ -36,28 +36,39 @@ export const sendMessage = createAction({
credentials: { apiKey }, credentials: { apiKey },
options: { botId, message, responseMapping, threadId }, options: { botId, message, responseMapping, threadId },
variables, variables,
logs,
}) => { }) => {
const res: ChatNodeResponse = await got try {
.post(apiBaseUrl + botId, { const res: ChatNodeResponse = await got
headers: { .post(apiBaseUrl + botId, {
Authorization: `Bearer ${apiKey}`, headers: {
}, Authorization: `Bearer ${apiKey}`,
json: { },
message, json: {
chat_session_id: isEmpty(threadId) ? undefined : threadId, message,
}, chat_session_id: isEmpty(threadId) ? undefined : threadId,
},
})
.json()
responseMapping?.forEach((mapping) => {
if (!mapping.variableId) return
const item = mapping.item ?? 'Message'
if (item === 'Message') variables.set(mapping.variableId, res.message)
if (item === 'Thread ID')
variables.set(mapping.variableId, res.chat_session_id)
}) })
.json() } catch (error) {
if (error instanceof HTTPError)
responseMapping?.forEach((mapping) => { return logs.add({
if (!mapping.variableId) return status: 'error',
description: error.message,
const item = mapping.item ?? 'Message' details: error.response.body,
if (item === 'Message') variables.set(mapping.variableId, res.message) })
console.error(error)
if (item === 'Thread ID') }
variables.set(mapping.variableId, res.chat_session_id)
})
}, },
}, },
}) })