config.go 953 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package apierr
  2. import (
  3. "net/http"
  4. "sort"
  5. "github.com/gin-gonic/gin"
  6. "kpt-tmr-group/pkg/grpcutil"
  7. "kpt-tmr-group/pkg/xerr"
  8. )
  9. var (
  10. ignoreCodes = []int{400, 401, 404, 429}
  11. codesSize = int(3)
  12. )
  13. // IgnoreStatusCodes ignore error with status codes
  14. func IgnoreStatusCodes(codes ...int) {
  15. sort.Ints(codes)
  16. ignoreCodes = codes
  17. codesSize = len(codes)
  18. }
  19. func shouldIgnoreCode(code int) bool {
  20. // SearchInts using binary search, more fast
  21. idx := sort.SearchInts(ignoreCodes, code)
  22. return idx < codesSize && ignoreCodes[idx] == code
  23. }
  24. // ClassifiedAbort classify base on error type
  25. func ClassifiedAbort(c *gin.Context, err error) {
  26. if err == nil {
  27. return
  28. }
  29. if e, isc := xerr.IsCustomError(err); isc {
  30. // 服务内部自定义错误
  31. AbortBadRequest(c, http.StatusBadRequest, e)
  32. } else if !grpcutil.FilterServerError(err) {
  33. // gRPC 部分错误
  34. AbortBadRequest(c, http.StatusBadRequest, err)
  35. } else {
  36. AbortError(c, err)
  37. }
  38. }