Skip to content

Commit d66b407

Browse files
authored
Merge pull request #168 from qicosmos/error_code
2 parents cf57512 + 4e72447 commit d66b407

File tree

7 files changed

+87
-25
lines changed

7 files changed

+87
-25
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.1)
1+
cmake_minimum_required(VERSION 3.5)
22
project(rest_rpc)
33

44
include_directories(include)

cmake/build.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Compile Standard
2-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11")
2+
set(CMAKE_CXX_STANDARD 11)
3+
if(UNIX)
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
5+
endif()
36

47
# Build Type
58
if(NOT CMAKE_BUILD_TYPE)

include/rest_rpc/error_code.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#pragma once
2+
#include <string>
3+
#include <system_error>
4+
5+
namespace rest_rpc {
6+
enum class rpc_errc {
7+
ok = 0,
8+
resolve_error = 1,
9+
no_such_key,
10+
invalid_req_type,
11+
function_exception,
12+
unknown_exception,
13+
no_such_function,
14+
socket_closed,
15+
connect_timeout,
16+
request_timeout,
17+
unknown
18+
};
19+
20+
class http_error_category : public std::error_category {
21+
public:
22+
const char *name() const noexcept override { return "rest_rpc_error"; }
23+
24+
std::string message(int ev) const override {
25+
switch (static_cast<rpc_errc>(ev)) {
26+
case rpc_errc::ok:
27+
return "ok";
28+
case rpc_errc::resolve_error:
29+
return "resolve failed";
30+
case rpc_errc::no_such_key:
31+
return "resolve failed";
32+
case rpc_errc::invalid_req_type:
33+
return "invalid request type";
34+
case rpc_errc::function_exception:
35+
return "logic function exception happend";
36+
case rpc_errc::unknown_exception:
37+
return "unknown function exception happend";
38+
case rpc_errc::no_such_function:
39+
return "no such function";
40+
case rpc_errc::socket_closed:
41+
return "socket closed";
42+
case rpc_errc::connect_timeout:
43+
return "Connect timeout";
44+
case rpc_errc::request_timeout:
45+
return "Request timeout";
46+
default:
47+
return "Unknown error";
48+
}
49+
}
50+
};
51+
52+
inline rest_rpc::http_error_category &category() {
53+
static rest_rpc::http_error_category instance;
54+
return instance;
55+
}
56+
57+
inline std::error_code make_error_code(rpc_errc e) {
58+
return {static_cast<int>(e), category()};
59+
}
60+
61+
inline bool operator==(const std::error_code &code, rpc_errc ec) {
62+
return code.value() == (int)ec;
63+
}
64+
} // namespace rest_rpc

include/rest_rpc/router.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define REST_RPC_ROUTER_H_
33

44
#include "codec.h"
5+
#include "error_code.h"
56
#include "md5.hpp"
67
#include "meta_util.hpp"
78
#include "string_view.hpp"
@@ -14,10 +15,8 @@ namespace rest_rpc {
1415
namespace rpc_service {
1516
class connection;
1617

17-
enum class router_error { ok, no_such_function, has_exception, unkonw };
18-
1918
struct route_result_t {
20-
router_error ec = router_error::unkonw;
19+
rpc_errc ec = rpc_errc::unknown;
2120
std::string result;
2221
};
2322

@@ -95,23 +94,23 @@ class router : asio::noncopyable {
9594
if (it == map_invokers_.end()) {
9695
result = codec.pack_args_str(
9796
result_code::FAIL, "unknown function: " + get_name_by_key(key));
98-
route_result.ec = router_error::no_such_function;
97+
route_result.ec = rpc_errc::no_such_function;
9998
} else {
10099
it->second(conn, data, result);
101-
route_result.ec = router_error::ok;
100+
route_result.ec = rpc_errc::ok;
102101
}
103102
} catch (const std::exception &ex) {
104103
msgpack_codec codec;
105104
result = codec.pack_args_str(
106105
result_code::FAIL,
107106
std::string("exception occur when call").append(ex.what()));
108-
route_result.ec = router_error::has_exception;
107+
route_result.ec = rpc_errc::function_exception;
109108
} catch (...) {
110109
msgpack_codec codec;
111110
result = codec.pack_args_str(
112111
result_code::FAIL, std::string("unknown exception occur when call ")
113112
.append(get_name_by_key(key)));
114-
route_result.ec = router_error::no_such_function;
113+
route_result.ec = rpc_errc::unknown_exception;
115114
}
116115

117116
route_result.result = std::move(result);

include/rest_rpc/rpc_client.hpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include "client_util.hpp"
33
#include "const_vars.h"
4+
#include "error_code.h"
45
#include "md5.hpp"
56
#include "meta_util.hpp"
67
#include "use_asio.hpp"
@@ -292,7 +293,7 @@ class rpc_client : private asio::noncopyable {
292293
encoded_func_name_and_args.size());
293294
write(fu_id, request_type::req_res, std::move(sbuffer),
294295
MD5::MD5Hash32(encoded_func_name_and_args.data()));
295-
return fu_id;
296+
return (long)fu_id;
296297
}
297298

298299
template <typename R, size_t TIMEOUT = DEFAULT_TIMEOUT, typename... Args>
@@ -553,9 +554,7 @@ class rpc_client : private asio::noncopyable {
553554

554555
if (!socket_.is_open()) {
555556
// LOG(INFO) << "socket already closed";
556-
call_back(req_id,
557-
asio::error::make_error_code(asio::error::connection_aborted),
558-
{});
557+
call_back(req_id, make_error_code(rpc_errc::socket_closed), {});
559558
return;
560559
}
561560

@@ -567,8 +566,7 @@ class rpc_client : private asio::noncopyable {
567566
callback_sub(ec, {body_.data(), body_len});
568567
} else {
569568
close();
570-
error_callback(
571-
asio::error::make_error_code(asio::error::invalid_argument));
569+
error_callback(make_error_code(rpc_errc::invalid_req_type));
572570
return;
573571
}
574572

@@ -604,7 +602,7 @@ class rpc_client : private asio::noncopyable {
604602
// For Java client.
605603
// TODO(qwang): Call java callback.
606604
// handle error.
607-
on_result_received_callback_(req_id,
605+
on_result_received_callback_((long)req_id,
608606
std::string(data.data(), data.size()));
609607
} else {
610608
// For CPP client.
@@ -625,8 +623,7 @@ class rpc_client : private asio::noncopyable {
625623
cl->cancel();
626624
cl->callback(ec, data);
627625
} else {
628-
cl->callback(asio::error::make_error_code(asio::error::timed_out),
629-
{});
626+
cl->callback(make_error_code(rpc_errc::request_timeout), {});
630627
}
631628

632629
std::unique_lock<std::mutex> lock(cb_mtx_);
@@ -670,8 +667,7 @@ class rpc_client : private asio::noncopyable {
670667

671668
it->second(data);
672669
} catch (const std::exception & /*ex*/) {
673-
error_callback(
674-
asio::error::make_error_code(asio::error::invalid_argument));
670+
error_callback(make_error_code(rpc_errc::function_exception));
675671
}
676672
}
677673

include/rest_rpc/rpc_server.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define REST_RPC_RPC_SERVER_H_
33

44
#include "connection.h"
5+
#include "error_code.h"
56
#include "io_service_pool.h"
67
#include "router.h"
78
#include <condition_variable>
@@ -162,7 +163,7 @@ class rpc_server : private asio::noncopyable {
162163
auto it = endpoints.begin();
163164

164165
if (it == endpoints.end()) {
165-
return std::make_error_code(std::errc::bad_address);
166+
return make_error_code(rpc_errc::resolve_error);
166167
}
167168

168169
auto endpoint = it->endpoint();
@@ -263,9 +264,8 @@ class rpc_server : private asio::noncopyable {
263264
conn->publish(key + token, *shared_data);
264265
}
265266
} else {
266-
error_callback(
267-
asio::error::make_error_code(asio::error::invalid_argument),
268-
"The subscriber of the key: " + key + " does not exist.");
267+
error_callback(make_error_code(rpc_errc::no_such_key),
268+
"The subscriber of the key: " + key + " does not exist.");
269269
}
270270
}
271271

tests/test_rest_rpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ TEST_CASE("test_client_subscribe_not_exist_key") {
349349
server.set_error_callback([&stop](asio::error_code ec, string_view msg) {
350350
std::cout << "line: " << __LINE__ << ", msg: " << ec.message() << " -- "
351351
<< msg << std::endl;
352-
CHECK_EQ(ec, asio::error::invalid_argument);
352+
CHECK_EQ(ec, rpc_errc::no_such_key);
353353
stop = true;
354354
});
355355
server.set_conn_timeout_callback([](int64_t conn_id) {

0 commit comments

Comments
 (0)