38 lines
676 B
Go
38 lines
676 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"`
|
|
}
|
|
|
|
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)
|
|
}
|
|
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()
|
|
|
|
// Send error
|
|
ctx.JSON(200, reply)
|
|
}
|