Skip to content

Commit 2759a95

Browse files
committed
fix: prevent TransferEncodingError in Gemini/Claude SSE streams
Convert yield Err(...) to yield Ok(Bytes::from(...)) with properly formatted SSE error events. Yielding Err into Body::from_stream() causes axum/hyper to abort the HTTP response with a TransferEncoding error instead of delivering the error as a valid SSE data frame. Files changed: - src-tauri/src/proxy/handlers/gemini.rs (primary fix) - src-tauri/src/proxy/mappers/claude/mod.rs (defense-in-depth)
1 parent 4fd9112 commit 2759a95

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src-tauri/src/proxy/handlers/gemini.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,15 @@ pub async fn handle_generate(
343343
Some(Ok(b)) => b,
344344
Some(Err(e)) => {
345345
error!("[Gemini-SSE] Connection error: {}", e);
346-
yield Err(format!("Stream error: {}", e));
346+
// Yield error as SSE content to prevent hyper from
347+
// aborting chunked encoding without 0\r\n\r\n
348+
let error_json = serde_json::json!({
349+
"error": {
350+
"message": format!("Stream error: {}", e),
351+
"type": "stream_error"
352+
}
353+
});
354+
yield Ok::<Bytes, String>(Bytes::from(format!("data: {}\n\n", error_json)));
347355
break;
348356
}
349357
None => break,

src-tauri/src/proxy/mappers/claude/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ where
8282
}
8383
}
8484
Err(e) => {
85-
yield Err(format!("Stream error: {}", e));
85+
let error_json = serde_json::json!({
86+
"error": {
87+
"message": format!("Stream error: {}", e),
88+
"type": "stream_error"
89+
}
90+
});
91+
yield Ok(Bytes::from(format!("data: {}\n\n", error_json)));
8692
break;
8793
}
8894
}

0 commit comments

Comments
 (0)