From 6080724de380b1a552228556313f3433e03ae8e6 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 17 Sep 2025 14:05:49 +0800 Subject: [PATCH 1/3] error code --- CMakeLists.txt | 2 +- cmake/build.cmake | 5 ++- include/rest_rpc/error_code.h | 64 +++++++++++++++++++++++++++++++++ include/rest_rpc/router.h | 13 ++++--- include/rest_rpc/rpc_client.hpp | 18 ++++------ include/rest_rpc/rpc_server.h | 8 ++--- 6 files changed, 86 insertions(+), 24 deletions(-) create mode 100644 include/rest_rpc/error_code.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 594f4b9..96a7a07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.1) +cmake_minimum_required(VERSION 3.5) project(rest_rpc) include_directories(include) diff --git a/cmake/build.cmake b/cmake/build.cmake index 2a71653..6927ec3 100644 --- a/cmake/build.cmake +++ b/cmake/build.cmake @@ -1,5 +1,8 @@ # Compile Standard -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11") +set(CMAKE_CXX_STANDARD 11) +if(UNIX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") +endif() # Build Type if(NOT CMAKE_BUILD_TYPE) diff --git a/include/rest_rpc/error_code.h b/include/rest_rpc/error_code.h new file mode 100644 index 0000000..462667d --- /dev/null +++ b/include/rest_rpc/error_code.h @@ -0,0 +1,64 @@ +#pragma once +#include +#include + +namespace rest_rpc { +enum class rpc_errc { + ok = 0, + resolve_error = 1, + no_such_key, + invalid_req_type, + function_exception, + unknown_exception, + no_such_function, + socket_closed, + connect_timeout, + request_timeout, + unknown +}; + +class http_error_category : public std::error_category { +public: + const char *name() const noexcept override { return "rest_rpc_error"; } + + std::string message(int ev) const override { + switch (static_cast(ev)) { + case rpc_errc::ok: + return "ok"; + case rpc_errc::resolve_error: + return "resolve failed"; + case rpc_errc::no_such_key: + return "resolve failed"; + case rpc_errc::invalid_req_type: + return "invalid request type"; + case rpc_errc::function_exception: + return "logic function exception happend"; + case rpc_errc::unknown_exception: + return "unknown function exception happend"; + case rpc_errc::no_such_function: + return "no such function"; + case rpc_errc::socket_closed: + return "socket closed"; + case rpc_errc::connect_timeout: + return "Connect timeout"; + case rpc_errc::request_timeout: + return "Request timeout"; + default: + return "Unknown error"; + } + } +}; + +inline rest_rpc::http_error_category &category() { + static rest_rpc::http_error_category instance; + return instance; +} + +inline std::error_code make_error_code(rpc_errc e) { + return {static_cast(e), category()}; +} + +inline bool operator==(const std::error_code &code, rpc_errc ec) { + return code.value() == (int)ec; +} +} // namespace rest_rpc \ No newline at end of file diff --git a/include/rest_rpc/router.h b/include/rest_rpc/router.h index 6d3a01b..a574b53 100644 --- a/include/rest_rpc/router.h +++ b/include/rest_rpc/router.h @@ -2,6 +2,7 @@ #define REST_RPC_ROUTER_H_ #include "codec.h" +#include "error_code.h" #include "md5.hpp" #include "meta_util.hpp" #include "string_view.hpp" @@ -14,10 +15,8 @@ namespace rest_rpc { namespace rpc_service { class connection; -enum class router_error { ok, no_such_function, has_exception, unkonw }; - struct route_result_t { - router_error ec = router_error::unkonw; + rpc_errc ec = rpc_errc::unknown; std::string result; }; @@ -95,23 +94,23 @@ public: if (it == map_invokers_.end()) { result = codec.pack_args_str( result_code::FAIL, "unknown function: " + get_name_by_key(key)); - route_result.ec = router_error::no_such_function; + route_result.ec = rpc_errc::no_such_function; } else { it->second(conn, data, result); - route_result.ec = router_error::ok; + route_result.ec = rpc_errc::ok; } } catch (const std::exception &ex) { msgpack_codec codec; result = codec.pack_args_str( result_code::FAIL, std::string("exception occur when call").append(ex.what())); - route_result.ec = router_error::has_exception; + route_result.ec = rpc_errc::function_exception; } catch (...) { msgpack_codec codec; result = codec.pack_args_str( result_code::FAIL, std::string("unknown exception occur when call ") .append(get_name_by_key(key))); - route_result.ec = router_error::no_such_function; + route_result.ec = rpc_errc::unknown_exception; } route_result.result = std::move(result); diff --git a/include/rest_rpc/rpc_client.hpp b/include/rest_rpc/rpc_client.hpp index e1825d8..ad8d057 100644 --- a/include/rest_rpc/rpc_client.hpp +++ b/include/rest_rpc/rpc_client.hpp @@ -1,6 +1,7 @@ #pragma once #include "client_util.hpp" #include "const_vars.h" +#include "error_code.h" #include "md5.hpp" #include "meta_util.hpp" #include "use_asio.hpp" @@ -292,7 +293,7 @@ public: encoded_func_name_and_args.size()); write(fu_id, request_type::req_res, std::move(sbuffer), MD5::MD5Hash32(encoded_func_name_and_args.data())); - return fu_id; + return (long)fu_id; } template @@ -553,9 +554,7 @@ private: if (!socket_.is_open()) { // LOG(INFO) << "socket already closed"; - call_back(req_id, - asio::error::make_error_code(asio::error::connection_aborted), - {}); + call_back(req_id, make_error_code(rpc_errc::socket_closed), {}); return; } @@ -567,8 +566,7 @@ private: callback_sub(ec, {body_.data(), body_len}); } else { close(); - error_callback( - asio::error::make_error_code(asio::error::invalid_argument)); + error_callback(make_error_code(rpc_errc::invalid_req_type)); return; } @@ -604,7 +602,7 @@ private: // For Java client. // TODO(qwang): Call java callback. // handle error. - on_result_received_callback_(req_id, + on_result_received_callback_((long)req_id, std::string(data.data(), data.size())); } else { // For CPP client. @@ -625,8 +623,7 @@ private: cl->cancel(); cl->callback(ec, data); } else { - cl->callback(asio::error::make_error_code(asio::error::timed_out), - {}); + cl->callback(make_error_code(rpc_errc::request_timeout), {}); } std::unique_lock lock(cb_mtx_); @@ -670,8 +667,7 @@ private: it->second(data); } catch (const std::exception & /*ex*/) { - error_callback( - asio::error::make_error_code(asio::error::invalid_argument)); + error_callback(make_error_code(rpc_errc::function_exception)); } } diff --git a/include/rest_rpc/rpc_server.h b/include/rest_rpc/rpc_server.h index 3fbe17b..a99a72c 100644 --- a/include/rest_rpc/rpc_server.h +++ b/include/rest_rpc/rpc_server.h @@ -2,6 +2,7 @@ #define REST_RPC_RPC_SERVER_H_ #include "connection.h" +#include "error_code.h" #include "io_service_pool.h" #include "router.h" #include @@ -162,7 +163,7 @@ private: auto it = endpoints.begin(); if (it == endpoints.end()) { - return std::make_error_code(std::errc::bad_address); + return make_error_code(rpc_errc::resolve_error); } auto endpoint = it->endpoint(); @@ -263,9 +264,8 @@ private: conn->publish(key + token, *shared_data); } } else { - error_callback( - asio::error::make_error_code(asio::error::invalid_argument), - "The subscriber of the key: " + key + " does not exist."); + error_callback(make_error_code(rpc_errc::no_such_key), + "The subscriber of the key: " + key + " does not exist."); } } From 27c39f653033c787c9f8df5892ba60cb10c4285e Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 17 Sep 2025 14:11:02 +0800 Subject: [PATCH 2/3] fix case --- tests/test_rest_rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_rest_rpc.cpp b/tests/test_rest_rpc.cpp index d85f079..021c75a 100644 --- a/tests/test_rest_rpc.cpp +++ b/tests/test_rest_rpc.cpp @@ -349,7 +349,7 @@ TEST_CASE("test_client_subscribe_not_exist_key") { server.set_error_callback([&stop](asio::error_code ec, string_view msg) { std::cout << "line: " << __LINE__ << ", msg: " << ec.message() << " -- " << msg << std::endl; - CHECK_EQ(ec, asio::error::invalid_argument); + CHECK_EQ(ec, rpc_error::no_such_key); stop = true; }); server.set_conn_timeout_callback([](int64_t conn_id) { From 4e724478878517116c689ff4248a75b5cf7f1dca Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 17 Sep 2025 14:18:10 +0800 Subject: [PATCH 3/3] compile --- tests/test_rest_rpc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_rest_rpc.cpp b/tests/test_rest_rpc.cpp index 021c75a..69b1e2a 100644 --- a/tests/test_rest_rpc.cpp +++ b/tests/test_rest_rpc.cpp @@ -349,7 +349,7 @@ TEST_CASE("test_client_subscribe_not_exist_key") { server.set_error_callback([&stop](asio::error_code ec, string_view msg) { std::cout << "line: " << __LINE__ << ", msg: " << ec.message() << " -- " << msg << std::endl; - CHECK_EQ(ec, rpc_error::no_such_key); + CHECK_EQ(ec, rpc_errc::no_such_key); stop = true; }); server.set_conn_timeout_callback([](int64_t conn_id) {