errors.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }
  24. }
  25. func FailResponse(msg string, data interface{}) *Errors {
  26. return &Errors{
  27. Code: CodeInternal,
  28. Msg: msg,
  29. Data: data,
  30. }
  31. }
  32. func ParamsInvalidResponse(msg string, data interface{}) *Errors {
  33. return &Errors{
  34. Code: CodeParamsInvalid,
  35. Msg: msg,
  36. Data: data,
  37. }
  38. }
  39. func OperationInvalidResponse(msg string, data interface{}) *Errors {
  40. return &Errors{
  41. Code: CodeOperationInvalid,
  42. Msg: msg,
  43. Data: data,
  44. }
  45. }
  46. func NormalResponse(code int, msg string, data interface{}) *Errors {
  47. return &Errors{
  48. Code: code,
  49. Msg: msg,
  50. Data: data,
  51. }
  52. }