optimzie error process logic adapter mir

pull/196/head
Michael Li 2 years ago
parent 0eda77775f
commit 7b325e33b8

@ -3,9 +3,10 @@
package v1
import (
"errors"
"net/http"
gin "github.com/gin-gonic/gin"
"github.com/alimy/mir/v3"
"github.com/gin-gonic/gin"
)
type AgentInfo struct {
@ -37,25 +38,25 @@ type WebCore interface {
// Chain provide handlers chain for gin
Chain() gin.HandlersChain
Index(c *gin.Context) error
Articles(c *gin.Context) error
Login(c *gin.Context, req *LoginReq) (*LoginResp, error)
Logout(c *gin.Context) error
Index(c *gin.Context) mir.Error
Articles(c *gin.Context) mir.Error
Login(c *gin.Context, req *LoginReq) (*LoginResp, mir.Error)
Logout(c *gin.Context) mir.Error
mustEmbedUnimplementedWebCoreServant()
}
type WebCoreBinding interface {
BindLogin(c *gin.Context) (*LoginReq, error)
BindLogin(c *gin.Context) (*LoginReq, mir.Error)
mustEmbedUnimplementedWebCoreBinding()
}
type WebCoreRender interface {
RenderIndex(c *gin.Context, err error)
RenderArticles(c *gin.Context, err error)
RenderLogin(c *gin.Context, data *LoginResp, err error)
RenderLogout(c *gin.Context, err error)
RenderIndex(c *gin.Context, err mir.Error)
RenderArticles(c *gin.Context, err mir.Error)
RenderLogin(c *gin.Context, data *LoginResp, err mir.Error)
RenderLogout(c *gin.Context, err mir.Error)
mustEmbedUnimplementedWebCoreRender()
}
@ -92,37 +93,37 @@ type UnimplementedWebCoreServant struct{}
// UnimplementedWebCoreBinding can be embedded to have forward compatible implementations.
type UnimplementedWebCoreBinding struct {
BindAny func(*gin.Context, any) error
BindAny func(*gin.Context, any) mir.Error
}
// UnimplementedWebCoreRender can be embedded to have forward compatible implementations.
type UnimplementedWebCoreRender struct {
RenderAny func(*gin.Context, any, error)
RenderAny func(*gin.Context, any, mir.Error)
}
func (UnimplementedWebCoreServant) Chain() gin.HandlersChain {
return nil
}
func (UnimplementedWebCoreServant) Index(c *gin.Context) error {
return errors.New("method Index not implemented")
func (UnimplementedWebCoreServant) Index(c *gin.Context) mir.Error {
return mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented))
}
func (UnimplementedWebCoreServant) Articles(c *gin.Context) error {
return errors.New("method Index not implemented")
func (UnimplementedWebCoreServant) Articles(c *gin.Context) mir.Error {
return mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented))
}
func (UnimplementedWebCoreServant) Login(c *gin.Context, req *LoginReq) (*LoginResp, error) {
return nil, errors.New("method Login not implemented")
func (UnimplementedWebCoreServant) Login(c *gin.Context, req *LoginReq) (*LoginResp, mir.Error) {
return nil, mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented))
}
func (UnimplementedWebCoreServant) Logout(c *gin.Context) error {
return errors.New("method Logout not implemented")
func (UnimplementedWebCoreServant) Logout(c *gin.Context) mir.Error {
return mir.Errorln(http.StatusNotImplemented, http.StatusText(http.StatusNotImplemented))
}
func (UnimplementedWebCoreServant) mustEmbedUnimplementedWebCoreServant() {}
func (b *UnimplementedWebCoreBinding) BindLogin(c *gin.Context) (*LoginReq, error) {
func (b *UnimplementedWebCoreBinding) BindLogin(c *gin.Context) (*LoginReq, mir.Error) {
obj := new(LoginReq)
err := b.BindAny(c, obj)
return obj, err
@ -130,19 +131,19 @@ func (b *UnimplementedWebCoreBinding) BindLogin(c *gin.Context) (*LoginReq, erro
func (b *UnimplementedWebCoreBinding) mustEmbedUnimplementedWebCoreBinding() {}
func (r *UnimplementedWebCoreRender) RenderIndex(c *gin.Context, err error) {
func (r *UnimplementedWebCoreRender) RenderIndex(c *gin.Context, err mir.Error) {
r.RenderAny(c, nil, err)
}
func (r *UnimplementedWebCoreRender) RenderArticles(c *gin.Context, err error) {
func (r *UnimplementedWebCoreRender) RenderArticles(c *gin.Context, err mir.Error) {
r.RenderAny(c, nil, err)
}
func (r *UnimplementedWebCoreRender) RenderLogin(c *gin.Context, data *LoginResp, err error) {
func (r *UnimplementedWebCoreRender) RenderLogin(c *gin.Context, data *LoginResp, err mir.Error) {
r.RenderAny(c, data, err)
}
func (r *UnimplementedWebCoreRender) RenderLogout(c *gin.Context, err error) {
func (r *UnimplementedWebCoreRender) RenderLogout(c *gin.Context, err mir.Error) {
r.RenderAny(c, nil, err)
}

@ -1,10 +1,10 @@
package core
import (
"fmt"
"net/http"
"os"
"github.com/alimy/mir/v3"
"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/pkg/xerror"
@ -30,16 +30,16 @@ func (BaseServant) userFrom(c *gin.Context) (*model.User, bool) {
return nil, false
}
func BindAny(c *gin.Context, obj any) error {
func BindAny(c *gin.Context, obj any) mir.Error {
var errs xerror.ValidErrors
err := c.ShouldBind(obj)
if err != nil {
return fmt.Errorf("bind errs: %w", xerror.InvalidParams.WithDetails(errs.Error()))
return mir.NewError(xerror.InvalidParams.Code(), xerror.InvalidParams.WithDetails(errs.Error()))
}
return nil
}
func RenderAny(c *gin.Context, data any, err error) {
func RenderAny(c *gin.Context, data any, err mir.Error) {
if err == nil {
hostname, _ := os.Hostname()
if data == nil {
@ -59,7 +59,7 @@ func RenderAny(c *gin.Context, data any, err error) {
c.JSON(http.StatusOK, data)
} else {
// TODO: error process logic
resp := gin.H{"code": http.StatusInternalServerError, "msg": err.Error()}
resp := gin.H{"code": err.StatusCode(), "msg": err.Error()}
// xerr := &xerror.Error{}
// if errors.As(err, xerr) {
// resp["code"], resp["msg"] = xerr.Code(), xerr.Msg()

Loading…
Cancel
Save