Skip to content

An AI-powered search engine with a generative UI

fix(search): describe a failed provider request by its HTTP status (#961)

Yoshiki Miura09d26ca750d9
diff --git a/lib/tools/search/providers/__tests__/http-error.test.ts b/lib/tools/search/providers/__tests__/http-error.test.ts
new file mode 100644
index 00000000..6e2ca823
--- /dev/null
+++ b/lib/tools/search/providers/__tests__/http-error.test.ts
@@ -0,0 +1,121 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import {
+ getToolFailureMessage,
+ serializeToolFailure,
+ ToolFailureError
+} from '@/lib/errors/tool-error'
+
+import { SearXNGSearchProvider } from '../searxng'
+import { TavilySearchProvider } from '../tavily'
+
+describe('search provider HTTP errors', () => {
+ afterEach(() => {
+ vi.unstubAllEnvs()
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+ })
+
+ it('preserves HTTP status details for classification', async () => {
+ vi.stubEnv('TAVILY_API_KEY', 'test-key')
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () =>
+ Promise.resolve(
+ new Response(null, { status: 403, statusText: 'Forbidden' })
+ )
+ )
+ )
+ vi.spyOn(console, 'error').mockImplementation(() => undefined)
+
+ const error = await new TavilySearchProvider()
+ .search('private query', 5, 'basic', [], [])
+ .catch(error => error)
+
+ expect(error).toBeInstanceOf(Error)
+ expect(error.message).toContain('HTTP 403: Forbidden')
+ expect(error.status).toBe(403)
+
+ const toolError = new ToolFailureError('search', error)
+ expect(getToolFailureMessage(toolError)).toBe(
+ 'The search service refused the request.'
+ )
+ expect(JSON.parse(serializeToolFailure(toolError))).toMatchObject({
+ error: 'The search service refused the request.',
+ retryable: false
+ })
+ })
+
+ it('classifies rate limiting as retryable', async () => {
+ vi.stubEnv('TAVILY_API_KEY', 'test-key')
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () =>
+ Promise.resolve(
+ new Response(null, {
+ status: 429,
+ statusText: 'Too Many Requests'
+ })
+ )
+ )
+ )
+ vi.spyOn(console, 'error').mockImplementation(() => undefined)
+
+ const error = await new TavilySearchProvider()
+ .search('private query', 5, 'basic', [], [])
+ .catch(error => error)
+ const toolError = new ToolFailureError('search', error)
+
+ expect(getToolFailureMessage(toolError)).toBe(
+ 'The search service is rate limiting requests. Please try again shortly.'
+ )
+ expect(JSON.parse(serializeToolFailure(toolError))).toMatchObject({
+ error:
+ 'The search service is rate limiting requests. Please try again shortly.',
+ retryable: true
+ })
+ })
+
+ it('does not include the query or upstream body', async () => {
+ const query = 'private model-authored query'
+ const responseBody = 'private upstream response body'
+ vi.stubEnv('SEARXNG_API_URL', 'https://search.example.com')
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () =>
+ Promise.resolve(
+ new Response(responseBody, {
+ status: 403,
+ statusText: 'Forbidden'
+ })
+ )
+ )
+ )
+ vi.spyOn(console, 'error').mockImplementation(() => undefined)
+
+ const error = await new SearXNGSearchProvider()
+ .search(query, 5, 'basic', [], [])
+ .catch(error => error)
+
+ expect(error.message).not.toContain(query)
+ expect(error.message).not.toContain(responseBody)
+ expect(error.message).toContain('HTTP 403: Forbidden')
+ })
+
+ it('uses a fallback for an empty status text', async () => {
+ vi.stubEnv('TAVILY_API_KEY', 'test-key')
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async () =>
+ Promise.resolve(new Response(null, { status: 503, statusText: '' }))
+ )
+ )
+ vi.spyOn(console, 'error').mockImplementation(() => undefined)
+
+ const error = await new TavilySearchProvider()
+ .search('private query', 5, 'basic', [], [])
+ .catch(error => error)
+
+ expect(error.message).toBe('Tavily search failed: HTTP 503: Unknown Status')
+ })
+})
diff --git a/lib/tools/search/providers/base.ts b/lib/tools/search/providers/base.ts
index 7666f6f3..ad55ee55 100644
--- a/lib/tools/search/providers/base.ts
+++ b/lib/tools/search/providers/base.ts
@@ -48,4 +48,17 @@ export abstract class BaseSearchProvider implements SearchProvider {
)
}
}
+
+ protected createHttpError(
+ response: Response,
+ providerName: string
+ ): Error & { status: number } {
+ const statusText = response.statusText || 'Unknown Status'
+ return Object.assign(
+ new Error(
+ `${providerName} search failed: HTTP ${response.status}: ${statusText}`
+ ),
+ { status: response.status }
+ )
+ }
}
diff --git a/lib/tools/search/providers/brave.ts b/lib/tools/search/providers/brave.ts
index ae4c6e5f..883ecb83 100644
--- a/lib/tools/search/providers/brave.ts
+++ b/lib/tools/search/providers/brave.ts
@@ -4,7 +4,7 @@ import {
SerperSearchResultItem
} from '@/lib/types'
-import { SearchProvider } from './base'
+import { BaseSearchProvider } from './base'
interface BraveWebResult {
title?: string
@@ -43,10 +43,11 @@ interface BraveImageResult {
height?: number
}
-export class BraveSearchProvider implements SearchProvider {
+export class BraveSearchProvider extends BaseSearchProvider {
private apiKey: string | undefined
constructor() {
+ super()
this.apiKey = process.env.BRAVE_SEARCH_API_KEY
}
@@ -124,7 +125,7 @@ export class BraveSearchProvider implements SearchProvider {
if (!response.ok) {
console.error(`Brave web search failed: ${response.statusText}`)
- throw new Error('Search failed')
+ throw this.createHttpError(response, 'Brave web')
}
const data = await response.json()
@@ -161,7 +162,7 @@ export class BraveSearchProvider implements SearchProvider {
if (!response.ok) {
console.error(`Brave video search failed: ${response.statusText}`)
- throw new Error('Search failed')
+ throw this.createHttpError(response, 'Brave video')
}
const data = await response.json()
@@ -208,7 +209,7 @@ export class BraveSearchProvider implements SearchProvider {
if (!response.ok) {
console.error(`Brave image search failed: ${response.statusText}`)
- throw new Error('Search failed')
+ throw this.createHttpError(response, 'Brave image')
}
const data = await response.json()
diff --git a/lib/tools/search/providers/searxng.ts b/lib/tools/search/providers/searxng.ts
index 54ec426f..c7f2db45 100644
--- a/lib/tools/search/providers/searxng.ts
+++ b/lib/tools/search/providers/searxng.ts
@@ -52,7 +52,7 @@ export class SearXNGSearchProvider extends BaseSearchProvider {
if (!response.ok) {
const errorText = await response.text()
console.error(`SearXNG API error (${response.status}):`, errorText)
- throw new Error('Search failed')
+ throw this.createHttpError(response, 'SearXNG')
}
const data: SearXNGResponse = await response.json()
diff --git a/lib/tools/search/providers/tavily.ts b/lib/tools/search/providers/tavily.ts
index 3a2ab5bd..459bbac3 100644
--- a/lib/tools/search/providers/tavily.ts
+++ b/lib/tools/search/providers/tavily.ts
@@ -51,7 +51,7 @@ export class TavilySearchProvider extends BaseSearchProvider {
console.error(
`Tavily API error: ${response.status} ${response.statusText}`
)
- throw new Error('Search failed')
+ throw this.createHttpError(response, 'Tavily')
}
const data = await response.json()