Tiny unify grpc existing error responses into new format (#15030)

This commit is contained in:
fzyzcjy
2025-12-13 13:25:10 +08:00
committed by GitHub
parent 9d3b411c56
commit e79ca95961
3 changed files with 50 additions and 130 deletions
@@ -10,7 +10,8 @@ use serde_json::json;
use tracing::{debug, error, warn}; use tracing::{debug, error, warn};
use crate::{ use crate::{
data_connector::ResponseId, routers::grpc::regular::responses::context::ResponsesContext, data_connector::ResponseId,
routers::{error, grpc::regular::responses::context::ResponsesContext},
}; };
/// Implementation for GET /v1/responses/{response_id} /// Implementation for GET /v1/responses/{response_id}
@@ -23,27 +24,14 @@ pub async fn get_response_impl(ctx: &ResponsesContext, response_id: &str) -> Res
// Retrieve response from storage // Retrieve response from storage
match ctx.response_storage.get_response(&resp_id).await { match ctx.response_storage.get_response(&resp_id).await {
Ok(Some(stored_response)) => axum::Json(stored_response.raw_response).into_response(), Ok(Some(stored_response)) => axum::Json(stored_response.raw_response).into_response(),
Ok(None) => ( Ok(None) => error::not_found(
StatusCode::NOT_FOUND, "response_not_found",
axum::Json(json!({ format!("Response with id '{}' not found", response_id),
"error": { ),
"message": format!("Response with id '{}' not found", response_id), Err(e) => error::internal_error(
"type": "not_found_error", "retrieve_response_failed",
"code": "response_not_found" format!("Failed to retrieve response: {}", e),
} ),
})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({
"error": {
"message": format!("Failed to retrieve response: {}", e),
"type": "internal_error"
}
})),
)
.into_response(),
} }
} }
@@ -112,41 +100,19 @@ pub async fn cancel_response_impl(ctx: &ResponsesContext, response_id: &str) ->
"Response {} has status '{}' but task handle is missing. Task may have crashed or storage update failed.", "Response {} has status '{}' but task handle is missing. Task may have crashed or storage update failed.",
response_id, current_status response_id, current_status
); );
( error::internal_error(
StatusCode::INTERNAL_SERVER_ERROR, "status_update_failed",
axum::Json(json!({ "Internal error: background task completed but failed to update status in storage",
"error": {
"message": "Internal error: background task completed but failed to update status in storage",
"type": "internal_error",
"code": "status_update_failed"
}
})),
) )
.into_response()
} }
} }
"completed" => ( "completed" => error::bad_request(
StatusCode::BAD_REQUEST, "response_already_completed",
axum::Json(json!({ "Cannot cancel completed response",
"error": { ),
"message": "Cannot cancel completed response", "failed" => {
"type": "invalid_request_error", error::bad_request("response_already_failed", "Cannot cancel failed response")
"code": "response_already_completed"
} }
})),
)
.into_response(),
"failed" => (
StatusCode::BAD_REQUEST,
axum::Json(json!({
"error": {
"message": "Cannot cancel failed response",
"type": "invalid_request_error",
"code": "response_already_failed"
}
})),
)
.into_response(),
"cancelled" => ( "cancelled" => (
StatusCode::OK, StatusCode::OK,
axum::Json(json!({ axum::Json(json!({
@@ -158,39 +124,20 @@ pub async fn cancel_response_impl(ctx: &ResponsesContext, response_id: &str) ->
.into_response(), .into_response(),
_ => { _ => {
// Unknown status // Unknown status
( error::internal_error(
StatusCode::INTERNAL_SERVER_ERROR, "unknown_response_status",
axum::Json(json!({ format!("Unknown response status: {}", current_status),
"error": {
"message": format!("Unknown response status: {}", current_status),
"type": "internal_error"
}
})),
) )
.into_response()
} }
} }
} }
Ok(None) => ( Ok(None) => error::not_found(
StatusCode::NOT_FOUND, "response_not_found",
axum::Json(json!({ format!("Response with id '{}' not found", response_id),
"error": { ),
"message": format!("Response with id '{}' not found", response_id), Err(e) => error::internal_error(
"type": "not_found_error", "retrieve_response_failed",
"code": "response_not_found" format!("Failed to retrieve response: {}", e),
} ),
})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({
"error": {
"message": format!("Failed to retrieve response: {}", e),
"type": "internal_error"
}
})),
)
.into_response(),
} }
} }
@@ -2,11 +2,8 @@
use std::sync::Arc; use std::sync::Arc;
use axum::{ use axum::response::Response;
http::StatusCode, use serde_json::to_value;
response::{IntoResponse, Response},
};
use serde_json::{json, to_value};
use tracing::{debug, error, warn}; use tracing::{debug, error, warn};
use crate::{ use crate::{
@@ -67,24 +64,14 @@ pub fn validate_worker_availability(
let available_models = worker_registry.get_models(); let available_models = worker_registry.get_models();
if !available_models.contains(&model.to_string()) { if !available_models.contains(&model.to_string()) {
return Some( return Some(error::service_unavailable(
( "no_available_workers",
StatusCode::SERVICE_UNAVAILABLE, format!(
axum::Json(json!({
"error": {
"message": format!(
"No workers available for model '{}'. Available models: {}", "No workers available for model '{}'. Available models: {}",
model, model,
available_models.join(", ") available_models.join(", ")
), ),
"type": "service_unavailable", ));
"param": "model",
"code": "no_available_workers"
}
})),
)
.into_response(),
);
} }
None None
@@ -36,7 +36,7 @@ use std::sync::Arc;
use axum::{ use axum::{
body::Body, body::Body,
http::{self, StatusCode}, http,
response::{IntoResponse, Response}, response::{IntoResponse, Response},
}; };
use bytes::Bytes; use bytes::Bytes;
@@ -85,18 +85,10 @@ pub async fn route_responses(
// 1. Reject background mode (no longer supported) // 1. Reject background mode (no longer supported)
let is_background = request.background.unwrap_or(false); let is_background = request.background.unwrap_or(false);
if is_background { if is_background {
return ( return error::bad_request(
StatusCode::BAD_REQUEST, "unsupported_parameter",
axum::Json(json!({ "Background mode is not supported. Please set 'background' to false or omit it.",
"error": { );
"message": "Background mode is not supported. Please set 'background' to false or omit it.",
"type": "invalid_request_error",
"param": "background",
"code": "unsupported_parameter"
}
})),
)
.into_response();
} }
// 2. Route based on execution mode // 2. Route based on execution mode
@@ -219,16 +211,10 @@ async fn route_responses_streaming(
let chat_request = match conversions::responses_to_chat(&modified_request) { let chat_request = match conversions::responses_to_chat(&modified_request) {
Ok(req) => Arc::new(req), Ok(req) => Arc::new(req),
Err(e) => { Err(e) => {
return ( return error::bad_request(
StatusCode::BAD_REQUEST, "convert_request_failed",
axum::Json(json!({ format!("Failed to convert request: {}", e),
"error": { );
"message": format!("Failed to convert request: {}", e),
"type": "invalid_request_error"
}
})),
)
.into_response();
} }
}; };