fix: improve stream api error format and add status code

This commit is contained in:
drbh 2024-10-22 11:59:14 -04:00
parent 85701eac32
commit 825f39d12b
1 changed files with 11 additions and 12 deletions

View File

@ -849,10 +849,10 @@ async fn completions(
yield Ok(event); yield Ok(event);
} }
Err(err) => { Err(err) => {
let error_event: ErrorEvent = err.into(); let event = Event::default()
let event = Event::default().json_data(error_event).unwrap_or_else(|e| { .json_data(ErrorEvent::into_api_error(err, 422))
InferError::StreamSerializationError(e.to_string()).into() .unwrap_or_else(|e| InferError::StreamSerializationError(e.to_string()).into());
}); println!("{:?}", event);
yield Ok::<Event, Infallible>(event); yield Ok::<Event, Infallible>(event);
break break
} }
@ -1358,10 +1358,9 @@ async fn chat_completions(
} }
} }
Err(err) => { Err(err) => {
let error_event: ErrorEvent = err.into(); let event = Event::default()
let event = Event::default().json_data(error_event).unwrap_or_else(|e| { .json_data(ErrorEvent::into_api_error(err, 422))
InferError::StreamSerializationError(e.to_string()).into() .unwrap_or_else(|e| InferError::StreamSerializationError(e.to_string()).into());
});
yield Ok::<Event, Infallible>(event); yield Ok::<Event, Infallible>(event);
break; break;
} }
@ -2525,7 +2524,7 @@ impl From<InferError> for Event {
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
pub struct APIError { pub struct APIError {
message: String, message: String,
http_status_code: i32, http_status_code: usize,
} }
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
@ -2533,12 +2532,12 @@ pub struct ErrorEvent {
error: APIError, error: APIError,
} }
impl From<InferError> for ErrorEvent { impl ErrorEvent {
fn from(err: InferError) -> Self { fn into_api_error(err: InferError, http_status_code: usize) -> Self {
ErrorEvent { ErrorEvent {
error: APIError { error: APIError {
message: err.to_string(), message: err.to_string(),
http_status_code: 500, http_status_code,
}, },
} }
} }