Commit 69953656 authored by kzkzzzz's avatar kzkzzzz

feat: 修改错误

parent 8a2cccea
......@@ -2,8 +2,8 @@ package errorx
import (
"fmt"
"github.com/go-playground/validator/v10"
"gokratos-base/common/validate"
"go-micro.dev/v4/errors"
"gomicro-base/common/validate"
)
const (
......@@ -22,36 +22,22 @@ func (c *ResponseError) Error() string {
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
if len(code) > 0 {
c = code[0]
}
e := &ResponseError{
Code: c,
Msg: message,
return &errors.Error{
Code: int32(c),
Detail: detail,
Status: "操作失败",
}
return e
}
func ParseError(err error) (errorCode int, msg string) {
switch v := err.(type) {
case *ResponseError:
errorCode = v.Code
msg = v.Msg
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()
func NewParamsError(err error) error {
return &errors.Error{
Code: int32(ParamsError),
Detail: validate.TransError(err),
Status: "参数不正确",
}
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 (
)
var (
vt *validator.Validate
tr ut.Translator
vt *validator.Validate
trans ut.Translator
)
func newTranslate(v *validator.Validate) ut.Translator {
......@@ -18,20 +18,36 @@ func newTranslate(v *validator.Validate) ut.Translator {
enT := en.New()
uni := ut.New(enT, zhT)
trans, _ := uni.GetTranslator("zh")
_ = zhTranslations.RegisterDefaultTranslations(v, trans)
return trans
tr, _ := uni.GetTranslator("zh")
_ = zhTranslations.RegisterDefaultTranslations(v, tr)
return tr
}
func init() {
vt = validator.New()
tr = newTranslate(vt)
trans = newTranslate(vt)
}
func GrtTrans() ut.Translator {
return tr
return trans
}
func Struct(data interface{}) error {
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