Skip to content

Commit

Permalink
wip: change error_details functions to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
devjgm committed Jan 20, 2021
1 parent 3f011c2 commit 7d4d516
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 53 deletions.
1 change: 0 additions & 1 deletion BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ grpc_cc_library(
standalone = True,
deps = [
"grpc++",
"//src/proto/grpc/status:status_proto",
],
)

Expand Down
12 changes: 4 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
Expand All @@ -3109,7 +3102,6 @@ if(gRPC_INSTALL)
)
endif()

endif()

if(gRPC_BUILD_CODEGEN)
add_library(grpc++_reflection
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
2 changes: 1 addition & 1 deletion build_autogenerated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion grpc.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,6 @@
'upb',
],
'sources': [
'src/proto/grpc/status/status.proto',
'src/cpp/util/error_details.cc',
],
},
Expand Down
48 changes: 38 additions & 10 deletions include/grpcpp/support/error_details.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,55 @@

#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.
/// The given \a to object will be cleared.
/// 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

Expand Down
31 changes: 0 additions & 31 deletions src/cpp/util/error_details.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/cpp/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down

0 comments on commit 7d4d516

Please sign in to comment.