From 32373eb8ed3d18b57ed080d302d9fe90407c09ce Mon Sep 17 00:00:00 2001 From: Wei Zhang Date: Tue, 5 Nov 2024 14:17:53 +0800 Subject: [PATCH] fix(graphQL): use better err msg for openai api error (#3371) --- ee/tabby-schema/src/schema/mod.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ee/tabby-schema/src/schema/mod.rs b/ee/tabby-schema/src/schema/mod.rs index d3e00572e2a1..4bf839987e57 100644 --- a/ee/tabby-schema/src/schema/mod.rs +++ b/ee/tabby-schema/src/schema/mod.rs @@ -19,9 +19,12 @@ pub mod worker; use std::{sync::Arc, time::Instant}; use access_policy::{AccessPolicyService, SourceIdAccessPolicy}; -use async_openai::types::{ - ChatCompletionRequestMessage, ChatCompletionRequestUserMessageArgs, - CreateChatCompletionRequestArgs, +use async_openai::{ + error::OpenAIError, + types::{ + ChatCompletionRequestMessage, ChatCompletionRequestUserMessageArgs, + CreateChatCompletionRequestArgs, + }, }; use auth::{ AuthenticationService, Invitation, RefreshTokenResponse, RegisterResponse, TokenAuthResponse, @@ -165,6 +168,15 @@ pub enum TestModelConnectionError { Other(#[from] CoreError), } +impl From for TestModelConnectionError { + fn from(err: OpenAIError) -> Self { + match err { + OpenAIError::ApiError(e) => Self::FailedToConnect(e.message), + _ => Self::FailedToConnect(err.to_string()), + } + } +} + impl IntoFieldError for TestModelConnectionError { fn into_field_error(self) -> FieldError { match self { @@ -763,7 +775,7 @@ impl Query { Ok(_) => Ok(ModelBackendHealthInfo { latency_ms: start.elapsed().as_millis() as i32, }), - Err(e) => Err(TestModelConnectionError::FailedToConnect(e.to_string())), + Err(e) => Err(e.into()), } } else { Err(TestModelConnectionError::NotEnabled)