|
@@ -3,6 +3,7 @@ package codes
|
|
|
import (
|
|
import (
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
|
|
+ "strconv"
|
|
|
|
|
|
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
|
)
|
|
)
|
|
@@ -40,3 +41,27 @@ func SystemErr(msg string) error {
|
|
|
func Customize() error {
|
|
func Customize() error {
|
|
|
return Error(1000, "")
|
|
return Error(1000, "")
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func ParseErrorWithData(err error, data interface{}) (int, string) {
|
|
|
|
|
+ st := errors.FromError(err)
|
|
|
|
|
+ if st == nil {
|
|
|
|
|
+ return 0, err.Error()
|
|
|
|
|
+ }
|
|
|
|
|
+ message := st.Message
|
|
|
|
|
+ metadata := st.GetMetadata()
|
|
|
|
|
+ code := st.Code
|
|
|
|
|
+ if _, ok := metadata["code"]; ok {
|
|
|
|
|
+ _code, _ := strconv.Atoi(metadata["code"])
|
|
|
|
|
+ code = int32(_code)
|
|
|
|
|
+ }
|
|
|
|
|
+ if data != nil {
|
|
|
|
|
+ if st.Reason != "" {
|
|
|
|
|
+ json.Unmarshal([]byte(st.Reason), data)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return int(code), message
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func ParseError(err error) (int, string) {
|
|
|
|
|
+ return ParseErrorWithData(err, nil)
|
|
|
|
|
+}
|