28 lines
567 B
Go
28 lines
567 B
Go
package middleware
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
"runtime/debug"
|
|
|
|
"github.com/company/ai-ops/pkg/errors"
|
|
"github.com/company/ai-ops/pkg/response"
|
|
)
|
|
|
|
// Recovery 中间件捕获 panic
|
|
func Recovery(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
slog.Error("panic_recovered",
|
|
"error", rec,
|
|
"stack", string(debug.Stack()),
|
|
"path", r.URL.Path,
|
|
)
|
|
response.Error(w, errors.ErrInternal)
|
|
}
|
|
}()
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|