Skip to content

Commit

Permalink
🚸 (chatnode) Add proper error message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 24, 2024
1 parent 7fc8bc9 commit fe98f2a
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions packages/forge/blocks/chatNode/actions/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createAction, option } from '@typebot.io/forge'
import { isDefined, isEmpty } from '@typebot.io/lib'
import { got } from 'got'
import { HTTPError, got } from 'got'
import { apiBaseUrl } from '../constants'
import { auth } from '../auth'
import { ChatNodeResponse } from '../types'
Expand Down Expand Up @@ -36,28 +36,39 @@ export const sendMessage = createAction({
credentials: { apiKey },
options: { botId, message, responseMapping, threadId },
variables,
logs,
}) => {
const res: ChatNodeResponse = await got
.post(apiBaseUrl + botId, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
json: {
message,
chat_session_id: isEmpty(threadId) ? undefined : threadId,
},
})
.json()
try {
const res: ChatNodeResponse = await got
.post(apiBaseUrl + botId, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
json: {
message,
chat_session_id: isEmpty(threadId) ? undefined : threadId,
},
})
.json()

responseMapping?.forEach((mapping) => {
if (!mapping.variableId) return
responseMapping?.forEach((mapping) => {
if (!mapping.variableId) return

const item = mapping.item ?? 'Message'
if (item === 'Message') variables.set(mapping.variableId, res.message)
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)
})
if (item === 'Thread ID')
variables.set(mapping.variableId, res.chat_session_id)
})
} catch (error) {
if (error instanceof HTTPError)
return logs.add({
status: 'error',
description: error.message,
details: error.response.body,
})
console.error(error)
}
},
},
})

0 comments on commit fe98f2a

Please sign in to comment.