Skip to content

Conversation

@lbenguigui
Copy link

Description

This PR ensures that bufferWriter returns an error when the underlying writer fails.
It fixes cases where the buffer incorrectly returns a 200 status code even when maxResponseBodyBytes is exceeded (see the unit test update for details).

related to #247

@ldez ldez self-requested a review March 21, 2025 15:23
@ldez ldez added the bug label Mar 21, 2025
outReq := b.copyRequest(req, body, totalSize)

defer func() {
if err := recover(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

with this recover, you will catch all the panic from other middleware.

return
}

panic(err)
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about that.
Why do you not write the error inside the response?

Copy link
Author

Choose a reason for hiding this comment

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

The fact that the Write method of bufferWriter doesn't return an error prevents ErrAbortHandler from being raised.
The idea is to fix the Write method and specifically handles ErrAbortHandler by returning a 500 status code to avoid breaking changes.
For other errors, a panic is raised to ensure that existing panic behaviors remain unchanged.

After giving it some thought, I wonder if there might be impacts I didn't anticipate. Perhaps an approach like this (#247) might be safer.

@ldez
Copy link
Member

ldez commented Mar 21, 2025

🤔 the implementation inside #247 seems a better approach, can you provide more explanation?

@lbenguigui
Copy link
Author

I agree with you, #247 seems like a safer approach. I close this PR accordingly.

@lbenguigui lbenguigui closed this Mar 21, 2025
@ldez ldez added invalid and removed bug labels Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants