feat:ok
This commit is contained in:
38
infra/response.go
Normal file
38
infra/response.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
var Response *Reply
|
||||
|
||||
type Reply struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data any `json:"data"`
|
||||
}
|
||||
|
||||
func (reply *Reply) Success(ctx *gin.Context, data any) {
|
||||
reply.Code = 200
|
||||
if data == nil {
|
||||
reply.Data = ""
|
||||
}
|
||||
|
||||
ctx.JSON(200, reply)
|
||||
return
|
||||
}
|
||||
func (reply *Reply) Error(ctx *gin.Context, err error) {
|
||||
reply.Code = 500
|
||||
// Status code defaults to 500
|
||||
e, ok := status.FromError(err)
|
||||
if ok {
|
||||
reply.Code = int(e.Code())
|
||||
}
|
||||
|
||||
reply.Msg = e.Message()
|
||||
|
||||
// Send error
|
||||
ctx.JSON(200, reply)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user