Skip to content

An AI-powered search engine with a generative UI

Fix error handling in submit and writer functions

Yoshiki Miura91e36572c6cd
diff --git a/app/actions.tsx b/app/actions.tsx
index 091ea457..0e9bca88 100644
--- a/app/actions.tsx
+++ b/app/actions.tsx
@@ -142,6 +142,8 @@ async function submit(formData?: FormData, skip?: boolean) {
toolOutputs = toolResponses
errorOccurred = hasError
+ console.log('response: ' + fullResponse, 'error: ' + hasError)
+
if (toolOutputs.length > 0) {
toolOutputs.map(output => {
aiState.update({
@@ -175,7 +177,13 @@ async function submit(formData?: FormData, skip?: boolean) {
: msg
) as CoreMessage[]
const latestMessages = modifiedMessages.slice(maxMessages * -1)
- answer = await writer(uiStream, streamText, latestMessages)
+ const { response, hasError } = await writer(
+ uiStream,
+ streamText,
+ latestMessages
+ )
+ answer = response
+ errorOccurred = hasError
} else {
streamText.done()
}
diff --git a/lib/agents/researcher.tsx b/lib/agents/researcher.tsx
index b655510f..7ca6a921 100644
--- a/lib/agents/researcher.tsx
+++ b/lib/agents/researcher.tsx
@@ -41,8 +41,17 @@ export async function researcher(
uiStream,
fullResponse
})
+ }).catch(err => {
+ hasError = true
+ fullResponse = 'Error: ' + err.message
+ streamText.update(fullResponse)
})
+ // If the result is not available, return an error response
+ if (!result) {
+ return { result, fullResponse, hasError, toolResponses: [] }
+ }
+
// Remove the spinner
uiStream.update(null)
@@ -77,6 +86,7 @@ export async function researcher(
toolResponses.push(delta)
break
case 'error':
+ console.log('Error: ' + delta.error)
hasError = true
fullResponse += `\nError occurred while executing the tool`
break
diff --git a/lib/agents/writer.tsx b/lib/agents/writer.tsx
index db3968cd..9c99aef5 100644
--- a/lib/agents/writer.tsx
+++ b/lib/agents/writer.tsx
@@ -10,6 +10,7 @@ export async function writer(
messages: CoreMessage[]
) {
let fullResponse = ''
+ let hasError = false
const answerSection = (
<Section title="Answer">
<BotMessage content={streamText.value} />
@@ -42,9 +43,14 @@ export async function writer(
}
}
})
+ .catch(err => {
+ hasError = true
+ fullResponse = 'Error: ' + err.message
+ streamText.update(fullResponse)
+ })
.finally(() => {
streamText.done()
})
- return fullResponse
+ return { response: fullResponse, hasError }
}