Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package prpc | |
| 6 | |
| 7 import ( | |
| 8 "net/http" | |
| 9 | |
| 10 "google.golang.org/grpc/codes" | |
| 11 ) | |
| 12 | |
| 13 // This file maps gRPC codes to HTTP statuses and vice-versa. | |
| 14 | |
| 15 // codeToStatus maps gRPC codes to HTTP statuses. | |
| 16 // This map may need to be corrected when | |
| 17 // https://github.com/grpc/grpc-common/issues/210 | |
| 18 // is closed. | |
| 19 var codeToStatus = map[codes.Code]int{ | |
| 20 codes.OK: http.StatusOK, | |
| 21 codes.Canceled: http.StatusNoContent, | |
|
iannucci
2016/01/27 02:55:50
NoContent? That doesn't sound right... that's 204
dnj (Google)
2016/01/27 03:24:26
Looks like this function isn't even used. Going to
| |
| 22 codes.Unknown: http.StatusInternalServerError, | |
| 23 codes.InvalidArgument: http.StatusBadRequest, | |
| 24 codes.DeadlineExceeded: http.StatusServiceUnavailable, | |
| 25 codes.NotFound: http.StatusNotFound, | |
| 26 codes.AlreadyExists: http.StatusConflict, | |
| 27 codes.PermissionDenied: http.StatusForbidden, | |
| 28 codes.Unauthenticated: http.StatusUnauthorized, | |
| 29 codes.ResourceExhausted: http.StatusServiceUnavailable, | |
| 30 codes.FailedPrecondition: http.StatusPreconditionFailed, | |
| 31 codes.Aborted: http.StatusInternalServerError, | |
| 32 codes.OutOfRange: http.StatusBadRequest, | |
| 33 codes.Unimplemented: http.StatusNotImplemented, | |
| 34 codes.Internal: http.StatusInternalServerError, | |
| 35 codes.Unavailable: http.StatusServiceUnavailable, | |
| 36 codes.DataLoss: http.StatusInternalServerError, | |
| 37 } | |
| 38 | |
| 39 // CodeStatus maps gRPC codes to HTTP statuses. | |
| 40 // | |
| 41 // The behavior of this function may change when | |
| 42 // https://github.com/grpc/grpc-common/issues/210 | |
| 43 // is closed. | |
| 44 func CodeStatus(code codes.Code) (status int, ok bool) { | |
| 45 status, ok = codeToStatus[code] | |
| 46 return | |
| 47 } | |
| 48 | |
| 49 // StatusCode maps HTTP statuses to gRPC codes. | |
| 50 // Falls back to codes.Unknown. | |
| 51 // | |
| 52 // The behavior of this function may change when | |
| 53 // https://github.com/grpc/grpc-common/issues/210 | |
| 54 // is closed. | |
| 55 func StatusCode(status int) codes.Code { | |
| 56 switch { | |
| 57 | |
| 58 case status >= 200 && status < 300: | |
| 59 return codes.OK | |
| 60 | |
| 61 case status == http.StatusUnauthorized: | |
| 62 return codes.Unauthenticated | |
| 63 case status == http.StatusForbidden: | |
| 64 return codes.PermissionDenied | |
| 65 case status == http.StatusNotFound: | |
| 66 return codes.NotFound | |
| 67 case status == http.StatusGone: | |
| 68 return codes.NotFound | |
| 69 case status == http.StatusPreconditionFailed: | |
| 70 return codes.FailedPrecondition | |
| 71 case status >= 400 && status < 500: | |
| 72 return codes.InvalidArgument | |
| 73 | |
| 74 case status == http.StatusNotImplemented: | |
| 75 return codes.Unimplemented | |
| 76 case status == http.StatusServiceUnavailable: | |
| 77 return codes.Unavailable | |
| 78 case status >= 500 && status < 600: | |
| 79 return codes.Internal | |
| 80 | |
| 81 default: | |
| 82 return codes.Unknown | |
| 83 } | |
| 84 } | |
| OLD | NEW |