Commit 69953656 authored by kzkzzzz's avatar kzkzzzz

feat: 修改错误

parent 8a2cccea
...@@ -2,8 +2,8 @@ package errorx ...@@ -2,8 +2,8 @@ package errorx
import ( import (
"fmt" "fmt"
"github.com/go-playground/validator/v10" "go-micro.dev/v4/errors"
"gokratos-base/common/validate" "gomicro-base/common/validate"
) )
const ( const (
...@@ -22,36 +22,22 @@ func (c *ResponseError) Error() string { ...@@ -22,36 +22,22 @@ func (c *ResponseError) Error() string {
return fmt.Sprintf("[%d]%s", c.Code, c.Msg) return fmt.Sprintf("[%d]%s", c.Code, c.Msg)
} }
func NewError(message string, code ...int) error { func NewMicroError(detail string, code ...int) error {
var c = DefaultError var c = DefaultError
if len(code) > 0 { if len(code) > 0 {
c = code[0] c = code[0]
} }
e := &ResponseError{ return &errors.Error{
Code: c, Code: int32(c),
Msg: message, Detail: detail,
Status: "操作失败",
} }
return e
} }
func ParseError(err error) (errorCode int, msg string) { func NewParamsError(err error) error {
switch v := err.(type) { return &errors.Error{
case *ResponseError: Code: int32(ParamsError),
errorCode = v.Code Detail: validate.TransError(err),
msg = v.Msg Status: "参数不正确",
case validator.ValidationErrors:
errorCode = ParamsError
if len(v) > 0 {
msg = v[0].Translate(validate.GrtTrans())
} else {
msg = v.Error()
}
case validator.FieldError:
errorCode = ParamsError
msg = v.Translate(validate.GrtTrans())
default:
errorCode = DefaultError
msg = err.Error()
} }
return
} }
package resutil
import (
"encoding/json"
"github.com/go-kratos/kratos/v2/log"
"net/http"
)
func HttpResponseEncoder(w http.ResponseWriter, r *http.Request, v interface{}) error {
if v == nil {
_, err := w.Write(nil)
return err
}
data, err := json.Marshal(MakeHttpSuccess(v))
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
return err
}
return nil
}
func HttpErrorEncoder(w http.ResponseWriter, r *http.Request, err error) {
data, err := json.Marshal(MakeHttpError(err))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Errorf("HttpErrorEncoder json编码失败: %s", err)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(data)
}
package resutil
import (
"gokratos-base/common/errorx"
)
type HttpResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
}
func MakeHttpSuccess(data interface{}) (r *HttpResponse) {
r = &HttpResponse{
Code: 1,
Msg: "ok",
Data: data,
}
return
}
func MakeHttpError(err error) (r *HttpResponse) {
r = &HttpResponse{}
r.Code, r.Msg = errorx.ParseError(err)
return
}
...@@ -9,8 +9,8 @@ import ( ...@@ -9,8 +9,8 @@ import (
) )
var ( var (
vt *validator.Validate vt *validator.Validate
tr ut.Translator trans ut.Translator
) )
func newTranslate(v *validator.Validate) ut.Translator { func newTranslate(v *validator.Validate) ut.Translator {
...@@ -18,20 +18,36 @@ func newTranslate(v *validator.Validate) ut.Translator { ...@@ -18,20 +18,36 @@ func newTranslate(v *validator.Validate) ut.Translator {
enT := en.New() enT := en.New()
uni := ut.New(enT, zhT) uni := ut.New(enT, zhT)
trans, _ := uni.GetTranslator("zh") tr, _ := uni.GetTranslator("zh")
_ = zhTranslations.RegisterDefaultTranslations(v, trans) _ = zhTranslations.RegisterDefaultTranslations(v, tr)
return trans return tr
} }
func init() { func init() {
vt = validator.New() vt = validator.New()
tr = newTranslate(vt) trans = newTranslate(vt)
} }
func GrtTrans() ut.Translator { func GrtTrans() ut.Translator {
return tr return trans
} }
func Struct(data interface{}) error { func Struct(data interface{}) error {
return vt.Struct(data) return vt.Struct(data)
} }
func TransError(err error) (msg string) {
switch v := err.(type) {
case validator.ValidationErrors:
if len(v) > 0 {
msg = v[0].Translate(trans)
} else {
msg = v.Error()
}
case validator.FieldError:
msg = v.Translate(trans)
default:
msg = err.Error()
}
return
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment