JSON-RPC 2.0
JSON-RPC 2.0 Modern C++ Library
Loading...
Searching...
No Matches
response.cpp
Go to the documentation of this file.
2
3#include <spdlog/spdlog.h>
4
5namespace jsonrpc::server {
6
7const ErrorInfoMap Response::kErrorInfoMap = {
8 {LibErrorKind::kParseError, {-32700, "Parse error"}},
9 {LibErrorKind::kInvalidRequest, {-32600, "Invalid Request"}},
10 {LibErrorKind::kMethodNotFound, {-32601, "Method not found"}},
11 {LibErrorKind::kInternalError, {-32603, "Internal error"}},
12 {LibErrorKind::kServerError, {-32000, "Server error"}}};
13
15 : response_(std::move(other.response_)) {
16}
17
18Response::Response(nlohmann::json response, std::optional<nlohmann::json> id)
19 : response_(std::move(response)) {
20 if (id.has_value()) {
21 response_["id"] = id.value();
22 }
23 ValidateResponse();
24}
25
27 const nlohmann::json &response_json,
28 std::optional<nlohmann::json> id) -> Response {
29 if (response_json.contains("result")) {
30 return CreateResult(response_json["result"], id);
31 }
32 if (response_json.contains("error")) {
33 return CreateUserError(response_json["error"], id);
34 }
35 throw std::invalid_argument(
36 "Response JSON must contain either 'result' or 'error' field");
37}
38
40 const nlohmann::json &result,
41 const std::optional<nlohmann::json> &id) -> Response {
42 nlohmann::json response = {{"jsonrpc", "2.0"}, {"result", result}};
43 if (id.has_value()) {
44 response["id"] = id.value();
45 }
46 return Response{std::move(response)};
47}
48
50 const nlohmann::json &error,
51 const std::optional<nlohmann::json> &id) -> Response {
52 nlohmann::json response = {{"jsonrpc", "2.0"}, {"error", error}};
53 if (id.has_value()) {
54 response["id"] = id.value();
55 }
56 return Response{std::move(response)};
57}
58
59auto Response::ToJson() const -> nlohmann::json {
60 return response_;
61}
62
63auto Response::ToStr() const -> std::string {
64 return response_.dump();
65}
66
68 LibErrorKind error_kind,
69 const std::optional<nlohmann::json> &id) -> Response {
70 const auto &[code, message] = kErrorInfoMap.at(error_kind);
71 nlohmann::json error_response = CreateErrorResponse(message, code, id);
72 return Response{std::move(error_response)};
73}
74
75auto Response::CreateErrorResponse(
76 const std::string &message, int code,
77 const std::optional<nlohmann::json> &id) -> nlohmann::json {
78 nlohmann::json error = {{"code", code}, {"message", message}};
79 nlohmann::json response = {{"jsonrpc", "2.0"}, {"error", error}};
80 if (id.has_value()) {
81 response["id"] = id.value();
82 } else {
83 response["id"] = nullptr;
84 }
85 return response;
86}
87
88void Response::ValidateResponse() const {
89 if (!response_.contains("result") && !response_.contains("error")) {
90 spdlog::error("Response validation failed: missing 'result' or 'error'");
91 throw std::invalid_argument(
92 "Response must contain either 'result' or 'error' field.");
93 }
94 if (response_.contains("error")) {
95 const auto &error = response_["error"];
96 if (!error.contains("code") || !error.contains("message")) {
97 spdlog::error(
98 "Response validation failed: missing 'code' or 'message' "
99 "in error object");
100 throw std::invalid_argument(
101 "Error object must contain 'code' and 'message' fields.");
102 }
103 }
104}
105
106} // namespace jsonrpc::server
Represents a JSON-RPC response.
Definition response.hpp:24
static auto CreateLibError(LibErrorKind error_kind, const std::optional< nlohmann::json > &id=std::nullopt) -> Response
Creates a Response object for a library error.
Definition response.cpp:67
auto ToJson() const -> nlohmann::json
Serializes the Response object to a JSON object.
Definition response.cpp:59
static auto CreateUserError(const nlohmann::json &error, const std::optional< nlohmann::json > &id) -> Response
Creates a Response object for a user error.
Definition response.cpp:49
static auto CreateResult(const nlohmann::json &result, const std::optional< nlohmann::json > &id) -> Response
Creates a successful Response object.
Definition response.cpp:39
static auto FromUserResponse(const nlohmann::json &response_json, std::optional< nlohmann::json > id) -> Response
Creates a Response object from a JSON object that represents a user response.
Definition response.cpp:26
auto ToStr() const -> std::string
Serializes the Response object to a string.
Definition response.cpp:63
LibErrorKind
Enumeration for library error kinds.
Definition response.hpp:12
std::unordered_map< LibErrorKind, std::pair< int, const char * > > ErrorInfoMap
Definition response.hpp:20