error.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package errors
  4. import (
  5. "google.golang.org/grpc/codes"
  6. "google.golang.org/grpc/status"
  7. )
  8. var (
  9. // 通用错误
  10. SystemError = status.Error(10000, "系统错误")
  11. ParamsError = status.Error(10001, "参数错误")
  12. NotFoundError = status.Error(10002, "无数据")
  13. DuplicateError = status.Error(10003, "数据重复")
  14. ServiceError = status.Error(10000, "内部服务错误")
  15. PassWordError = status.Error(10004, "密码错误")
  16. UserStatusError = status.Error(10005, "当前用户禁止登陆")
  17. NoTokenError = status.Error(10008, "没有token")
  18. TokenExpiredError = status.Error(10009, "token已过期")
  19. TokenFailedError = status.Error(10010, "token错误")
  20. FileTypeError = status.Error(10011, "文件类型错误")
  21. FileToLargestError = status.Error(10012, "文件太大")
  22. UploadTypeError = status.Error(10013, "上传类型的未知")
  23. FileOpenFailed = status.Error(20317, "文件打开失败")
  24. ExportPdfFailed = status.Error(20318, "PDF生成失败")
  25. ExportWordFailed = status.Error(20319, "word生成失败")
  26. )
  27. func ErrorTransForm(err error) error {
  28. if err == nil {
  29. return nil
  30. }
  31. errStatus := status.Convert(err)
  32. code := errStatus.Code()
  33. if code == codes.Unknown || code == codes.Unavailable || code == codes.Internal {
  34. return ServiceError
  35. }
  36. return err
  37. }