From 8f0e1f605d93e042067540df3fb05015f95408e6 Mon Sep 17 00:00:00 2001 From: what Date: Wed, 30 Aug 2023 14:55:01 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E4=BC=98=E5=8C=96=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- errno.go | 14 +++--------- support/response.go | 54 +++++++++------------------------------------ 2 files changed, 13 insertions(+), 55 deletions(-) diff --git a/errno.go b/errno.go index 4e17f47..b2efd6c 100644 --- a/errno.go +++ b/errno.go @@ -1,10 +1,5 @@ package contracts -import ( - "fmt" - "runtime" -) - // 定义错误码 type Errno struct { Code int `json:"code"` @@ -23,7 +18,7 @@ type Err struct { } func (err *Err) Error() string { - return fmt.Sprintf("Err - code: %d, message: %s, error: %s", err.Code, err.Msg, err.Errord) + return err.Errord } // 错误码设计 @@ -95,12 +90,9 @@ var ( // 使用 错误码 和 error 创建新的 错误 func NewErr(errno *Errno, err error) *Err { - stackBuf := make([]byte, 1024) - n := runtime.Stack(stackBuf[:], false) - return &Err{ Code: errno.Code, - Msg: err.Error(), - Errord: string(stackBuf[:n]), + Msg: errno.Error(), + Errord: err.Error(), } } diff --git a/support/response.go b/support/response.go index 7ec63d0..c29d0e8 100644 --- a/support/response.go +++ b/support/response.go @@ -2,9 +2,9 @@ package support import ( "encoding/json" + "fmt" "net/http" "os" - "runtime" "strings" "git.fsdpf.net/go/contracts" @@ -125,51 +125,13 @@ func NewMsgResponse(msg string, code int) contracts.HttpResponse { return &MsgResponse{msg: msg, code: code} } -func NewErrResponse(p ...any) contracts.HttpResponse { - code := contracts.InternalServerError.Code - msg := contracts.InternalServerError.Error() - data := any(nil) - - for i := 0; i < len(p); i++ { - switch v := p[i].(type) { - case error: - msg = v.Error() - case string: - msg = v - case int: - code = v - case contracts.Errno: - msg = v.Error() - code = v.Code - case *contracts.Errno: - msg = v.Error() - code = v.Code - default: - data = v - } - } - - stackBuf := make([]byte, 1024) - n := runtime.Stack(stackBuf[:], false) - - b, _ := json.Marshal(struct { - Code int `json:"code"` - Msg string `json:"msg"` - Data any `json:"data,omitempty"` - Stack string `json:"stack"` - }{ - Code: code, - Msg: msg, - Data: data, - Stack: string(stackBuf[:n]), - }) - - return NewJsonResponse(b) +func NewErrResponse(err *contracts.Err) contracts.HttpResponse { + return NewJsonResponse([]byte(fmt.Sprintf(`{"code": %d, "msg": %q, "errord": %q}`, err.Code, err.Msg, err.Errord))) } func HttpResponse(data any) contracts.HttpResponse { - var err error = contracts.InternalServerError - + var err error + // fmt.Printf("%#v \n", data) switch v := data.(type) { case contracts.HttpResponse: return v @@ -177,6 +139,10 @@ func HttpResponse(data any) contracts.HttpResponse { return NewMsgResponse(v.Error(), v.Code) case *contracts.Errno: return NewMsgResponse(v.Error(), v.Code) + case contracts.Err: + return NewErrResponse(&v) + case *contracts.Err: + return NewErrResponse(v) case error: err = v case []byte: @@ -201,5 +167,5 @@ func HttpResponse(data any) contracts.HttpResponse { } } - return NewMsgResponse(err.Error(), contracts.InternalServerError.Code) + return NewErrResponse(contracts.NewErr(contracts.InternalServerError, err)) }