errors.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package common
  2. type Errors struct {
  3. Code int `json:"code"`
  4. Success bool `json:"success"`
  5. Msg string `json:"msg"`
  6. Data interface{} `json:"data"`
  7. }
  8. const (
  9. CodeSucceed = 200 //成功
  10. CodeInternal = 9999 //内部错误
  11. CodeParamsInvalid = 10001 //非法参数
  12. CodeOperationInvalid = 10002 //非法操作
  13. )
  14. const (
  15. Succeeded = "操作成功"
  16. Success = "success"
  17. )
  18. func SuccessResponse(msg string, data interface{}) *Errors {
  19. return &Errors{
  20. Code: CodeSucceed,
  21. Msg: msg,
  22. Data: data,
  23. Success: true,
  24. }
  25. }
  26. func FailResponse(msg string, data interface{}) *Errors {
  27. return &Errors{
  28. Code: CodeInternal,
  29. Msg: msg,
  30. Data: data,
  31. }
  32. }
  33. func ParamsInvalidResponse(msg string, data interface{}) *Errors {
  34. return &Errors{
  35. Code: CodeParamsInvalid,
  36. Msg: msg,
  37. Data: data,
  38. }
  39. }
  40. func StatusUnauthorized(msg string, data interface{}) *Errors {
  41. return &Errors{
  42. Code: 401,
  43. Msg: msg,
  44. Data: data,
  45. }
  46. }
  47. func StatusForbidden(msg string, data interface{}) *Errors {
  48. return &Errors{
  49. Code: 403,
  50. Msg: msg,
  51. Data: data,
  52. }
  53. }
  54. func OperationInvalidResponse(msg string, data interface{}) *Errors {
  55. return &Errors{
  56. Code: CodeOperationInvalid,
  57. Msg: msg,
  58. Data: data,
  59. }
  60. }
  61. func NormalResponse(code int, msg string, data interface{}) *Errors {
  62. return &Errors{
  63. Code: code,
  64. Msg: msg,
  65. Data: data,
  66. }
  67. }