Skip to content

fix(api): re-panic aborts and guard a nil formatted response - #80

Merged
josuegiron merged 2 commits into
masterfrom
fix/retro-cleanup
Nov 21, 2026
Merged

josuegiron merged 2 commits into
masterfrom
fix/retro-cleanup

Conversation

@josuegiron

Copy link
Copy Markdown
Member

Scope

One PR (1 of 1), module api only. Small cleanup from the epic-api-v1-instance retrospective (findings 9, 11, 12; action items 4 and 5). Includes: re-panic of http.ErrAbortHandler in ProcessRequest, a nil formatted response guard in the built-in writer, doc-comment fixes (WithLogger, WithErrorHook, WithResponseWriter, ResponseWriter), removal of the dead test helper requestWithInstance, and tests for the nested-instance body limit and a type that embeds ResponseWriter. Leaves out: log redaction (#78), the constant-time test (#79). No further changes planned on top. 237+/55- lines.

Reason

  • ProcessRequest's recover answered Error500() to panic(http.ErrAbortHandler), which net/http defines as "abort the response" (finding 11).
  • The built-in ResponseWriter.Write dereferenced a nil *core.ResponseFormatted (a custom formatter returning nil) and panicked (finding 12).
  • WithLogger overstated "a request and a response record per request"; WithErrorHook had a broken wrap; requestWithInstance was unused (finding 12).
  • "Nested instances: the smallest limit applies" and "a type that embeds ResponseWriter logs through slog.Default with the registered formatter" were documented but not tested (finding 9).

Changes

  • middlewares.go: after the handler aborted debug record, an abort (plain or wrapped, via the shared isAbortPanic) is re-panicked as the plain http.ErrAbortHandler, with nothing written: nothing has reached the client because ProcessRequest buffers, so net/http drops the connection as it does without ProcessRequest. The plain sentinel is used because net/http logs any other value with its text and a stack. Other panics: unchanged (500 + panic recovered).
  • instance_log.go: ResponseWriter.Write on a nil response logs response cannot be written (error: nil formatted response) and answers like an encode failure (formatter's Error500() body, else bare 500).
  • Doc comments: 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.
  • Tests: new middlewares_abort_test.go (instance plain/wrapped, default instance: sentinel re-panicked, nothing written, only request + handler aborted); nil formatted response (instance → bare 500 + record; zero ResponseWriter → Error500() envelope through slog.Default); TestNestedInstancesApplyTheSmallestLimit (both nesting orders → 413; small body → 200); TestInstanceEmbeddedWriterLogsThroughSlogDefault (record in slog.Default, none in the instance logger, 500 body from the registered envelope formatter, not the instance's problem formatter). Removed requestWithInstance and the old abort test that pinned a 500.
  • No new exported name, option, package variable or dependency.

Release notes

  • Fix: ProcessRequest no longer answers 500 to panic(http.ErrAbortHandler): it logs the handler aborted debug record and re-panics http.ErrAbortHandler, so net/http aborts 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).
  • Fix: the built-in response writer no longer panics on a nil formatted response (a custom formatter returning nil): it answers 500 and logs response cannot be written.
  • Docs and tests only: WithLogger/WithErrorHook/WithResponseWriter doc fixes; nested-instance body limit and embedded ResponseWriter behavior pinned by tests.

Review

Quick review lens; applied: re-panic the plain sentinel for a wrapped abort (a wrapped value would reach Server.ErrorLog unredacted with a stack), WithLogger wording on response records, a WithResponseWriter wrap, 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 output
  • go vet ./... — ok
  • go test -race ./... — ok for every package
  • govulncheck ./... — No vulnerabilities found

🤖 Generated with Claude Code

@josuegiron
josuegiron merged commit f791a53 into master Nov 21, 2026
2 checks passed
@josuegiron
josuegiron deleted the fix/retro-cleanup branch November 21, 2026 19:57
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.

1 participant