2
0

🔊 Add response debug log for failing requests without errors

This commit is contained in:
Baptiste Arnaud
2023-11-17 15:42:40 +01:00
parent 3a47a0fcbd
commit 5298538ecb
7 changed files with 17 additions and 12 deletions

View File

@@ -5,7 +5,7 @@
"i18n-ally.enabledFrameworks": ["custom"], "i18n-ally.enabledFrameworks": ["custom"],
"i18n-ally.sortKeys": true, "i18n-ally.sortKeys": true,
"editor.codeActionsOnSave": { "editor.codeActionsOnSave": {
"source.fixAll.eslint": true "source.fixAll.eslint": "explicit"
}, },
"editor.defaultFormatter": "esbenp.prettier-vscode", "editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true, "editor.formatOnSave": true,

View File

@@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/js", "name": "@typebot.io/js",
"version": "0.2.21", "version": "0.2.22",
"description": "Javascript library to display typebots on your website", "description": "Javascript library to display typebots on your website",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -51,7 +51,7 @@ export const Bot = (props: BotProps & { class?: string }) => {
typeof props.typebot === 'string' ? props.typebot : undefined typeof props.typebot === 'string' ? props.typebot : undefined
const isPreview = const isPreview =
typeof props.typebot !== 'string' || (props.isPreview ?? false) typeof props.typebot !== 'string' || (props.isPreview ?? false)
const { data, error } = await startChatQuery({ const { data, error, response } = await startChatQuery({
stripeRedirectStatus: urlParams.get('redirect_status') ?? undefined, stripeRedirectStatus: urlParams.get('redirect_status') ?? undefined,
typebot: props.typebot, typebot: props.typebot,
apiHost: props.apiHost, apiHost: props.apiHost,
@@ -81,6 +81,7 @@ export const Bot = (props: BotProps & { class?: string }) => {
if (!data) { if (!data) {
if (error) console.error(error) if (error) console.error(error)
console.error({ data, error, response })
return setError(new Error("Error! Couldn't initiate the chat.")) return setError(new Error("Error! Couldn't initiate the chat."))
} }

View File

@@ -41,7 +41,7 @@ export async function startChatQuery({
: undefined : undefined
if (paymentInProgressState) { if (paymentInProgressState) {
removePaymentInProgressFromStorage() removePaymentInProgressFromStorage()
const { data, error } = await sendRequest<InitialChatReply>({ const { data, error, response } = await sendRequest<InitialChatReply>({
method: 'POST', method: 'POST',
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v1/sessions/${ url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v1/sessions/${
paymentInProgressState.sessionId paymentInProgressState.sessionId
@@ -64,11 +64,12 @@ export async function startChatQuery({
} }
: undefined, : undefined,
error, error,
response,
} }
} }
const typebotId = typeof typebot === 'string' ? typebot : typebot.id const typebotId = typeof typebot === 'string' ? typebot : typebot.id
if (isPreview) { if (isPreview) {
const { data, error } = await sendRequest<InitialChatReply>({ const { data, error, response } = await sendRequest<InitialChatReply>({
method: 'POST', method: 'POST',
url: `${ url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost() isNotEmpty(apiHost) ? apiHost : guessApiHost()
@@ -82,10 +83,11 @@ export async function startChatQuery({
return { return {
data, data,
error, error,
response,
} }
} }
const { data, error } = await sendRequest<InitialChatReply>({ const { data, error, response } = await sendRequest<InitialChatReply>({
method: 'POST', method: 'POST',
url: `${ url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost() isNotEmpty(apiHost) ? apiHost : guessApiHost()
@@ -100,5 +102,6 @@ export async function startChatQuery({
return { return {
data, data,
error, error,
response,
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/nextjs", "name": "@typebot.io/nextjs",
"version": "0.2.21", "version": "0.2.22",
"description": "Convenient library to display typebots on your Next.js website", "description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@@ -1,6 +1,6 @@
{ {
"name": "@typebot.io/react", "name": "@typebot.io/react",
"version": "0.2.21", "version": "0.2.22",
"description": "Convenient library to display typebots on your React app", "description": "Convenient library to display typebots on your React app",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

View File

@@ -28,10 +28,11 @@ export const sendRequest = async <ResponseData>(
body?: Record<string, unknown> | FormData body?: Record<string, unknown> | FormData
} }
| string | string
): Promise<{ data?: ResponseData; error?: Error }> => { ): Promise<{ data?: ResponseData; error?: Error; response?: Response }> => {
let response
try { try {
const url = typeof params === 'string' ? params : params.url const url = typeof params === 'string' ? params : params.url
const response = await fetch(url, { response = await fetch(url, {
method: typeof params === 'string' ? 'GET' : params.method, method: typeof params === 'string' ? 'GET' : params.method,
mode: 'cors', mode: 'cors',
headers: headers:
@@ -47,10 +48,10 @@ export const sendRequest = async <ResponseData>(
}) })
const data = await response.json() const data = await response.json()
if (!response.ok) throw 'error' in data ? data.error : data if (!response.ok) throw 'error' in data ? data.error : data
return { data } return { data, response }
} catch (e) { } catch (e) {
console.error(e) console.error(e)
return { error: e as Error } return { error: e as Error, response }
} }
} }