errors.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package util
  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. Failed = "操作失败"
  18. Fail = "fail"
  19. )
  20. func SuccessResponse(msg string, data interface{}) *Errors {
  21. return &Errors{
  22. Code: CodeSucceed,
  23. Success: true,
  24. Msg: msg,
  25. Data: data,
  26. }
  27. }
  28. func FailResponse(msg string, data interface{}) *Errors {
  29. return &Errors{
  30. Code: CodeInternal,
  31. Success: false,
  32. Msg: msg,
  33. Data: data,
  34. }
  35. }
  36. func ParamsInvalidResponse(msg string, data interface{}) *Errors {
  37. return &Errors{
  38. Code: CodeParamsInvalid,
  39. Success: false,
  40. Msg: msg,
  41. Data: data,
  42. }
  43. }
  44. func OperationInvalidResponse(msg string, data interface{}) *Errors {
  45. return &Errors{
  46. Code: CodeOperationInvalid,
  47. Success: false,
  48. Msg: msg,
  49. Data: data,
  50. }
  51. }
  52. func NormalResponse(code int, msg string, data interface{}) *Errors {
  53. return &Errors{
  54. Code: code,
  55. Success: true,
  56. Msg: msg,
  57. Data: data,
  58. }
  59. }