@@ -125,7 +125,7 @@ async def successful_func():
125125
126126@pytest .mark .asyncio
127127async def test_rest_stream_error_handler_generator_error (caplog ):
128- """Test rest_stream_error_handler logs error during async generation and ends stream gracefully ."""
128+ """Test rest_stream_error_handler catches error during async generation after first success ."""
129129 error = InternalError (message = 'Stream error during generation' )
130130
131131 async def failing_generator ():
@@ -141,18 +141,21 @@ async def successful_prep_failing_stream():
141141 # Assert it returns successfully
142142 assert isinstance (response , MockEventSourceResponse )
143143
144- # Consume the stream - error should be logged but not re-raised
144+ # Now consume the stream
145145 chunks = []
146- with caplog .at_level (logging .ERROR ):
146+ with (
147+ caplog .at_level (logging .ERROR ),
148+ pytest .raises (InternalError ) as exc_info ,
149+ ):
147150 async for chunk in response .body_iterator :
148151 chunks .append (chunk ) # noqa: PERF401
149152 assert chunks == ['success chunk 1' ]
150- assert 'Stream error during generation' in caplog . text
153+ assert exc_info . value == error
151154
152155
153156@pytest .mark .asyncio
154157async def test_rest_stream_error_handler_generator_unknown_error (caplog ):
155- """Test rest_stream_error_handler logs unknown error during async generation and ends stream gracefully ."""
158+ """Test rest_stream_error_handler catches unknown error during async generation."""
156159
157160 async def failing_generator ():
158161 yield 'success chunk 1'
@@ -164,9 +167,11 @@ async def successful_prep_failing_stream():
164167
165168 response = await successful_prep_failing_stream ()
166169
167- # Consume the stream - error should be logged but not re-raised
168170 chunks = []
169- with caplog .at_level (logging .ERROR ):
171+ with (
172+ caplog .at_level (logging .ERROR ),
173+ pytest .raises (RuntimeError , match = 'Unknown stream failure' ),
174+ ):
170175 async for chunk in response .body_iterator :
171176 chunks .append (chunk ) # noqa: PERF401
172177 assert chunks == ['success chunk 1' ]
0 commit comments