github.com/songzhibin97/gkit@v1.2.13/errors/internal/httputil.go (about) 1 package httputil 2 3 import ( 4 "net/http" 5 6 "google.golang.org/grpc/codes" 7 ) 8 9 const StatusClientClosed = 499 10 11 func GRPCCodeFromStatus(code int) codes.Code { 12 switch code { 13 case http.StatusOK: 14 return codes.OK 15 case http.StatusBadRequest: 16 return codes.InvalidArgument 17 case http.StatusUnauthorized: 18 return codes.Unauthenticated 19 case http.StatusForbidden: 20 return codes.PermissionDenied 21 case http.StatusNotFound: 22 return codes.NotFound 23 case http.StatusConflict: 24 return codes.Aborted 25 case http.StatusTooManyRequests: 26 return codes.ResourceExhausted 27 case http.StatusInternalServerError: 28 return codes.Internal 29 case http.StatusNotImplemented: 30 return codes.Unimplemented 31 case http.StatusServiceUnavailable: 32 return codes.Unavailable 33 case http.StatusGatewayTimeout: 34 return codes.DeadlineExceeded 35 case StatusClientClosed: 36 return codes.Canceled 37 } 38 return codes.Unknown 39 } 40 41 func StatusFromGRPCCode(code codes.Code) int { 42 switch code { 43 case codes.OK: 44 return http.StatusOK 45 case codes.Canceled: 46 return StatusClientClosed 47 case codes.Unknown: 48 return http.StatusInternalServerError 49 case codes.InvalidArgument: 50 return http.StatusBadRequest 51 case codes.DeadlineExceeded: 52 return http.StatusGatewayTimeout 53 case codes.NotFound: 54 return http.StatusNotFound 55 case codes.AlreadyExists: 56 return http.StatusConflict 57 case codes.PermissionDenied: 58 return http.StatusForbidden 59 case codes.Unauthenticated: 60 return http.StatusUnauthorized 61 case codes.ResourceExhausted: 62 return http.StatusTooManyRequests 63 case codes.FailedPrecondition: 64 return http.StatusBadRequest 65 case codes.Aborted: 66 return http.StatusConflict 67 case codes.OutOfRange: 68 return http.StatusBadRequest 69 case codes.Unimplemented: 70 return http.StatusNotImplemented 71 case codes.Internal: 72 return http.StatusInternalServerError 73 case codes.Unavailable: 74 return http.StatusServiceUnavailable 75 case codes.DataLoss: 76 return http.StatusInternalServerError 77 } 78 return http.StatusInternalServerError 79 }