Quellcode durchsuchen

增加错误处理

zhangyu131 vor 9 Monaten
Ursprung
Commit
49b6cffb03
10 geänderte Dateien mit 176 neuen und 0 gelöschten Zeilen
  1. BIN
      .DS_Store
  2. 1 0
      constantx/request.go
  3. 11 0
      xerr/code.go
  4. 25 0
      xerr/code_msg.go
  5. 24 0
      xerr/server_msg.go
  6. BIN
      zerox/.DS_Store
  7. 55 0
      zerox/resp.go
  8. BIN
      zerox/tpl/.DS_Store
  9. 31 0
      zerox/tpl/api/handler.tpl
  10. 29 0
      zerox/tpl/api/logic.tpl

BIN
.DS_Store


+ 1 - 0
constantx/request.go

@@ -0,0 +1 @@
+package constantx

+ 11 - 0
xerr/code.go

@@ -0,0 +1,11 @@
+package xerr
+
+// 失败错误码
+const (
+	BaseError    = 10000 //基础错误
+	InvalidParam = 10001 //参数错误
+	DbError      = 10002 //数据库异常
+	RedisError   = 10003 //redis异常
+	ApiError     = 10004 //第三方接口异常
+	StatusError  = 10005 //状态异常
+)

+ 25 - 0
xerr/code_msg.go

@@ -0,0 +1,25 @@
+package xerr
+
+import "fmt"
+
+// 基础错误
+type CodeMsg struct {
+	Code int
+	Msg  string
+}
+
+func (e *CodeMsg) GetErrCode() int {
+	return e.Code
+}
+
+func (e *CodeMsg) GetErrMsg() string {
+	return e.Msg
+}
+
+func (e *CodeMsg) Error() string {
+	return fmt.Sprintf("code: %d, msg: %s", e.Code, e.Msg)
+}
+
+func NewCodeMsg(code int, msg string) *CodeMsg {
+	return &CodeMsg{Code: code, Msg: msg}
+}

+ 24 - 0
xerr/server_msg.go

@@ -0,0 +1,24 @@
+package xerr
+
+import "fmt"
+
+type ServerMsg struct {
+	Code int
+	Msg  string
+}
+
+func (e *ServerMsg) GetErrCode() int {
+	return e.Code
+}
+
+func (e *ServerMsg) GetErrMsg() string {
+	return e.Msg
+}
+
+func (e *ServerMsg) Error() string {
+	return fmt.Sprintf("code: %d, msg: %s", e.Code, e.Msg)
+}
+
+func NewServerMsg(code int, msg string) *ServerMsg {
+	return &ServerMsg{Code: code, Msg: msg}
+}

BIN
zerox/.DS_Store


+ 55 - 0
zerox/resp.go

@@ -0,0 +1,55 @@
+package zerox
+
+import (
+	"context"
+	"git.gzquan.cn/pkg/go-common/xerr"
+	"github.com/zeromicro/go-zero/core/logx"
+	"github.com/zeromicro/go-zero/rest/httpx"
+	"net/http"
+)
+
+// BaseResp 基础返回结构
+type BaseResp struct {
+	Code int    `json:"code"`
+	Msg  string `json:"message"`
+	Data any    `json:"data"`
+}
+
+// SetOkHandler 成功处理方法
+func SetOkHandler() {
+	httpx.SetOkHandler(func(ctx context.Context, a any) any {
+		resp := &BaseResp{
+			Code: http.StatusOK,
+			Msg:  "success",
+			Data: a,
+		}
+		return resp
+	})
+}
+
+// SetErrorHandler 失败处理方法
+func SetErrorHandler() {
+	httpx.SetErrorHandler(func(err error) (int, any) {
+		logx.Errorf("error type is %T, value: %v", err, err)
+		switch e := err.(type) {
+		case *xerr.CodeMsg:
+			return http.StatusOK, &BaseResp{
+				Code: e.GetErrCode(),
+				Msg:  e.GetErrMsg(),
+				Data: nil,
+			}
+		case *xerr.ServerMsg:
+			return http.StatusInternalServerError, &BaseResp{
+				Code: e.GetErrCode(),
+				Msg:  e.GetErrMsg(),
+				Data: nil,
+			}
+		default:
+			return http.StatusServiceUnavailable, &BaseResp{
+				Code: xerr.BaseError,
+				Msg:  err.Error(),
+				Data: nil,
+			}
+		}
+	})
+}

BIN
zerox/tpl/.DS_Store


+ 31 - 0
zerox/tpl/api/handler.tpl

@@ -0,0 +1,31 @@
+package {{.PkgName}}
+
+import (
+	"net/http"
+
+	"commonapi/internal/xerr"
+
+	"github.com/zeromicro/go-zero/rest/httpx"
+	{{.ImportPackages}}
+)
+
+func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
+	return func(w http.ResponseWriter, r *http.Request) {
+		{{if .HasRequest}}var req types.{{.RequestType}}
+		if err := httpx.Parse(r, &req); err != nil {
+			httpx.ErrorCtx(r.Context(), w, &xerr.CodeMsg{
+				Code: xerr.InvalidParam,
+				Msg:  err.Error(),
+			})
+			return
+		}
+
+		{{end}}l := {{.LogicName}}.New{{.LogicType}}(r, svcCtx)
+		{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})
+		if err != nil {
+			httpx.ErrorCtx(r.Context(), w, err)
+		} else {
+			httpx.OkJsonCtx(r.Context(), w, resp)
+		}
+	}
+}

+ 29 - 0
zerox/tpl/api/logic.tpl

@@ -0,0 +1,29 @@
+package {{.pkgName}}
+
+import (
+	{{.imports}}
+	"net/http"
+)
+
+type {{.logic}} struct {
+	logx.Logger
+	ctx    context.Context
+	svcCtx *svc.ServiceContext
+	req *http.Request
+}
+
+func New{{.logic}}(r *http.Request, svcCtx *svc.ServiceContext) *{{.logic}} {
+	ctx := r.Context()
+	return &{{.logic}}{
+		Logger: logx.WithContext(ctx),
+		ctx:    ctx,
+		svcCtx: svcCtx,
+		req: r,
+	}
+}
+
+func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} {
+	// todo: add your logic here and delete this line
+
+	{{.returnString}}
+}