liuhaijun 3f5f28d785 add sheduling agent
Change-Id: I89f35fb3984044c57f10727432755012542f9fd8
2023-11-16 10:55:57 +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-agent/internal/pkg/errors"
r "git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/internal/pkg/response"
"git.inspur.com/sbg-jszt/cfn/cfn-schedule-agent/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.Error("异常: %s", 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())
}