Use appropriate preprocessor guards to allow building without exceptions

pull/13815/head
Vijay Pai 7 years ago
parent 9427eabf3b
commit 9809ce38e9
  1. 18
      BUILD
  2. 4
      bazel/grpc_build_system.bzl
  3. 10
      include/grpc++/impl/codegen/method_handler_impl.h
  4. 15
      include/grpc/impl/codegen/port_platform.h
  5. 4
      test/cpp/end2end/exception_test.cc

18
BUILD

@ -38,6 +38,16 @@ config_setting(
values = {"define": "grpc_no_ares=true"},
)
config_setting(
name = "grpc_allow_exceptions",
values = {"define": "GRPC_ALLOW_EXCEPTIONS=1"},
)
config_setting(
name = "grpc_disallow_exceptions",
values = {"define": "GRPC_ALLOW_EXCEPTIONS=0"},
)
config_setting(
name = "remote_execution",
values = {"define": "GRPC_PORT_ISOLATED_RUNTIME=1"},
@ -544,24 +554,24 @@ grpc_cc_library(
grpc_cc_library(
name = "debug_location",
public_hdrs = ["src/core/lib/support/debug_location.h"],
language = "c++",
public_hdrs = ["src/core/lib/support/debug_location.h"],
)
grpc_cc_library(
name = "ref_counted",
public_hdrs = ["src/core/lib/support/ref_counted.h"],
language = "c++",
public_hdrs = ["src/core/lib/support/ref_counted.h"],
deps = [
"grpc_trace",
"debug_location",
"grpc_trace",
],
)
grpc_cc_library(
name = "ref_counted_ptr",
public_hdrs = ["src/core/lib/support/ref_counted_ptr.h"],
language = "c++",
public_hdrs = ["src/core/lib/support/ref_counted_ptr.h"],
)
grpc_cc_library(

@ -60,6 +60,10 @@ def grpc_cc_library(name, srcs = [], public_hdrs = [], hdrs = [],
defines = select({"//:grpc_no_ares": ["GRPC_ARES=0"],
"//conditions:default": [],}) +
select({"//:remote_execution": ["GRPC_PORT_ISOLATED_RUNTIME=1"],
"//conditions:default": [],} +
select({"//:grpc_allow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=1"],
"//:grpc_disallow_exceptions":
["GRPC_ALLOW_EXCEPTIONS=0"],
"//conditions:default": [],}),
hdrs = _maybe_update_cc_library_hdrs(hdrs + public_hdrs),
deps = deps + _get_external_deps(external_deps),

@ -35,15 +35,19 @@ namespace internal {
// so this process doesn't require additional overhead in the common case.
// Additionally, we don't need to return if we caught an exception or not;
// the handling is the same in either case.
template <class F>
Status CatchingFunctionHandler(F&& callable) {
template <class Callable>
Status CatchingFunctionHandler(Callable&& handler) {
#if GRPC_ALLOW_EXCEPTIONS
try {
return callable();
return handler();
} catch (const std::exception& e) {
return Status(StatusCode::UNKNOWN, e.what());
} catch (...) {
return Status(StatusCode::UNKNOWN, "Exception in method handler");
}
#else
return handler();
#endif
}
/// A wrapper class of an application provided rpc method handler.

@ -477,6 +477,21 @@ typedef unsigned __int64 uint64_t;
#endif /* GPR_ATTRIBUTE_NO_TSAN (2) */
#endif /* GPR_ATTRIBUTE_NO_TSAN (1) */
/* GRPC_ALLOW_EXCEPTIONS should be 0 or 1 if exceptions are allowed or not */
#ifndef GRPC_ALLOW_EXCEPTIONS
/* If not already set, set to 1 on Windows (style guide standard) but to
* 0 on non-Windows platforms unless the compiler defines __EXCEPTIONS */
#ifdef GPR_WINDOWS
#define GRPC_ALLOW_EXCEPTIONS 1
#else
#ifdef __EXCEPTIONS
#define GRPC_ALLOW_EXCEPTIONS 1
#else
#define GRPC_ALLOW_EXCEPTIONS 0
#endif
#endif
#endif
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif

@ -24,6 +24,7 @@
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc/impl/codegen/port_platform.h>
#include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "test/core/util/test_config.h"
@ -35,6 +36,7 @@ namespace testing {
const char* kErrorMessage = "This service caused an exception";
#if GRPC_ALLOW_EXCEPTIONS
class ExceptingServiceImpl : public ::grpc::testing::EchoTestService::Service {
public:
Status Echo(ServerContext* server_context, const EchoRequest* request,
@ -106,6 +108,8 @@ TEST_F(ExceptionTest, RequestStream) {
EXPECT_EQ(s.error_message(), kErrorMessage);
}
#endif
} // namespace testing
} // namespace grpc

Loading…
Cancel
Save