From bf35228b4987be6711992d93a6537e6ec0228c95 Mon Sep 17 00:00:00 2001 From: Zhaokun Date: Tue, 21 Oct 2025 10:36:53 +0800 Subject: [PATCH] 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. --- src/main/knowledge/reranker/BaseReranker.ts | 1 + .../knowledge/reranker/GeneralReranker.ts | 34 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/knowledge/reranker/BaseReranker.ts b/src/main/knowledge/reranker/BaseReranker.ts index 9483cb3d4e..1e321e2d86 100644 --- a/src/main/knowledge/reranker/BaseReranker.ts +++ b/src/main/knowledge/reranker/BaseReranker.ts @@ -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) diff --git a/src/main/knowledge/reranker/GeneralReranker.ts b/src/main/knowledge/reranker/GeneralReranker.ts index e4b3503606..e3ac5e8c21 100644 --- a/src/main/knowledge/reranker/GeneralReranker.ts +++ b/src/main/knowledge/reranker/GeneralReranker.ts @@ -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()