3#include <spdlog/spdlog.h>
15 : response_(std::move(other.response_)) {
19 : response_(std::move(response)) {
21 response_[
"id"] =
id.value();
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);
32 if (response_json.contains(
"error")) {
33 return CreateUserError(response_json[
"error"],
id);
35 throw std::invalid_argument(
36 "Response JSON must contain either 'result' or 'error' field");
40 const nlohmann::json &result,
41 const std::optional<nlohmann::json> &
id) ->
Response {
42 nlohmann::json response = {{
"jsonrpc",
"2.0"}, {
"result", result}};
44 response[
"id"] =
id.value();
46 return Response{std::move(response)};
50 const nlohmann::json &error,
51 const std::optional<nlohmann::json> &
id) ->
Response {
52 nlohmann::json response = {{
"jsonrpc",
"2.0"}, {
"error", error}};
54 response[
"id"] =
id.value();
56 return Response{std::move(response)};
64 return response_.dump();
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)};
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}};
81 response[
"id"] =
id.value();
83 response[
"id"] =
nullptr;
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.");
94 if (response_.contains(
"error")) {
95 const auto &error = response_[
"error"];
96 if (!error.contains(
"code") || !error.contains(
"message")) {
98 "Response validation failed: missing 'code' or 'message' "
100 throw std::invalid_argument(
101 "Error object must contain 'code' and 'message' fields.");
Represents a JSON-RPC response.
static auto CreateLibError(LibErrorKind error_kind, const std::optional< nlohmann::json > &id=std::nullopt) -> Response
Creates a Response object for a library error.
auto ToJson() const -> nlohmann::json
Serializes the Response object to a JSON object.
static auto CreateUserError(const nlohmann::json &error, const std::optional< nlohmann::json > &id) -> Response
Creates a Response object for a user error.
static auto CreateResult(const nlohmann::json &result, const std::optional< nlohmann::json > &id) -> Response
Creates a successful Response object.
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.
auto ToStr() const -> std::string
Serializes the Response object to a string.
LibErrorKind
Enumeration for library error kinds.
std::unordered_map< LibErrorKind, std::pair< int, const char * > > ErrorInfoMap