Commit 5b83219
fix: allow response error handler to set status code (#1963)
* fix: allow response error handler to set status code
change the order the visit response body function sets the status code
and content type so if an error occurs during json marshalling the
response error handler can set the status code
* fix: buffer JSON responses before writing headers
JSON responses in strict server Visit*Response functions are now encoded
to a bytes.Buffer before setting headers and calling WriteHeader. This
ensures that if JSON encoding fails, nothing has been written to the
ResponseWriter, allowing ResponseErrorHandlerFunc to set an appropriate
status code (e.g. 500 instead of 200).
Non-JSON content types (text, formdata, multipart, binary/io.Reader)
retain the original headers-first ordering since their write patterns
do not benefit from buffering.
Adds regression tests for all content type paths and an integration test
verifying that ResponseErrorHandlerFunc can set the status code when
JSON encoding fails.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Address Greptile feedback
Also buffer formdata responses before writing headers
Apply the same marshal-before-headers pattern to Formdata responses:
runtime.MarshalForm is called before setting Content-Type or calling
WriteHeader, so if marshalling fails the error handler can still set
an appropriate status code.
Adds a regression test for the formdata encoding error path.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: Marcin Romaszewicz <marcinr@gmail.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent a13ef41 commit 5b83219
File tree
22 files changed
+1482
-195
lines changed- internal/test
- issues
- issue-1093/api
- child
- parent
- issue-1208-1209
- issue-1212/pkg1
- issue-1378
- bionicle
- fooservice
- issue-1529/strict-echo
- issue-1676
- issue-1963
- issue-2113/gen/api
- issue-2190
- issue-removed-external-ref/gen/spec_base
- strict-server
- chi
- echo
- gin
- gorilla
- stdhttp
- pkg/codegen/templates/strict
22 files changed
+1482
-195
lines changedSome generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
0 commit comments