Repository navigation
fix(api): re-panic aborts and guard a nil formatted response - #80
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scope
One PR (1 of 1), module
apionly. Small cleanup from the epic-api-v1-instance retrospective (findings 9, 11, 12; action items 4 and 5). Includes: re-panic ofhttp.ErrAbortHandlerinProcessRequest, a nil formatted response guard in the built-in writer, doc-comment fixes (WithLogger,WithErrorHook,WithResponseWriter,ResponseWriter), removal of the dead test helperrequestWithInstance, and tests for the nested-instance body limit and a type that embedsResponseWriter. Leaves out: log redaction (#78), the constant-time test (#79). No further changes planned on top. 237+/55- lines.Reason
ProcessRequest's recover answeredError500()topanic(http.ErrAbortHandler), whichnet/httpdefines as "abort the response" (finding 11).ResponseWriter.Writedereferenced a nil*core.ResponseFormatted(a custom formatter returning nil) and panicked (finding 12).WithLoggeroverstated "a request and a response record per request";WithErrorHookhad a broken wrap;requestWithInstancewas unused (finding 12).ResponseWriterlogs throughslog.Defaultwith the registered formatter" were documented but not tested (finding 9).Changes
middlewares.go: after thehandler aborteddebug record, an abort (plain or wrapped, via the sharedisAbortPanic) is re-panicked as the plainhttp.ErrAbortHandler, with nothing written: nothing has reached the client becauseProcessRequestbuffers, sonet/httpdrops the connection as it does withoutProcessRequest. The plain sentinel is used becausenet/httplogs any other value with its text and a stack. Other panics: unchanged (500 +panic recovered).instance_log.go:ResponseWriter.Writeon a nil response logsresponse cannot be written(error:nil formatted response) and answers like an encode failure (formatter'sError500()body, else bare 500).WithLogger(record per completed response, none after a recovered panic; abort re-panicked; rewrapped),WithErrorHook(rewrapped),WithResponseWriter,ResponseWriter,logPanic.README.md(request flow, Logging),docs/adr/0001-v1-instance-api.md§6 amended note.middlewares_abort_test.go(instance plain/wrapped, default instance: sentinel re-panicked, nothing written, onlyrequest+handler aborted); nil formatted response (instance → bare 500 + record; zeroResponseWriter→Error500()envelope throughslog.Default);TestNestedInstancesApplyTheSmallestLimit(both nesting orders → 413; small body → 200);TestInstanceEmbeddedWriterLogsThroughSlogDefault(record inslog.Default, none in the instance logger, 500 body from the registered envelope formatter, not the instance's problem formatter). RemovedrequestWithInstanceand the old abort test that pinned a 500.Release notes
ProcessRequestno longer answers 500 topanic(http.ErrAbortHandler): it logs thehandler aborteddebug record and re-panicshttp.ErrAbortHandler, sonet/httpaborts the response as documented by the standard library (a wrapped abort is re-panicked as the plain sentinel, so the server does not log it).response cannot be written.WithLogger/WithErrorHook/WithResponseWriterdoc fixes; nested-instance body limit and embeddedResponseWriterbehavior pinned by tests.Review
Quick review lens; applied: re-panic the plain sentinel for a wrapped abort (a wrapped value would reach
Server.ErrorLogunredacted with a stack),WithLoggerwording on response records, aWithResponseWriterwrap, and the embedded-writer test now pins the registered formatter. Rejected: MAJOR-vs-PATCH under Principle V (owner directed PATCH, recorded as an owner question), a nil-response sentinel (no package variables), a near-duplicate test logger helper (cosmetic).Verification
Test-first: the abort tests and the nil-response test failed before the change (no re-panic; nil pointer dereference), pass after. The nested-limit and embedded-writer tests pin behavior that already held.
Local runs:
gofmt -l .— no outputgo vet ./...— okgo test -race ./...— ok for every packagegovulncheck ./...— No vulnerabilities found🤖 Generated with Claude Code