wip: change error_details functions to templates

pull/25196/head
Greg Miller 4 years ago
parent 3f011c2171
commit 7d4d516511
  1. 1
      BUILD
  2. 12
      CMakeLists.txt
  3. 1
      Package.swift
  4. 2
      build_autogenerated.yaml
  5. 1
      grpc.gyp
  6. 48
      include/grpcpp/support/error_details.h
  7. 31
      src/cpp/util/error_details.cc
  8. 1
      test/cpp/util/BUILD

@ -462,7 +462,6 @@ grpc_cc_library(
standalone = True, standalone = True,
deps = [ deps = [
"grpc++", "grpc++",
"//src/proto/grpc/status:status_proto",
], ],
) )

@ -3039,12 +3039,7 @@ if(gRPC_INSTALL)
endif() endif()
if(gRPC_BUILD_CODEGEN)
add_library(grpc++_error_details 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 src/cpp/util/error_details.cc
) )
@ -3097,9 +3092,7 @@ foreach(_hdr
DESTINATION "${gRPC_INSTALL_INCLUDEDIR}/${_path}" DESTINATION "${gRPC_INSTALL_INCLUDEDIR}/${_path}"
) )
endforeach() endforeach()
endif()
if(gRPC_BUILD_CODEGEN)
if(gRPC_INSTALL) if(gRPC_INSTALL)
install(TARGETS grpc++_error_details EXPORT gRPCTargets install(TARGETS grpc++_error_details EXPORT gRPCTargets
@ -3109,7 +3102,6 @@ if(gRPC_INSTALL)
) )
endif() endif()
endif()
if(gRPC_BUILD_CODEGEN) if(gRPC_BUILD_CODEGEN)
add_library(grpc++_reflection add_library(grpc++_reflection
@ -10804,6 +10796,10 @@ endif()
if(gRPC_BUILD_TESTS) if(gRPC_BUILD_TESTS)
add_executable(error_details_test 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.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.grpc.pb.cc
${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.pb.h ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.pb.h

@ -106,7 +106,6 @@ let package = Package(
"src/cpp/server/load_reporter/", "src/cpp/server/load_reporter/",
"src/cpp/util/core_stats.cc", "src/cpp/util/core_stats.cc",
"src/cpp/util/core_stats.h", "src/cpp/util/core_stats.h",
"src/cpp/util/error_details.cc",
], ],
sources: [ sources: [
"src/cpp/", "src/cpp/",

@ -2395,7 +2395,6 @@ libs:
- include/grpcpp/support/error_details.h - include/grpcpp/support/error_details.h
headers: [] headers: []
src: src:
- src/proto/grpc/status/status.proto
- src/cpp/util/error_details.cc - src/cpp/util/error_details.cc
deps: deps:
- grpc++ - grpc++
@ -5896,6 +5895,7 @@ targets:
language: c++ language: c++
headers: [] headers: []
src: src:
- src/proto/grpc/status/status.proto
- src/proto/grpc/testing/echo_messages.proto - src/proto/grpc/testing/echo_messages.proto
- test/cpp/util/error_details_test.cc - test/cpp/util/error_details_test.cc
deps: deps:

@ -1471,7 +1471,6 @@
'upb', 'upb',
], ],
'sources': [ 'sources': [
'src/proto/grpc/status/status.proto',
'src/cpp/util/error_details.cc', 'src/cpp/util/error_details.cc',
], ],
}, },

@ -21,12 +21,6 @@
#include <grpcpp/support/status.h> #include <grpcpp/support/status.h>
namespace google {
namespace rpc {
class Status;
} // namespace rpc
} // namespace google
namespace grpc { namespace grpc {
/// Map a \a grpc::Status to a \a google::rpc::Status. /// Map a \a grpc::Status to a \a google::rpc::Status.
@ -34,14 +28,48 @@ namespace grpc {
/// On success, returns status with OK. /// On success, returns status with OK.
/// Returns status with \a INVALID_ARGUMENT, if failed to deserialize. /// Returns status with \a INVALID_ARGUMENT, if failed to deserialize.
/// Returns status with \a FAILED_PRECONDITION, if \a to is nullptr. /// 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 <typename T>
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. /// Map \a google::rpc::Status to a \a grpc::Status.
/// Returns OK on success. /// Returns OK on success.
/// Returns status with \a FAILED_PRECONDITION if \a to is nullptr. /// 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 <typename T>
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<grpc::StatusCode>(from.code());
}
*to = grpc::Status(code, from.message(), from.SerializeAsString());
return grpc::Status::OK;
}
} // namespace grpc } // namespace grpc

@ -17,34 +17,3 @@
*/ */
#include <grpcpp/support/error_details.h> #include <grpcpp/support/error_details.h>
#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<grpc::StatusCode>(from.code());
}
*to = grpc::Status(code, from.message(), from.SerializeAsString());
return grpc::Status::OK;
}
} // namespace grpc

@ -290,6 +290,7 @@ grpc_cc_test(
], ],
deps = [ deps = [
"//:grpc++_error_details", "//:grpc++_error_details",
"//src/proto/grpc/status:status_proto",
"//src/proto/grpc/testing:echo_messages_proto", "//src/proto/grpc/testing:echo_messages_proto",
"//test/core/util:grpc_test_util", "//test/core/util:grpc_test_util",
], ],

Loading…
Cancel
Save