Chromium Code Reviews| Index: common/prpc/codes.go |
| diff --git a/common/prpc/codes.go b/common/prpc/codes.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..98100e4800863a40fb9c42574b532c540e8d11a6 |
| --- /dev/null |
| +++ b/common/prpc/codes.go |
| @@ -0,0 +1,84 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package prpc |
| + |
| +import ( |
| + "net/http" |
| + |
| + "google.golang.org/grpc/codes" |
| +) |
| + |
| +// This file maps gRPC codes to HTTP statuses and vice-versa. |
| + |
| +// codeToStatus maps gRPC codes to HTTP statuses. |
| +// This map may need to be corrected when |
| +// https://github.com/grpc/grpc-common/issues/210 |
| +// is closed. |
| +var codeToStatus = map[codes.Code]int{ |
| + codes.OK: http.StatusOK, |
| + 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
|
| + codes.Unknown: http.StatusInternalServerError, |
| + codes.InvalidArgument: http.StatusBadRequest, |
| + codes.DeadlineExceeded: http.StatusServiceUnavailable, |
| + codes.NotFound: http.StatusNotFound, |
| + codes.AlreadyExists: http.StatusConflict, |
| + codes.PermissionDenied: http.StatusForbidden, |
| + codes.Unauthenticated: http.StatusUnauthorized, |
| + codes.ResourceExhausted: http.StatusServiceUnavailable, |
| + codes.FailedPrecondition: http.StatusPreconditionFailed, |
| + codes.Aborted: http.StatusInternalServerError, |
| + codes.OutOfRange: http.StatusBadRequest, |
| + codes.Unimplemented: http.StatusNotImplemented, |
| + codes.Internal: http.StatusInternalServerError, |
| + codes.Unavailable: http.StatusServiceUnavailable, |
| + codes.DataLoss: http.StatusInternalServerError, |
| +} |
| + |
| +// CodeStatus maps gRPC codes to HTTP statuses. |
| +// |
| +// The behavior of this function may change when |
| +// https://github.com/grpc/grpc-common/issues/210 |
| +// is closed. |
| +func CodeStatus(code codes.Code) (status int, ok bool) { |
| + status, ok = codeToStatus[code] |
| + return |
| +} |
| + |
| +// StatusCode maps HTTP statuses to gRPC codes. |
| +// Falls back to codes.Unknown. |
| +// |
| +// The behavior of this function may change when |
| +// https://github.com/grpc/grpc-common/issues/210 |
| +// is closed. |
| +func StatusCode(status int) codes.Code { |
| + switch { |
| + |
| + case status >= 200 && status < 300: |
| + return codes.OK |
| + |
| + case status == http.StatusUnauthorized: |
| + return codes.Unauthenticated |
| + case status == http.StatusForbidden: |
| + return codes.PermissionDenied |
| + case status == http.StatusNotFound: |
| + return codes.NotFound |
| + case status == http.StatusGone: |
| + return codes.NotFound |
| + case status == http.StatusPreconditionFailed: |
| + return codes.FailedPrecondition |
| + case status >= 400 && status < 500: |
| + return codes.InvalidArgument |
| + |
| + case status == http.StatusNotImplemented: |
| + return codes.Unimplemented |
| + case status == http.StatusServiceUnavailable: |
| + return codes.Unavailable |
| + case status >= 500 && status < 600: |
| + return codes.Internal |
| + |
| + default: |
| + return codes.Unknown |
| + } |
| +} |