ai update.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Package infra 提供基础设施功能
|
||||
// 包括统一响应处理、健康检查、日志等
|
||||
package infra
|
||||
|
||||
import (
|
||||
@@ -7,13 +9,16 @@ import (
|
||||
|
||||
var Response Reply
|
||||
|
||||
// Reply 统一响应结构体
|
||||
type Reply struct {
|
||||
Code int32 `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Result any `json:"result"`
|
||||
Code int32 `json:"code"` // 响应码
|
||||
Message string `json:"message"` // 响应消息
|
||||
Result any `json:"result"` // 响应数据
|
||||
}
|
||||
|
||||
// Success writes a normalized success payload with code=0.
|
||||
// Success 返回成功响应
|
||||
// ctx: Gin上下文
|
||||
// data: 响应数据
|
||||
func (reply *Reply) Success(ctx *gin.Context, data any) {
|
||||
reply.Code = 0
|
||||
reply.Result = data
|
||||
@@ -24,12 +29,13 @@ func (reply *Reply) Success(ctx *gin.Context, data any) {
|
||||
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.
|
||||
// Error 返回错误响应
|
||||
// ctx: Gin上下文
|
||||
// err: 错误对象
|
||||
func (reply *Reply) Error(ctx *gin.Context, err error) {
|
||||
reply.Code = 500
|
||||
reply.Result = ""
|
||||
// Status code defaults to 500
|
||||
// 默认状态码为500
|
||||
e, ok := status.FromError(err)
|
||||
if ok {
|
||||
reply.Code = int32(e.Code())
|
||||
@@ -38,6 +44,6 @@ func (reply *Reply) Error(ctx *gin.Context, err error) {
|
||||
reply.Message = err.Error()
|
||||
}
|
||||
|
||||
// Send error
|
||||
// 发送错误响应
|
||||
ctx.JSON(200, reply)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user