Skip to content

Commit 3a42e93

Browse files
committed
Add read_error() and has_read_error() methods to enhance error handling in streaming API
1 parent 1fa9d88 commit 3a42e93

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

README-stream.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ auto result = httplib::stream::Get(cli, "/path", headers);
135135
| `status()` | `int` | HTTP status code |
136136
| `headers()` | `Headers&` | Response headers |
137137
| `body(chunk_size)` | `Generator<std::string_view>` | Generator yielding body chunks |
138+
| `read_error()` | `Error` | Get the last read error |
139+
| `has_read_error()` | `bool` | Check if a read error occurred |
138140

139141
### Generator Class
140142

httplib-stream.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ class Result {
279279
// Get read error (socket direct mode only)
280280
Error read_error() const { return handle_.get_read_error(); }
281281

282+
// Check if a read error occurred
283+
bool has_read_error() const { return handle_.has_read_error(); }
284+
282285
// Streaming body access - returns Generator for lazy iteration
283286
Generator<std::string_view> body(size_t chunk_size = 8192) {
284287
return detail::stream_body(std::move(handle_), chunk_size);

test/test-stream.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ TEST_F(StreamingServerTest, stream_Get_BodyIteration) {
288288
body.append(chunk);
289289
}
290290
EXPECT_EQ("Hello World!", body);
291+
292+
// No read error after successful read
293+
EXPECT_FALSE(result.has_read_error());
294+
EXPECT_EQ(httplib::Error::Success, result.read_error());
291295
}
292296

293297
TEST_F(StreamingServerTest, stream_Get_ConnectionError) {

0 commit comments

Comments
 (0)