// Package middleware ...
//
// Description : middleware ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2026-01-04 11:28
package middleware

import (
	"net/http"

	"git.zhangdeman.cn/zhangdeman/exception"
	"git.zhangdeman.cn/zhangdeman/gin/define"
	"git.zhangdeman.cn/zhangdeman/gin/logger"
	"git.zhangdeman.cn/zhangdeman/gin/response"
	"git.zhangdeman.cn/zhangdeman/gin/util"
	pkgLogger "git.zhangdeman.cn/zhangdeman/logger"
	"github.com/gin-gonic/gin"
)

func CustomRecover() gin.HandlerFunc {
	// 自定义 panic 捕获, 固定规则, 直接内置, 不暴露配置项
	return gin.CustomRecovery(func(c *gin.Context, err any) {
		logger.Instance.Error("服务发生panic被捕获,请检查日志", pkgLogger.NewLogData(util.GinCtxToContext(c), logger.RecordType, logger.CodeServicePanic, map[string]any{
			"err": err,
		}).ToFieldList()...)
		response.SendWithException(c, exception.New(http.StatusInternalServerError, map[string]any{
			"info": "Server Error",
		}), &define.ResponseOption{
			Extension: map[string]any{
				"err": err,
			},
		})
		c.Abort()
	})
}
