Skip to content

Return 413 Content Too Large for oversized response bodies#5

Draft
Copilot wants to merge 7 commits intofeature/better-middlewarefrom
copilot/sub-pr-2
Draft

Return 413 Content Too Large for oversized response bodies#5
Copilot wants to merge 7 commits intofeature/better-middlewarefrom
copilot/sub-pr-2

Conversation

Copy link
Contributor

Copilot AI commented Dec 23, 2025

When response bodies exceed MaxBodyBytes, the middleware was either returning 500 (enforce mode) or committing truncated, potentially corrupt data (audit mode).

Changes

  • Return HTTP 413 consistently when bw.tooLarge is detected, regardless of validation mode
  • Remove mode-dependent branching that committed partial responses
  • Use http.StatusRequestEntityTooLarge with descriptive error payload
// Before: mode-dependent handling
if cfg.Mode == ResponseValidationEnforce {
    c.Status(http.StatusInternalServerError) // 500
    return
}
_ = bw.commit() // commits truncated data in audit mode

// After: consistent 413 response
c.Status(http.StatusRequestEntityTooLarge) // 413
c.Writer.Write([]byte(fmt.Sprintf(`{"error":"response body too large","detail":%q}`, msg)))

Logging preserved for observability.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 6 commits December 23, 2025 17:43
Co-authored-by: bwalsh <47808+bwalsh@users.noreply.github.com>
Co-authored-by: bwalsh <47808+bwalsh@users.noreply.github.com>
Co-authored-by: bwalsh <47808+bwalsh@users.noreply.github.com>
Co-authored-by: bwalsh <47808+bwalsh@users.noreply.github.com>
Co-authored-by: bwalsh <47808+bwalsh@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix 413 exception handling in middleware feature Return 413 Content Too Large for oversized response bodies Dec 23, 2025
Copilot AI requested a review from bwalsh December 23, 2025 17:50
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.

2 participants