The Recovery middleware wraps every Gin handler in a deferredDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/AndresGT/GoKit/llms.txt
Use this file to discover all available pages before exploring further.
recover() call. If any handler or downstream middleware panics, Recovery intercepts the error, logs it with structured fields including the request context and optional stack trace, and writes a clean JSON 500 Internal Server Error response — keeping your server alive and your error trail complete.
Import
Overview
When a panic occurs the middleware:- Normalises the panic value to a string (works with
error,string, or anyanytype). - Captures a stack trace via
runtime/debug.Stack()ifEnableStackTraceis enabled. - Reads
user_idandrolefrom the Gin context (populated byAuthmiddleware when available). - Logs the full event at
ErrorLevelwith all fields attached. - Calls your
RecoveryHandlerif one is configured, or writes a default JSON 500 response.
Presets
GoKit provides two one-line presets for the most common environments:DefaultRecovery
Development mode. Enables stack trace logging and includes the panic detail in the HTTP response body — useful for debugging locally.
ProductionRecovery
Production mode. Disables stack trace logging and hides error detail from the HTTP response — clients receive only
{"error": "internal server error"}.Manual Configuration
Usemiddleware.Recovery(RecoveryConfig{...}) when you need custom behaviour:
RecoveryConfig Fields
The logger instance used to record the panic event. Defaults to the GoKit global logger when
nil.A custom function called after the panic is logged. Use this to write your own response shape. When
nil, the middleware writes the default JSON response and calls c.AbortWithStatusJSON.When
true, captures the full goroutine stack trace via debug.Stack() and attaches it to the log entry as the stack field. Default in DefaultRecovery is true; in ProductionRecovery it is false.When
true, the HTTP response body includes a detail field with the panic message in addition to the generic error field. Only use this in development. Default in DefaultRecovery is true; in ProductionRecovery it is false.The HTTP status code sent on panic. Defaults to
500 (http.StatusInternalServerError).What Gets Logged
On every panic the middleware logs atErrorLevel with the message "Panic recovered" and the following structured fields:
| Field | Source | Always present |
|---|---|---|
error | Normalised panic value | ✅ |
method | c.Request.Method | ✅ |
path | c.Request.URL.Path | ✅ |
ip | c.ClientIP() | ✅ |
user_id | Auth middleware context | Only when authenticated |
role | Auth middleware context | Only when authenticated |
stack | debug.Stack() | Only when EnableStackTrace: true |
Default HTTP Response
When noRecoveryHandler is set, the response body shape depends on ExposeError:
Custom Recovery Handler Example
Return a structured error that includes the request ID, making it easy for clients to report the exact failing request:Middleware Ordering
Recovery should be registered after RequestID in your middleware chain. This ensures the request ID is already stored in the Gin context by the time a panic occurs, so your Because
RecoveryHandler can include it in the response.The recommended global middleware order is:Recovery is deferred inside each handler call, it effectively wraps all middleware that runs after it in the chain.