github.com/godaddy-x/freego@v1.0.156/ex/exception.go (about) 1 package ex 2 3 import ( 4 "github.com/godaddy-x/freego/utils" 5 "github.com/godaddy-x/freego/zlog" 6 "strings" 7 ) 8 9 /** 10 * @author shadow 11 * @createby 2018.12.13 12 */ 13 14 const ( 15 sep = "∵∴" 16 BIZ = 100000 // 普通业务异常 17 GRPC = 300000 // GRPC请求失败 18 WS_SEND = 999993 // WS发送数据失败 19 JSON = 999994 // JSON转换异常 20 NUMBER = 999995 // 数值转换异常 21 DATA = 999996 // 数据服务异常 22 CACHE = 999997 // 缓存服务异常 23 SYSTEM = 999998 // 系统级异常 24 UNKNOWN = 999999 // 未知异常 25 26 MQ = 800000 // MQ服务异常 27 REDIS_LOCK_ACQUIRE = 800001 // redis锁获取失败 28 REDIS_LOCK_PENDING = 800002 // redis锁正在处理 29 REDIS_LOCK_TIMEOUT = 800003 // redis锁自旋超时 30 ) 31 32 const ( 33 JSON_ERR = "failed to respond to JSON data" 34 GOB_ERR = "failed to respond to GOB data" 35 DATA_ERR = "failed to loaded data service" 36 DATA_C_ERR = "failed to save data" 37 DATA_R_ERR = "failed to read data" 38 DATA_U_ERR = "failed to update data" 39 DATA_D_ERR = "failed to delete data" 40 CACHE_ERR = "failed to loaded cache service" 41 CACHE_C_ERR = "failed to save cache data" 42 CACHE_R_ERR = "failed to read cache data" 43 CACHE_U_ERR = "failed to update cache data" 44 CACHE_D_ERR = "failed to delete cache data" 45 46 MQ_ERR = "failed to loaded mq service" 47 MQ_SEND_ERR = "failed to send mq data" 48 MQ_REVD_ERR = "failed to receive mq data" 49 ) 50 51 type Throw struct { 52 Code int 53 Msg string 54 Url string 55 Err error 56 Arg []string 57 } 58 59 func (self Throw) Error() string { 60 if self.Code == 0 { 61 self.Code = BIZ 62 } 63 errMsg := utils.AddStr(self.Code, sep, self.Msg) 64 if len(self.Url) > 0 { 65 errMsg = utils.AddStr(errMsg, sep, self.Url) 66 } 67 if len(self.Arg) > 0 { 68 if len(self.Url) == 0 { 69 errMsg = utils.AddStr(errMsg, sep, self.Url) 70 } 71 errMsg = utils.AddStr(errMsg, sep, self.Arg) 72 } 73 return errMsg 74 } 75 76 func Catch(err error) Throw { 77 if throw, ok := err.(Throw); ok { 78 return throw 79 } 80 spl := strings.Split(err.Error(), sep) 81 if len(spl) == 1 { 82 return Throw{Code: UNKNOWN, Msg: spl[0]} 83 } else if len(spl) == 2 { 84 if c, err := utils.StrToInt(spl[0]); err != nil { 85 return Throw{Code: SYSTEM, Msg: err.Error()} 86 } else { 87 return Throw{Code: c, Msg: spl[1]} 88 } 89 } else if len(spl) == 3 { 90 if c, err := utils.StrToInt(spl[0]); err != nil { 91 return Throw{Code: SYSTEM, Msg: err.Error()} 92 } else { 93 return Throw{Code: c, Msg: spl[1], Url: spl[2]} 94 } 95 } else if len(spl) == 4 { 96 if c, err := utils.StrToInt(spl[0]); err != nil { 97 return Throw{Code: SYSTEM, Msg: err.Error()} 98 } else { 99 var args []string 100 if err := utils.JsonUnmarshal(utils.Str2Bytes(spl[3]), &args); err != nil { 101 zlog.Error("exception args unmarshal failed", 0, zlog.AddError(err)) 102 } 103 return Throw{Code: c, Msg: spl[1], Url: spl[2], Arg: args} 104 } 105 } 106 return Throw{Code: UNKNOWN, Msg: "failed to catch exception", Err: err} 107 } 108 109 func OutError(title string, err error) { 110 throw, ok := err.(Throw) 111 if ok { 112 if throw.Err == nil { 113 throw.Err = utils.Error(throw.Msg) 114 } 115 zlog.Error(title, 0, zlog.AddError(throw.Err)) 116 } else { 117 zlog.Error(title, 0, zlog.AddError(err)) 118 } 119 }