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

View File

@@ -10,7 +10,8 @@ use serde_json::json;
use tracing::{debug, error, warn};
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}
@@ -23,27 +24,14 @@ pub async fn get_response_impl(ctx: &ResponsesContext, response_id: &str) -> Res
// Retrieve response from storage
match ctx.response_storage.get_response(&resp_id).await {
Ok(Some(stored_response)) => axum::Json(stored_response.raw_response).into_response(),
Ok(None) => (
StatusCode::NOT_FOUND,
axum::Json(json!({
"error": {
"message": format!("Response with id '{}' not found", response_id),
"type": "not_found_error",
"code": "response_not_found"
}
})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({
"error": {
"message": format!("Failed to retrieve response: {}", e),
"type": "internal_error"
}
})),
)
.into_response(),
Ok(None) => error::not_found(
"response_not_found",
format!("Response with id '{}' not found", response_id),
),
Err(e) => error::internal_error(
"retrieve_response_failed",
format!("Failed to retrieve response: {}", e),
),
}
}
@@ -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_id, current_status
);
(
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({
"error": {
"message": "Internal error: background task completed but failed to update status in storage",
"type": "internal_error",
"code": "status_update_failed"
}
})),
error::internal_error(
"status_update_failed",
"Internal error: background task completed but failed to update status in storage",
)
.into_response()
}
}
"completed" => (
StatusCode::BAD_REQUEST,
axum::Json(json!({
"error": {
"message": "Cannot cancel completed response",
"type": "invalid_request_error",
"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(),
"completed" => error::bad_request(
"response_already_completed",
"Cannot cancel completed response",
),
"failed" => {
error::bad_request("response_already_failed", "Cannot cancel failed response")
}
"cancelled" => (
StatusCode::OK,
axum::Json(json!({
@@ -158,39 +124,20 @@ pub async fn cancel_response_impl(ctx: &ResponsesContext, response_id: &str) ->
.into_response(),
_ => {
// Unknown status
(
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({
"error": {
"message": format!("Unknown response status: {}", current_status),
"type": "internal_error"
}
})),
error::internal_error(
"unknown_response_status",
format!("Unknown response status: {}", current_status),
)
.into_response()
}
}
}
Ok(None) => (
StatusCode::NOT_FOUND,
axum::Json(json!({
"error": {
"message": format!("Response with id '{}' not found", response_id),
"type": "not_found_error",
"code": "response_not_found"
}
})),
)
.into_response(),
Err(e) => (
StatusCode::INTERNAL_SERVER_ERROR,
axum::Json(json!({
"error": {
"message": format!("Failed to retrieve response: {}", e),
"type": "internal_error"
}
})),
)
.into_response(),
Ok(None) => error::not_found(
"response_not_found",
format!("Response with id '{}' not found", response_id),
),
Err(e) => error::internal_error(
"retrieve_response_failed",
format!("Failed to retrieve response: {}", e),
),
}
}

View File

@@ -2,11 +2,8 @@
use std::sync::Arc;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
use serde_json::{json, to_value};
use axum::response::Response;
use serde_json::to_value;
use tracing::{debug, error, warn};
use crate::{
@@ -67,24 +64,14 @@ pub fn validate_worker_availability(
let available_models = worker_registry.get_models();
if !available_models.contains(&model.to_string()) {
return Some(
(
StatusCode::SERVICE_UNAVAILABLE,
axum::Json(json!({
"error": {
"message": format!(
"No workers available for model '{}'. Available models: {}",
model,
available_models.join(", ")
),
"type": "service_unavailable",
"param": "model",
"code": "no_available_workers"
}
})),
)
.into_response(),
);
return Some(error::service_unavailable(
"no_available_workers",
format!(
"No workers available for model '{}'. Available models: {}",
model,
available_models.join(", ")
),
));
}
None

View File

@@ -36,7 +36,7 @@ use std::sync::Arc;
use axum::{
body::Body,
http::{self, StatusCode},
http,
response::{IntoResponse, Response},
};
use bytes::Bytes;
@@ -85,18 +85,10 @@ pub async fn route_responses(
// 1. Reject background mode (no longer supported)
let is_background = request.background.unwrap_or(false);
if is_background {
return (
StatusCode::BAD_REQUEST,
axum::Json(json!({
"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();
return error::bad_request(
"unsupported_parameter",
"Background mode is not supported. Please set 'background' to false or omit it.",
);
}
// 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) {
Ok(req) => Arc::new(req),
Err(e) => {
return (
StatusCode::BAD_REQUEST,
axum::Json(json!({
"error": {
"message": format!("Failed to convert request: {}", e),
"type": "invalid_request_error"
}
})),
)
.into_response();
return error::bad_request(
"convert_request_failed",
format!("Failed to convert request: {}", e),
);
}
};