31 lines
785 B
Go
Raw Normal View History

2025-08-09 15:08:26 +07:00
package middleware
import (
"eslogad-be/internal/contract"
"eslogad-be/internal/logger"
"eslogad-be/internal/util"
"github.com/gin-gonic/gin"
"net/http"
"runtime/debug"
)
func Recover() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
logger.NonContext.Errorf(nil, "Recovered from panic %v", map[string]interface{}{
"stack_trace": string(debug.Stack()),
"error": err,
})
debug.PrintStack()
errorResponse := contract.BuildErrorResponse([]*contract.ResponseError{
contract.NewResponseError("900", "", string(debug.Stack())),
})
util.WriteResponse(c.Writer, c.Request, *errorResponse, http.StatusInternalServerError, "Middleware::Recover")
c.Abort()
}
}()
c.Next()
}
}