An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: Improve error message handling and display
Yoshiki Miura3d8cb027e92a
diff --git a/components/fetch-preview.tsx b/components/fetch-preview.tsxindex 76fb8531..6ac46e08 100644--- a/components/fetch-preview.tsx+++ b/components/fetch-preview.tsx@@ -59,11 +59,11 @@ export function FetchPreview({if (status === 'error') {return (- <div className="p-3 bg-card border border-destructive rounded-lg">+ <div className="p-3 bg-card border border-border rounded-lg"><div className="flex items-center gap-2 w-full"><Globe className="w-4 h-4 text-muted-foreground shrink-0" /><span className="text-sm text-destructive block flex-1 truncate min-w-0">- Failed to retrieve: {error}+ {error}</span></div></div>diff --git a/components/fetch-section.tsx b/components/fetch-section.tsxindex 78a2d284..45bb7b06 100644--- a/components/fetch-section.tsx+++ b/components/fetch-section.tsx@@ -27,14 +27,14 @@ export function FetchSection({ tool }: FetchSectionProps) {let title: string | undefinedlet contentLength: number | undefined- // Check if output is available- if (!tool.output) {- // Still fetching- displayStatus = 'fetching'- } else if (tool.state === 'output-error') {+ // Check tool state first+ if (tool.state === 'output-error') {// Error statedisplayStatus = 'error'error = tool.errorText || 'Failed to retrieve content'+ } else if (!tool.output) {+ // Still fetching+ displayStatus = 'fetching'} else {// Success state - we have outputconst data = tool.output as SearchResultsTypediff --git a/components/search-section.tsx b/components/search-section.tsxindex 321ebaf5..8da8d620 100644--- a/components/search-section.tsx+++ b/components/search-section.tsx@@ -96,7 +96,7 @@ export function SearchSection({)}{isError ? (<Section>- <div className="p-3 bg-card border border-destructive rounded-lg">+ <div className="bg-card rounded-lg"><div className="flex items-center gap-2 w-full"><span className="text-sm text-destructive block flex-1 min-w-0">{errorMessage}diff --git a/lib/tools/fetch.ts b/lib/tools/fetch.tsindex 696c0192..e2e3b233 100644--- a/lib/tools/fetch.ts+++ b/lib/tools/fetch.ts@@ -83,9 +83,7 @@ async function fetchRegularData(url: string): Promise<SearchResultsType> {throw new Error('Request timeout after 10 seconds')}console.error('Regular fetch error:', error)- throw new Error(- `Failed to fetch URL: ${error instanceof Error ? error.message : 'Unknown error'}`- )+ throw error instanceof Error ? error : new Error('Unknown fetch error')}}@@ -118,9 +116,7 @@ async function fetchJinaReaderData(url: string): Promise<SearchResultsType> {}} catch (error) {console.error('Jina Reader API error:', error)- throw new Error(- `Jina Reader API failed: ${error instanceof Error ? error.message : 'Unknown error'}`- )+ throw error instanceof Error ? error : new Error('Jina Reader API failed')}}@@ -155,9 +151,9 @@ async function fetchTavilyExtractData(url: string): Promise<SearchResultsType> {}} catch (error) {console.error('Tavily Extract API error:', error)- throw new Error(- `Tavily Extract API failed: ${error instanceof Error ? error.message : 'Unknown error'}`- )+ throw error instanceof Error+ ? error+ : new Error('Tavily Extract API failed')}}diff --git a/lib/tools/search.ts b/lib/tools/search.tsindex 55468464..1bb6f94e 100644--- a/lib/tools/search.ts+++ b/lib/tools/search.ts@@ -119,9 +119,7 @@ export function createSearchTool(fullModel: string) {} catch (error) {console.error('Search API error:', error)// Re-throw the error to let AI SDK handle it properly- throw new Error(- `Search failed: ${error instanceof Error ? error.message : 'Unknown error'}`- )+ throw error instanceof Error ? error : new Error('Unknown search error')}// Add citation mapping and toolCallId to search results