fix: capture detailed error response body for reranker API failures (#10839)

* fix: capture detailed error response body for reranker API failures

Previously, when reranker API returned 400 or other error status codes,
only the HTTP status and status text were captured, without reading the
actual error response body that contains detailed error information.

This commit fixes the issue by:
- Reading the error response body (as JSON or text) before throwing error
- Attaching the response details to the error object
- Including responseBody in formatErrorMessage output

This will help diagnose issues like "qwen3-reranker not available" by
showing the actual error message from the API provider.

* fix: enhance error handling in GeneralReranker for API failures

This update improves the error handling in the GeneralReranker class by ensuring that the response body is properly cloned and read when an API call fails. The detailed error information, including the status, status text, and body, is now attached to the error object. This change aids in diagnosing issues by providing more context in error messages.
This commit is contained in:
Zhaokun 2025-10-21 10:36:53 +08:00 committed by GitHub
parent 5df8a55f1e
commit bf35228b49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -80,6 +80,7 @@ export default abstract class BaseReranker {
message: error.message,
status: error.response?.status,
statusText: error.response?.statusText,
responseBody: error.response?.body, // Include the actual API error response
requestBody: requestBody
}
return JSON.stringify(errorDetails, null, 2)

View File

@ -2,6 +2,15 @@ import { KnowledgeBaseParams, KnowledgeSearchResult } from '@types'
import { net } from 'electron'
import BaseReranker from './BaseReranker'
interface RerankError extends Error {
response?: {
status: number
statusText: string
body?: unknown
}
}
export default class GeneralReranker extends BaseReranker {
constructor(base: KnowledgeBaseParams) {
super(base)
@ -17,7 +26,30 @@ export default class GeneralReranker extends BaseReranker {
})
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
// Read the response body to get detailed error information
// Clone the response to avoid consuming the body multiple times
const clonedResponse = response.clone()
let errorBody: unknown
try {
errorBody = await clonedResponse.json()
} catch {
// If response body is not JSON, try to read as text
try {
errorBody = await response.text()
} catch {
errorBody = null
}
}
const error = new Error(`HTTP ${response.status}: ${response.statusText}`) as RerankError
// Attach response details to the error object for formatErrorMessage
error.response = {
status: response.status,
statusText: response.statusText,
body: errorBody
}
throw error
}
const data = await response.json()