@@ -269,3 +269,73 @@ async def test_raised_unhandled_exception(
269269
270270 # log OEC
271271 assert "OEC:" in caplog .text
272+
273+
274+ async def test_not_implemented_error_is_501 (client : TestClient ):
275+ """Test that NotImplementedError is correctly mapped to HTTP 501 NOT IMPLEMENTED."""
276+ response = await client .get (
277+ "/v1/raise_exception" , params = {"exc" : NotImplementedError .__name__ }
278+ )
279+ assert response .status == status .HTTP_501_NOT_IMPLEMENTED
280+
281+ # Check that the response is properly enveloped
282+ payload = await response .json ()
283+ assert is_enveloped (payload )
284+
285+ # Verify error details
286+ data , error = unwrap_envelope (payload )
287+ assert not data
288+ assert error
289+ assert error .get ("status" ) == status .HTTP_501_NOT_IMPLEMENTED
290+
291+
292+ async def test_timeout_error_is_504 (client : TestClient ):
293+ """Test that TimeoutError is correctly mapped to HTTP 504 GATEWAY TIMEOUT."""
294+ response = await client .get (
295+ "/v1/raise_exception" , params = {"exc" : asyncio .TimeoutError .__name__ }
296+ )
297+ assert response .status == status .HTTP_504_GATEWAY_TIMEOUT
298+
299+ # Check that the response is properly enveloped
300+ payload = await response .json ()
301+ assert is_enveloped (payload )
302+
303+ # Verify error details
304+ data , error = unwrap_envelope (payload )
305+ assert not data
306+ assert error
307+ assert error .get ("status" ) == status .HTTP_504_GATEWAY_TIMEOUT
308+
309+
310+ async def test_exception_in_non_api_route (client : TestClient ):
311+ """Test how exceptions are handled in routes not under the API path."""
312+ response = await client .get ("/free/raise_exception" )
313+
314+ # This should be a raw exception, not processed by our middleware
315+ assert response .status == status .HTTP_500_INTERNAL_SERVER_ERROR
316+
317+ # Should not be enveloped since it's outside the API path
318+ text = await response .text ()
319+ try :
320+ # If it happens to be JSON, check it's not enveloped
321+ payload = json .loads (text )
322+ assert not is_enveloped (payload )
323+ except json .JSONDecodeError :
324+ # If it's not JSON, that's expected too
325+ pass
326+
327+
328+ async def test_http_ok_with_text_is_enveloped (client : TestClient ):
329+ """Test that HTTPOk with text is properly enveloped."""
330+ response = await client .get ("/v1/raise_success_with_text" )
331+ assert response .status == status .HTTP_200_OK
332+
333+ # Should be enveloped
334+ payload = await response .json ()
335+ assert is_enveloped (payload )
336+
337+ # Check the content was preserved
338+ data , error = unwrap_envelope (payload )
339+ assert not error
340+ assert data
341+ assert data .get ("ok" ) is True
0 commit comments