Skip to content

fix: preserve text/plain error body in raise_if_error() after stream is consumed#736

Open
stvoutsin wants to merge 2 commits intoastropy:mainfrom
stvoutsin:fix-empty-error
Open

fix: preserve text/plain error body in raise_if_error() after stream is consumed#736
stvoutsin wants to merge 2 commits intoastropy:mainfrom
stvoutsin:fix-empty-error

Conversation

@stvoutsin
Copy link
Contributor

Description

DALServiceError raised by raise_if_error was showing an empty message
Example:
DALServiceError: for https://.../sync

when the caller used the pattern:

data = sq.execute_stream().read()
sq.raise_if_error() 

The root cause seemed to be that execute_stream returns the raw response stream without reading the body.
If a caller consumed that stream before calling raise_if_error the response_text that DALServiceError.from_except reads was empty because presumably the underlying buffer was already exhausted.

Fix

The fix reads and caches the response body in the exception handler, before the raw stream is returned to the caller. This ensures response.text remains available when raise_if_error is called later regardless of whether the caller has already read the stream.

@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.63%. Comparing base (c813d4a) to head (71ee099).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #736   +/-   ##
=======================================
  Coverage   82.62%   82.63%           
=======================================
  Files          91       91           
  Lines       10287    10286    -1     
=======================================
  Hits         8500     8500           
+ Misses       1787     1786    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

# Cache the response body before returning the raw stream. If the
# caller consumes the stream before calling raise_if_error()
# this would lead to an empty error message
_ = response.content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hu. That's too magic for me. How would this local reference "cache" the response body? I've not researched the implementation of the content attribute in requests; it probably reads the stream as a side effect?

If that's what it is, there should at least be a comment to that effect, but frankly I'd like that to be a lot more explicit. Do we have a strong reason to not digest the respone content to a user-accessible message right here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I don't disagree with that take.
The initial approach of accessing response.content worked because requests caches the body as a side effect, but that's implicit and probably fragile.

I've updated the fix to build the DALServiceError eagerly in execute_stream while the response body is still available, rather than deferring to raise_if_error.
Now raise_if_error will just re-raise the constructed exception so there should be no timing dependency on stream consumption now.

Does this seem like a better approach?

@bsipocz bsipocz added this to the v1.8.2 milestone Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants