From 7d4d516511855fd440f77188a81bcd2e01eef849 Mon Sep 17 00:00:00 2001 From: Greg Miller <9447643+devjgm@users.noreply.github.com> Date: Wed, 13 Jan 2021 13:49:37 -0500 Subject: [PATCH] wip: change error_details functions to templates --- BUILD | 1 - CMakeLists.txt | 12 +++---- Package.swift | 1 - build_autogenerated.yaml | 2 +- grpc.gyp | 1 - include/grpcpp/support/error_details.h | 48 ++++++++++++++++++++------ src/cpp/util/error_details.cc | 31 ----------------- test/cpp/util/BUILD | 1 + 8 files changed, 44 insertions(+), 53 deletions(-) diff --git a/BUILD b/BUILD index 9702615098f..9a235cf8fa7 100644 --- a/BUILD +++ b/BUILD @@ -462,7 +462,6 @@ grpc_cc_library( standalone = True, deps = [ "grpc++", - "//src/proto/grpc/status:status_proto", ], ) diff --git a/CMakeLists.txt b/CMakeLists.txt index 64a4a9076e5..35e60ac4730 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3039,12 +3039,7 @@ if(gRPC_INSTALL) endif() -if(gRPC_BUILD_CODEGEN) add_library(grpc++_error_details - ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/status/status.pb.cc - ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/status/status.grpc.pb.cc - ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/status/status.pb.h - ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/status/status.grpc.pb.h src/cpp/util/error_details.cc ) @@ -3097,9 +3092,7 @@ foreach(_hdr DESTINATION "${gRPC_INSTALL_INCLUDEDIR}/${_path}" ) endforeach() -endif() -if(gRPC_BUILD_CODEGEN) if(gRPC_INSTALL) install(TARGETS grpc++_error_details EXPORT gRPCTargets @@ -3109,7 +3102,6 @@ if(gRPC_INSTALL) ) endif() -endif() if(gRPC_BUILD_CODEGEN) add_library(grpc++_reflection @@ -10804,6 +10796,10 @@ endif() if(gRPC_BUILD_TESTS) add_executable(error_details_test + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/status/status.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/status/status.grpc.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/status/status.pb.h + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/status/status.grpc.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.grpc.pb.cc ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.pb.h diff --git a/Package.swift b/Package.swift index 610ecb39ff9..c97d43f61c4 100644 --- a/Package.swift +++ b/Package.swift @@ -106,7 +106,6 @@ let package = Package( "src/cpp/server/load_reporter/", "src/cpp/util/core_stats.cc", "src/cpp/util/core_stats.h", - "src/cpp/util/error_details.cc", ], sources: [ "src/cpp/", diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 45dd397425d..5fca555a21c 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -2395,7 +2395,6 @@ libs: - include/grpcpp/support/error_details.h headers: [] src: - - src/proto/grpc/status/status.proto - src/cpp/util/error_details.cc deps: - grpc++ @@ -5896,6 +5895,7 @@ targets: language: c++ headers: [] src: + - src/proto/grpc/status/status.proto - src/proto/grpc/testing/echo_messages.proto - test/cpp/util/error_details_test.cc deps: diff --git a/grpc.gyp b/grpc.gyp index d8a2616c233..4ad878bcb5a 100644 --- a/grpc.gyp +++ b/grpc.gyp @@ -1471,7 +1471,6 @@ 'upb', ], 'sources': [ - 'src/proto/grpc/status/status.proto', 'src/cpp/util/error_details.cc', ], }, diff --git a/include/grpcpp/support/error_details.h b/include/grpcpp/support/error_details.h index 15b917f6c5c..72305e8f3ab 100644 --- a/include/grpcpp/support/error_details.h +++ b/include/grpcpp/support/error_details.h @@ -21,12 +21,6 @@ #include -namespace google { -namespace rpc { -class Status; -} // namespace rpc -} // namespace google - namespace grpc { /// Map a \a grpc::Status to a \a google::rpc::Status. @@ -34,14 +28,48 @@ namespace grpc { /// On success, returns status with OK. /// Returns status with \a INVALID_ARGUMENT, if failed to deserialize. /// Returns status with \a FAILED_PRECONDITION, if \a to is nullptr. -grpc::Status ExtractErrorDetails(const grpc::Status& from, - ::google::rpc::Status* to); +/// +/// \note +/// This function is a template to avoid a build dep on \a status.proto. +/// However, this function still requires that \tparam T is of type +/// \a google::rpc::Status, which is defined at +/// https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto +template +grpc::Status ExtractErrorDetails(const grpc::Status& from, T* to) { + if (to == nullptr) { + return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, ""); + } + if (!to->ParseFromString(from.error_details())) { + return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, ""); + } + return grpc::Status::OK; +} +inline grpc::Status ExtractErrorDetails(const grpc::Status&, std::nullptr_t) { + return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, ""); +} /// Map \a google::rpc::Status to a \a grpc::Status. /// Returns OK on success. /// Returns status with \a FAILED_PRECONDITION if \a to is nullptr. -grpc::Status SetErrorDetails(const ::google::rpc::Status& from, - grpc::Status* to); +/// +/// \note +/// This function is a template to avoid a build dep on \a status.proto. +/// However, this function still requires that \tparam T is of type +/// \a google::rpc::Status, which is defined at +/// https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto +template +grpc::Status SetErrorDetails(const T& from, grpc::Status* to) { + if (to == nullptr) { + return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, ""); + } + grpc::StatusCode code = grpc::StatusCode::UNKNOWN; + if (from.code() >= grpc::StatusCode::OK && + from.code() <= grpc::StatusCode::UNAUTHENTICATED) { + code = static_cast(from.code()); + } + *to = grpc::Status(code, from.message(), from.SerializeAsString()); + return grpc::Status::OK; +} } // namespace grpc diff --git a/src/cpp/util/error_details.cc b/src/cpp/util/error_details.cc index f35a6125552..0330f012c27 100644 --- a/src/cpp/util/error_details.cc +++ b/src/cpp/util/error_details.cc @@ -17,34 +17,3 @@ */ #include - -#include "src/proto/grpc/status/status.pb.h" - -namespace grpc { - -grpc::Status ExtractErrorDetails(const grpc::Status& from, - ::google::rpc::Status* to) { - if (to == nullptr) { - return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, ""); - } - if (!to->ParseFromString(from.error_details())) { - return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, ""); - } - return grpc::Status::OK; -} - -grpc::Status SetErrorDetails(const ::google::rpc::Status& from, - grpc::Status* to) { - if (to == nullptr) { - return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, ""); - } - grpc::StatusCode code = grpc::StatusCode::UNKNOWN; - if (from.code() >= grpc::StatusCode::OK && - from.code() <= grpc::StatusCode::UNAUTHENTICATED) { - code = static_cast(from.code()); - } - *to = grpc::Status(code, from.message(), from.SerializeAsString()); - return grpc::Status::OK; -} - -} // namespace grpc diff --git a/test/cpp/util/BUILD b/test/cpp/util/BUILD index 6520a80e58b..9f927856248 100644 --- a/test/cpp/util/BUILD +++ b/test/cpp/util/BUILD @@ -290,6 +290,7 @@ grpc_cc_test( ], deps = [ "//:grpc++_error_details", + "//src/proto/grpc/status:status_proto", "//src/proto/grpc/testing:echo_messages_proto", "//test/core/util:grpc_test_util", ],