Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Unified Diff: common/prpc/codes.go

Issue 1605363002: common/prpc, tools/cmd/cproto: prpc client (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: rebased and addressed comments Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « common/prpc/client_test.go ('k') | common/prpc/doc.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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,
+ 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
+ }
+}
« no previous file with comments | « common/prpc/client_test.go ('k') | common/prpc/doc.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698