henriqueatila/golang-gin-best-practices / skills/golang-gin-api
golang-gin-api
VerifiedBuild REST APIs with Go Gin. Use when creating Go web servers, adding Gin routes, writing handlers, or asking about middleware, binding, error handling, or project structure.
references/error-handling-recovery.md
View on GitHub# Error Handling — Panic Recovery Middleware
See also: `error-handling-apperror.md`, `error-handling-validation.md`
## Panic Recovery Middleware
Use custom recovery to return JSON instead of Gin's default plain-text 500.
```go
// pkg/middleware/recovery.go
package middleware
import (
"log/slog"
"net/http"
"github.com/gin-gonic/gin"
)
// Recovery returns a Gin middleware that recovers from panics and logs them.
// Register with r.Use(middleware.Recovery(logger)) instead of gin.Recovery().
func Recovery(logger *slog.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if rec := recover(); rec != nil {
logger.ErrorContext(c.Request.Context(), "panic recovered",
"panic", rec,
"path", c.FullPath(),
"method", c.Request.Method,
)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": "internal server error",
})
}
}()
c.Next()
}
}
```
Register in main.go:
```go
r := gin.New()
r.Use(middleware.Logger(logger))
r.Use(middleware.Recovery(logger))
```
**Why custom recovery over `gin.Recovery()`:** `gin.Recovery()` writes a plain-text response. Custom recovery returns JSON, which API clients can parse consistently. It also integrates with your structured logger (`log/slog`) and request ID.
**What panics to expect:**
- Nil pointer dereferences in handlers (most common)
- Index out of range errors
- Type assertion failures without the two-value form
**Best practice:** Fix panics at their source — don't rely on recovery as a general error handler. Recovery is a last-resort safety net, not a substitute for proper error handling with `AppError` and `handleServiceError`.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |