Skip to content

An AI-powered search engine with a generative UI

fix: Properly propagate tool errors for AI SDK v5

Yoshiki Miuraeeaf10dd6a2f
diff --git a/lib/tools/fetch.ts b/lib/tools/fetch.ts
index 4e25407c..78f91c7b 100644
--- a/lib/tools/fetch.ts
+++ b/lib/tools/fetch.ts
@@ -8,7 +8,7 @@ const TITLE_CHARACTER_LIMIT = 100
async function fetchRegularData(
url: string
-): Promise<SearchResultsType | null> {
+): Promise<SearchResultsType> {
try {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 10000) // 10 second timeout
@@ -82,17 +82,18 @@ async function fetchRegularData(
}
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
- console.error('Request timeout after 10 seconds')
- return null
+ throw new Error('Request timeout after 10 seconds')
}
console.error('Regular fetch error:', error)
- return null
+ throw new Error(
+ `Failed to fetch URL: ${error instanceof Error ? error.message : 'Unknown error'}`
+ )
}
}
async function fetchJinaReaderData(
url: string
-): Promise<SearchResultsType | null> {
+): Promise<SearchResultsType> {
try {
const response = await fetch(`https://r.jina.ai/${url}`, {
method: 'GET',
@@ -103,7 +104,7 @@ async function fetchJinaReaderData(
})
const json = await response.json()
if (!json.data || json.data.length === 0) {
- return null
+ throw new Error('No data returned from Jina Reader API')
}
const content = json.data.content.slice(0, CONTENT_CHARACTER_LIMIT)
@@ -121,13 +122,15 @@ async function fetchJinaReaderData(
}
} catch (error) {
console.error('Jina Reader API error:', error)
- return null
+ throw new Error(
+ `Jina Reader API failed: ${error instanceof Error ? error.message : 'Unknown error'}`
+ )
}
}
async function fetchTavilyExtractData(
url: string
-): Promise<SearchResultsType | null> {
+): Promise<SearchResultsType> {
try {
const apiKey = process.env.TAVILY_API_KEY
const response = await fetch('https://api.tavily.com/extract', {
@@ -139,7 +142,7 @@ async function fetchTavilyExtractData(
})
const json = await response.json()
if (!json.results || json.results.length === 0) {
- return null
+ throw new Error('No results returned from Tavily Extract API')
}
const result = json.results[0]
@@ -158,7 +161,9 @@ async function fetchTavilyExtractData(
}
} catch (error) {
console.error('Tavily Extract API error:', error)
- return null
+ throw new Error(
+ `Tavily Extract API failed: ${error instanceof Error ? error.message : 'Unknown error'}`
+ )
}
}
@@ -167,7 +172,7 @@ export const fetchTool = tool({
'Fetch content from any URL. By default uses "regular" type which performs fast, direct HTML fetching without external APIs - ideal for most websites. Only use "api" type when you need: 1) PDF content extraction, 2) Complex JavaScript-rendered pages, 3) Better markdown formatting, 4) Table extraction. The "api" type requires Jina or Tavily API keys.',
inputSchema: fetchSchema,
execute: async ({ url, type = 'regular' }) => {
- let results: SearchResultsType | null
+ let results: SearchResultsType
if (type === 'regular') {
// Use regular fetch for direct HTML retrieval
@@ -182,10 +187,6 @@ export const fetchTool = tool({
}
}
- if (!results) {
- return null
- }
-
return results
}
})
diff --git a/lib/tools/search.ts b/lib/tools/search.ts
index 7324d0b0..55468464 100644
--- a/lib/tools/search.ts
+++ b/lib/tools/search.ts
@@ -118,12 +118,10 @@ export function createSearchTool(fullModel: string) {
}
} catch (error) {
console.error('Search API error:', error)
- searchResult = {
- results: [],
- query: filledQuery,
- images: [],
- number_of_results: 0
- }
+ // Re-throw the error to let AI SDK handle it properly
+ throw new Error(
+ `Search failed: ${error instanceof Error ? error.message : 'Unknown error'}`
+ )
}
// Add citation mapping and toolCallId to search results