Files
core/infra/response.go
yanweidong 9f70704081 feat(dependencies): 更新 go.mod 和 go.sum 文件,添加新依赖并优化现有依赖版本
在 go.mod 中添加了多个新依赖,包括用于微服务架构、数据库支持、缓存和消息队列的库。同时,更新了 go.sum 文件以反映这些更改。README.md 文件也进行了相应的更新,增加了微服务架构的描述和功能模块的详细信息,确保文档与代码保持一致。
2025-09-27 00:41:27 +08:00

44 lines
930 B
Go

package infra
import (
"github.com/gin-gonic/gin"
"google.golang.org/grpc/status"
)
var Response Reply
type Reply struct {
Code int32 `json:"code"`
Message string `json:"message"`
Result any `json:"result"`
}
// Success writes a normalized success payload with code=0.
func (reply *Reply) Success(ctx *gin.Context, data any) {
reply.Code = 0
reply.Result = data
reply.Message = ""
if data == nil {
reply.Result = ""
}
ctx.JSON(200, reply)
}
// Error converts an error (including gRPC status) to a normalized payload.
// Error converts an error (including gRPC status) to a normalized payload.
func (reply *Reply) Error(ctx *gin.Context, err error) {
reply.Code = 500
reply.Result = ""
// Status code defaults to 500
e, ok := status.FromError(err)
if ok {
reply.Code = int32(e.Code())
reply.Message = e.Message()
} else {
reply.Message = err.Error()
}
// Send error
ctx.JSON(200, reply)
}