Skip to content

feat: add recovery middleware to handle panic gracefully#1537

Open
hhc7 wants to merge 1 commit into
apache:mainfrom
hhc7:feat/add-recovery-middleware
Open

feat: add recovery middleware to handle panic gracefully#1537
hhc7 wants to merge 1 commit into
apache:mainfrom
hhc7:feat/add-recovery-middleware

Conversation

@hhc7
Copy link
Copy Markdown

@hhc7 hhc7 commented May 28, 2026

Fixes #1536

Proposed Changes

  • Add middleware.Recovery() that catches panics from any subsequent middleware or handler, logs the panic message with full stack trace via log.Errorf + debug.Stack(), and returns a unified 500 JSON response (reason: base.unknown) via handler.NewRespBody + TrMsg — consistent with how other errors are handled in internal/base/handler/handler.go.
  • Mount Recovery() as the first middleware in internal/base/server/http.go so it covers all subsequent middleware (brotli, accept-language, short-id, auth, etc.) and handlers.
  • Add unit tests in recovery_test.go covering both the panic path (verifies 500 + base.unknown reason) and the no-panic path (verifies normal requests pass through unaffected).

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.

The Gin engine is initialized without a recovery middleware, causing connection drops on panic

1 participant