liuhaijun e94826ce29 add server
Change-Id: I0760f17f6a01c0121b59fcbfafc666032dbc30af
2024-09-19 09:44:15 +00:00

55 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/errors"
r "git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/response"
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/pkg/log"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// Success 业务成功响应
func Success(c *gin.Context, data ...any) {
response := r.Resp()
if data != nil {
response.WithDataSuccess(c, data[0])
return
}
response.Success(c)
}
// FailCode 业务失败响应
func FailCode(c *gin.Context, code int, err error, data ...any) {
response := r.Resp()
if err != nil {
log.Errorf("异常: %v", err.Error())
}
if data != nil {
response.WithData(data[0]).FailCode(c, code)
return
}
response.FailCode(c, code)
}
// Fail 业务失败响应
func Fail(c *gin.Context, code int, message string, data ...any) {
response := r.Resp()
if data != nil {
response.WithData(data[0]).FailCode(c, code, message)
return
}
response.FailCode(c, code, message)
}
// Err 判断错误类型是自定义类型则自动返回错误中携带的code和message否则返回服务器错误
func Err(c *gin.Context, e error) {
businessError, err := errors.AsBusinessError(e)
if err != nil {
log.Logger.Warn("Unknown error:", zap.Any("Error reason:", err))
FailCode(c, errors.ServerError, err)
return
}
Fail(c, businessError.GetCode(), businessError.GetMessage())
}