Merge pull request #25196 from devjgm/error-details-template

refactor!: change error_details functions to templates
reviewable/pr25078/r11^2
Esun Kim 4 years ago committed by GitHub
commit a1ae48db20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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,
deps = [
"grpc++",
"//src/proto/grpc/status:status_proto",
],
)

@ -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
@ -10857,6 +10849,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

@ -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/",

@ -2393,7 +2393,6 @@ libs:
- include/grpcpp/support/error_details.h
headers: []
src:
- src/proto/grpc/status/status.proto
- src/cpp/util/error_details.cc
deps:
- grpc++
@ -5914,6 +5913,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:

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

@ -21,12 +21,6 @@
#include <grpcpp/support/status.h>
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 <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.
/// 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 <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

@ -17,34 +17,3 @@
*/
#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 = [
"//:grpc++_error_details",
"//src/proto/grpc/status:status_proto",
"//src/proto/grpc/testing:echo_messages_proto",
"//test/core/util:grpc_test_util",
],

Loading…
Cancel
Save