fix: return error on non-2xx response in stream_generate_content (#3)
Some checks failed
Rust / build (pull_request) Has been cancelled

Check response.status() before building the SSE event stream. On non-success
status, parse the response body as GenerateContentResponseError when
possible (returning Error::VertexError), and fall back to Error::HttpStatus
with the status code and raw text. Adds tests for HTTP 400 Vertex error,
non-JSON HTTP 500 error, and successful 200 SSE streaming.
This commit is contained in:
2026-10-18 22:47:23 +01:00
parent 196d5a9f55
commit ba9870277c
3 changed files with 166 additions and 26 deletions

View File

@@ -16,6 +16,11 @@ pub enum Error {
NoCandidatesError,
/// An error occurred while decoding the SSE event stream.
EventSourceError(LinesCodecError),
/// An HTTP error status was returned by the server.
HttpStatus {
status: reqwest::StatusCode,
body: String,
},
}
impl Display for Error {
@@ -28,6 +33,7 @@ impl Display for Error {
Error::VertexError(e) => write!(f, "Vertex error: {e}"),
Error::NoCandidatesError => write!(f, "No candidates returned for the prompt"),
Error::EventSourceError(e) => write!(f, "EventSource error: {e}"),
Error::HttpStatus { status, body } => write!(f, "HTTP status {status}: {body}"),
}
}
}