蛋蛋星球 后台端

error.go 1.4 KiB

2ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package e
  2. import (
  3. "fmt"
  4. "path"
  5. "runtime"
  6. )
  7. type E struct {
  8. Code int // 错误码
  9. msg string // 报错代码
  10. st string // 堆栈信息
  11. }
  12. func NewErrCode(code int) error {
  13. if msg, ok := MsgFlags[code]; ok {
  14. return E{code, msg, stack(3)}
  15. }
  16. return E{ERR_UNKNOWN, "unknown", stack(3)}
  17. }
  18. func NewErr(code int, msg string) error {
  19. return E{code, msg, stack(3)}
  20. }
  21. func NewErrf(code int, msg string, args ...interface{}) error {
  22. return E{code, fmt.Sprintf(msg, args), stack(3)}
  23. }
  24. func (e E) Error() string {
  25. return e.msg
  26. }
  27. func stack(skip int) string {
  28. stk := make([]uintptr, 32)
  29. str := ""
  30. l := runtime.Callers(skip, stk[:])
  31. for i := 0; i < l; i++ {
  32. f := runtime.FuncForPC(stk[i])
  33. name := f.Name()
  34. file, line := f.FileLine(stk[i])
  35. str += fmt.Sprintf("\n%-30s[%s:%d]", name, path.Base(file), line)
  36. }
  37. return str
  38. }
  39. // ErrorIsAccountBan is 检查这个账号是否被禁用的错误
  40. func ErrorIsAccountBan(e error) bool {
  41. err, ok := e.(E)
  42. if ok && err.Code == 403029 {
  43. return true
  44. }
  45. return false
  46. }
  47. // ErrorIsAccountNoActive is 检查这个账号是否被禁用的错误
  48. func ErrorIsAccountNoActive(e error) bool {
  49. err, ok := e.(E)
  50. if ok && err.Code == 403028 {
  51. return true
  52. }
  53. return false
  54. }
  55. // ErrorIsUserDel is 检查这个账号是否被删除
  56. func ErrorIsUserDel(e error) bool {
  57. err, ok := e.(E)
  58. if ok && err.Code == 403053 {
  59. return true
  60. }
  61. return false
  62. }