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 // StatusCode maps HTTP statuses to gRPC codes. |
| 14 // Falls back to codes.Unknown. |
| 15 // |
| 16 // The behavior of this function may change when |
| 17 // https://github.com/grpc/grpc-common/issues/210 |
| 18 // is closed. |
| 19 func StatusCode(status int) codes.Code { |
| 20 switch { |
| 21 |
| 22 case status >= 200 && status < 300: |
| 23 return codes.OK |
| 24 |
| 25 case status == http.StatusUnauthorized: |
| 26 return codes.Unauthenticated |
| 27 case status == http.StatusForbidden: |
| 28 return codes.PermissionDenied |
| 29 case status == http.StatusNotFound: |
| 30 return codes.NotFound |
| 31 case status == http.StatusGone: |
| 32 return codes.NotFound |
| 33 case status == http.StatusPreconditionFailed: |
| 34 return codes.FailedPrecondition |
| 35 case status >= 400 && status < 500: |
| 36 return codes.InvalidArgument |
| 37 |
| 38 case status == http.StatusNotImplemented: |
| 39 return codes.Unimplemented |
| 40 case status == http.StatusServiceUnavailable: |
| 41 return codes.Unavailable |
| 42 case status >= 500 && status < 600: |
| 43 return codes.Internal |
| 44 |
| 45 default: |
| 46 return codes.Unknown |
| 47 } |
| 48 } |
OLD | NEW |