diff --git a/include/rest_rpc/rpc_router.hpp b/include/rest_rpc/rpc_router.hpp index 424e362..8b68a1e 100644 --- a/include/rest_rpc/rpc_router.hpp +++ b/include/rest_rpc/rpc_router.hpp @@ -85,12 +85,10 @@ public: route_result.ec = rpc_errc::ok; } } catch (const std::exception &ex) { - rpc_service::msgpack_codec codec; route_result.result = std::string("exception occur when call").append(ex.what()); route_result.ec = rpc_errc::function_exception; } catch (...) { - rpc_service::msgpack_codec codec; route_result.result = std::string("unknown exception occur when call ") .append(get_name_by_key(key)); route_result.ec = rpc_errc::function_unknown_exception; @@ -120,24 +118,16 @@ private: using args_tuple = typename util::function_traits::parameters_type; using R = typename util::function_traits::return_type; - try { - if constexpr (std::tuple_size_v == 0) { - co_await handle_zero_arg(f, ret, self); + if constexpr (std::tuple_size_v == 0) { + co_await handle_zero_arg(f, ret, self); + } else { + using first_t = std::tuple_element_t<0, args_tuple>; + if constexpr (std::tuple_size_v == 1 && + util::is_basic_v) { + co_await handle_one_arg(str, f, ret, self); } else { - using first_t = std::tuple_element_t<0, args_tuple>; - if constexpr (std::tuple_size_v == 1 && - util::is_basic_v) { - co_await handle_one_arg(str, f, ret, self); - } else { - co_await handle_more_args(str, f, ret, self); - } + co_await handle_more_args(str, f, ret, self); } - } catch (std::invalid_argument &e) { - ret.ec = rpc_errc::invalid_argument; - ret.result = e.what(); - } catch (const std::exception &e) { - ret.ec = rpc_errc::function_exception; - ret.result = e.what(); } }; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ca08e8b..84a5a3f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,7 +6,7 @@ include_directories( "../thirdparty/asio" ) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") +#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") add_executable(test_rest_rpc test_rest_rpc.cpp) diff --git a/tests/test_rest_rpc.cpp b/tests/test_rest_rpc.cpp index 5963787..b18de2c 100644 --- a/tests/test_rest_rpc.cpp +++ b/tests/test_rest_rpc.cpp @@ -87,6 +87,10 @@ asio::awaitable echo_coro(std::string str) { co_return str; } void no_arg() { std::cout << "no args\n"; } +void exception_func() { throw std::invalid_argument("invalid"); } + +void unknown_exception_func() { throw 1; } + asio::awaitable no_arg_coro() { std::cout << "no args\n"; co_return; @@ -109,8 +113,25 @@ asio::awaitable test_router() { router.register_handler(); router.register_handler(); router.register_handler(); + router.register_handler(); + + auto name = router.get_name_by_key(get_key()); + CHECK(name == "add"); + router.remove_handler(); + name = router.get_name_by_key(get_key()); + CHECK(name != "add"); + router.register_handler(); + name = router.get_name_by_key(get_key()); + CHECK(name == "add"); + CHECK_THROWS_AS(router.register_handler(), std::invalid_argument); + + name = router.get_name_by_key(111); + CHECK(name == "111"); { + auto ret0 = co_await router.route(get_key(), ""); + CHECK(ret0.ec == rpc_errc::ok); + auto ret = co_await router.route(get_key(), ""); CHECK(ret.ec == rpc_errc::ok); @@ -119,6 +140,15 @@ asio::awaitable test_router() { auto ret2 = co_await router.route(get_key(), "test"); CHECK(ret2.ec == rpc_errc::ok); + + router.register_handler(); + router.register_handler(); + auto ret3 = co_await router.route(get_key(), ""); + CHECK(ret3.ec == rpc_errc::function_exception); + auto ret4 = co_await router.route(get_key(), ""); + CHECK(ret4.ec == rpc_errc::function_unknown_exception); + auto ret5 = co_await router.route(111, ""); + CHECK(ret5.ec == rpc_errc::no_such_function); } dummy d{};