Commit 1a9f547d by zhengqiuyun86

初始化

parent fc0c8f31
...@@ -53,12 +53,10 @@ func getError(errs validator.ValidationErrors, r interface{}) string { ...@@ -53,12 +53,10 @@ func getError(errs validator.ValidationErrors, r interface{}) string {
} }
type DError struct { type DError struct {
Err string Err string
Code string
} }
func (e *DError) Error() string {
return e.Err
}
func ThrowsErrS(err string) { func ThrowsErrS(err string) {
if &err != nil && err != "" { if &err != nil && err != "" {
log.Error(err) log.Error(err)
...@@ -72,9 +70,8 @@ func AssertThrowableErr(throwable bool, err error) { ...@@ -72,9 +70,8 @@ func AssertThrowableErr(throwable bool, err error) {
panic(err) panic(err)
} }
} }
func AssertThrowable(throwable bool, err string) { func AssertThrowable(throwable bool, code, err string) {
if throwable { if throwable {
log.Error(err) panic(DError{Code: code, Err: err})
panic(DError{Err: err})
} }
} }
...@@ -4,9 +4,9 @@ import ( ...@@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"git.168cad.top/zhengqiuyun/rym-util/exception" "git.168cad.top/zhengqiuyun/rym-util/exception"
"git.168cad.top/zhengqiuyun/rym-util/getWay" "git.168cad.top/zhengqiuyun/rym-util/getWay"
"git.168cad.top/zhengqiuyun/rym-util/log"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"runtime" "runtime"
"strings"
) )
func ExceptionCatch(c *gin.Context) { func ExceptionCatch(c *gin.Context) {
...@@ -15,17 +15,19 @@ func ExceptionCatch(c *gin.Context) { ...@@ -15,17 +15,19 @@ func ExceptionCatch(c *gin.Context) {
switch err.(type) { switch err.(type) {
case exception.DError: case exception.DError:
e := (err).(exception.DError) e := (err).(exception.DError)
getWay.FailAndMsg(c, e.Error()) if c == nil {
log.Error(fmt.Sprintf("%s-%s", e.Code, e.Err))
} else {
getWay.FailAndMsg(c, e.Code, e.Err)
}
break break
case error: case error:
e := (err).(error) e := (err).(error)
if strings.Index(e.Error(), "duplicate key") > 0 { var buf [1024]byte
getWay.FailAndMsg(c, "信息重复") n := runtime.Stack(buf[:], true)
} else { fmt.Println(string(buf[:]), n)
var buf [1024]byte if c != nil {
n := runtime.Stack(buf[:], true) getWay.FailAndMsg(c, "SYSTEM.ERROR", e.Error())
fmt.Println(string(buf[:]), n)
getWay.FailAndMsg(c, e.Error())
} }
break break
} }
......
...@@ -15,8 +15,10 @@ import ( ...@@ -15,8 +15,10 @@ import (
) )
type ServerResponse struct { type ServerResponse struct {
Code State `json:"code"` //0为成功,其他都是失败 Code State `json:"code"` //0为成功,其他都是失败
Msg string `json:"msg,omitempty"` //非1000情况下的错误描述 Msg string `json:"msg,omitempty"` //非1000情况下的错误描述
SubCode string `json:"subCode"`
SubMsg string `json:"subMsg"`
} }
type ServerDataResponse struct { type ServerDataResponse struct {
...@@ -35,9 +37,9 @@ func FailGetWay(c *gin.Context, state State) { ...@@ -35,9 +37,9 @@ func FailGetWay(c *gin.Context, state State) {
logEnd(c, resp) logEnd(c, resp)
} }
func FailAndMsg(c *gin.Context, msg string) { func FailAndMsg(c *gin.Context, subCode, subMsg string) {
db.Rollback() db.Rollback()
resp := ServerResponse{Code: BusinessError, Msg: msg} resp := ServerResponse{Code: BusinessError, Msg: "业务错误", SubCode: subCode, SubMsg: subMsg}
c.JSON(http.StatusOK, resp) c.JSON(http.StatusOK, resp)
c.Abort() c.Abort()
logEnd(c, resp) logEnd(c, resp)
......
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