Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@adam-sikora
Copy link
Contributor

Summary

Avoid passing anyio.EndOfStream as exception context when reraising the exception in BaseHTTPMiddleware. Instead try to preserve the original context.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@adam-sikora adam-sikora marked this pull request as draft August 5, 2025 11:17
Avoid passing `anyio.EndOfStream` as exception context when reraising
the exception in middleware. Instead try to preserve the original
context.
@adam-sikora adam-sikora force-pushed the fix/middleware-error-context branch from 635b6a6 to ec88bf0 Compare August 5, 2025 11:19
@adam-sikora adam-sikora marked this pull request as ready for review August 5, 2025 11:22
@Kludex
Copy link
Owner

Kludex commented Aug 8, 2025

I think we'd want to merge #2830 instead. Can you try that PR?

# propagated as a cause with the reraise. This is necessary in order
# to prevent `anyio.EndOfStream` from polluting the exception
# context.
raise app_exc from app_exc.__cause__ or app_exc.__context__
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess what happens if the exception has no cause or context? Then does anyio.EndOfStream become the new cause / context?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If neither the context nor cause are present then this is equivalent to raise app_exc from None which suppresses the exception context and just shows the raised exception. This means that anyio.EndOfStream does not show up in the traceback.

Should I expand the comment to make this more clear?

This is a desired solution for me since when the anyio.EndOfStream was propagated in traceback as the innermost exception (context of app_exc) - it caused our Error Reporting (tool from Google cloud that aggregates encountered errors from logs) to group all our API errors into single group as the innermost exception was always the same.

@adam-sikora
Copy link
Contributor Author

I think we'd want to merge #2830 instead. Can you try that PR?

I can not test it directly at the moment but I believe that the mentioned PR does not change the exception behavior for the part that was changed in this PR. Furthermore the passing of the context there was is still just a suggestion in one comment.

@adam-sikora
Copy link
Contributor Author

I think we'd want to merge #2830 instead. Can you try that PR?

I can not test it directly at the moment but I believe that the mentioned PR does not change the exception behavior for the part that was changed in this PR. Furthermore the passing of the context there was is still just a suggestion in one comment.

I managed to test the #2830 PR even with the last suggestion there to add context/cause to the exception and I can confirm that it does not cover the issue solved in this PR

@adam-sikora
Copy link
Contributor Author

@Kludex @adriangb can we please move forward with this PR? As stated above the #2830 in its current state does not fix the issue that this PR addresses.

Copy link
Owner

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

Makes sense. Great PR.

Sorry the delay!

@Kludex Kludex enabled auto-merge (squash) October 28, 2025 08:02
@Kludex Kludex merged commit 26d66bb into Kludex:main Oct 28, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants