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