codes.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package http
  2. import (
  3. "google.golang.org/grpc/codes"
  4. "net/http"
  5. )
  6. func httpStatusFromGrpcCode(code codes.Code) int {
  7. switch code {
  8. case codes.OK:
  9. return http.StatusOK
  10. case codes.Canceled:
  11. return http.StatusRequestTimeout
  12. case codes.Unknown:
  13. return http.StatusInternalServerError
  14. case codes.InvalidArgument:
  15. return http.StatusBadRequest
  16. case codes.DeadlineExceeded:
  17. return http.StatusGatewayTimeout
  18. case codes.ResourceExhausted:
  19. return http.StatusTooManyRequests
  20. case codes.FailedPrecondition:
  21. return http.StatusBadRequest
  22. case codes.OutOfRange:
  23. return http.StatusBadRequest
  24. case codes.Unimplemented:
  25. return http.StatusNotImplemented
  26. case codes.Internal:
  27. return http.StatusInternalServerError
  28. case codes.Unavailable:
  29. return http.StatusServiceUnavailable
  30. case codes.DataLoss:
  31. return http.StatusInternalServerError
  32. case codes.NotFound:
  33. return http.StatusNotFound
  34. case codes.Unauthenticated:
  35. return http.StatusUnauthorized
  36. case codes.PermissionDenied:
  37. return http.StatusForbidden
  38. }
  39. return http.StatusInternalServerError
  40. }