JSON RPC
JSON-RPC 2.0 library for C++
exception.h
Go to the documentation of this file.
1#ifndef CC75354D_5C03_4B34_B773_96A9E6189611
2#define CC75354D_5C03_4B34_B773_96A9E6189611
3
4/**
5 * @file exception.h
6 * @brief Contains the definition of the JSON RPC Exception class.
7 * @see https://www.jsonrpc.org/specification#error_object
8 */
9
10#include <exception>
11#include <string>
12#include <string_view>
13#include <nlohmann/json.hpp>
14
15#include "export.h"
16
17namespace wwa::json_rpc {
18
19/**
20 * @defgroup error_message Error Messages
21 * @brief Error messages used by the library. These constants can be useful in unit tests.
22 * @{
23 */
24
25/**
26 * @brief Error message for when the request is not a JSON-RPC 2.0 request.
27 * @see exception::INVALID_REQUEST
28 * @see https://www.jsonrpc.org/specification#request_object
29 * @details > `jsonrpc`: A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
30 */
31static constexpr std::string_view err_not_jsonrpc_2_0_request = "Not a JSON-RPC 2.0 request";
32
33/**
34 * @brief Error message for when the parameters passed to the method are not correct.
35 * @see exception::INVALID_PARAMS
36 */
37static constexpr std::string_view err_invalid_params_passed_to_method = "Invalid parameters passed to method";
38
39/**
40 * @brief Error message for when the method is not found.
41 * @see exception::METHOD_NOT_FOUND
42 */
43static constexpr std::string_view err_method_not_found = "Method not found";
44
45/**
46 * @brief Error message for when the method is empty.
47 * @see exception::INVALID_REQUEST
48 */
49static constexpr std::string_view err_empty_method = "Method cannot be empty";
50
51/**
52 * @brief Error message for when the parameters are not an array or an object.
53 * @see exception::INVALID_PARAMS
54 */
55static constexpr std::string_view err_bad_params_type = "Parameters must be either an array or an object or omitted";
56
57/**
58 * @brief Error message for when the ID is not a number, a string, or null.
59 * @see exception::INVALID_REQUEST
60 * @see https://www.jsonrpc.org/specification#request_object
61 * @details > An identifier established by the Client that MUST contain a String, Number, or NULL value if included.
62 */
63static constexpr std::string_view err_bad_id_type = "ID must be either a number, a string, or null";
64
65/**
66 * @brief Error message for when the batch request is empty.
67 * @see exception::INVALID_REQUEST
68 * @see https://www.jsonrpc.org/specification#batch
69 */
70static constexpr std::string_view err_empty_batch = "Empty batch request";
71/** @} */
72
73/**
74 * @brief JSON RPC Exception class.
75 *
76 * This class represents an exception that can occur during the processing of JSON RPC requests.
77 * It includes an error code, a message, and optional additional data.
78 */
79class WWA_JSONRPC_EXPORT exception : public std::exception {
80public:
81 /**
82 * @defgroup error_codes Error Codes
83 * @brief Error codes defined by the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object).
84 * @see https://www.jsonrpc.org/specification#error_object
85 * @{
86 */
87
88 /**
89 * @brief Invalid JSON was received by the server.
90 *
91 * An error occurred on the server while parsing the JSON text.
92 */
93 static constexpr int PARSE_ERROR = -32700;
94
95 /**
96 * @brief The JSON sent is not a valid Request object.
97 */
98 static constexpr int INVALID_REQUEST = -32600;
99
100 /**
101 * @brief The method does not exist or is not available.
102 */
103 static constexpr int METHOD_NOT_FOUND = -32601;
104
105 /**
106 * @brief Invalid method parameter(s).
107 */
108 static constexpr int INVALID_PARAMS = -32602;
109
110 /**
111 * @brief Internal JSON-RPC error.
112 */
113 static constexpr int INTERNAL_ERROR = -32603;
114 /** @} */
115
116 /**
117 * @brief Construct a new exception object with additional data.
118 *
119 * @tparam T Type of the @a data. Must be [convertible to `nlohmann::json`](https://github.com/nlohmann/json?tab=readme-ov-file#arbitrary-types-conversions).
120 * @param code Indicates the error type that occurred.
121 * @param message Provides a short description of the error. The message SHOULD be limited to a concise single sentence.
122 * @param data Additional information about the error.
123 *
124 * @see https://www.jsonrpc.org/specification#error_object
125 */
126 template<typename T>
127 exception(int code, std::string_view message, const T& data) : m_message(message), m_data(data), m_code(code)
128 {}
129
130 /**
131 * @brief Construct a new exception object.
132 *
133 * @param code Indicates the error type that occurred.
134 * @param message Provides a short description of the error. The message SHOULD be limited to a concise single sentence.
135 *
136 * @see https://www.jsonrpc.org/specification#error_object
137 */
138 exception(int code, std::string_view message) : m_message(message), m_code(code) {}
139
140 /**
141 * @brief Default copy constructor.
142 */
143 exception(const exception&) = default;
144
145 /**
146 * @brief Default move constructor.
147 */
148 exception(exception&&) = default;
149
150 /**
151 * @brief Default copy assignment operator.
152 *
153 * @param rhs Right-hand side of the assignment.
154 * @return Reference to this object.
155 */
156 exception& operator=(const exception& rhs) = default;
157
158 /**
159 * @brief Default move assignment operator.
160 *
161 * @param rhs Right-hand side of the assignment.
162 * @return Reference to this object.
163 */
164 exception& operator=(exception&& rhs) = default;
165
166 /**
167 * @brief Default destructor
168 */
169 ~exception() override;
170
171 /**
172 * @brief Returns the error code.
173 *
174 * @return Error code.
175 */
176 [[nodiscard]] int code() const noexcept { return this->m_code; }
177
178 /**
179 * @brief Returns the error message.
180 *
181 * @return Error message.
182 */
183 [[nodiscard]] const std::string& message() const noexcept { return this->m_message; }
184
185 /**
186 * @brief Returns custom data associated with the error.
187 *
188 * @return Custom data in JSON format.
189 */
190 [[nodiscard]] const nlohmann::json& data() const noexcept { return this->m_data; }
191
192 /**
193 * @brief Returns the error message.
194 *
195 * @see https://en.cppreference.com/w/cpp/error/exception/what
196 * @return Pointer to a null-terminated string with explanatory information.
197 *
198 * @see message()
199 */
200 [[nodiscard]] const char* what() const noexcept override { return this->m_message.c_str(); }
201
202 /**
203 * @brief Returns the error message as an Error Object.
204 *
205 * @see https://www.jsonrpc.org/specification#error_object
206 * @return Error Object as JSON.
207 */
209 {
210 nlohmann::json j{
211 {"code", this->m_code},
212 {"message", this->m_message},
213 };
214
215 if (!this->m_data.is_null()) {
216 j["data"] = this->m_data;
217 }
218
219 return j;
220 }
221
222private:
223 std::string m_message; ///< Error message.
224 nlohmann::json m_data; ///< Custom data associated with the error.
225 int m_code; ///< Error code.
226};
227
228} // namespace wwa::json_rpc
229
230#endif /* CC75354D_5C03_4B34_B773_96A9E6189611 */
Private implementation of the JSON RPC dispatcher class.
A class that manages JSON RPC method handlers and processes JSON RPC requests.
Definition dispatcher.h:77
dispatcher & operator=(dispatcher &&rhs)=default
Move assignment operator.
dispatcher(dispatcher &&rhs)=default
Move constructor.
virtual void on_request(const nlohmann::json &extra)
Invoked when a request is received.
void add_ex(std::string_view method, F &&f, C instance)
Adds a method handler with an extra parameter and a class instance.
Definition dispatcher.h:277
std::string parse_and_process_request(const std::string &request, const nlohmann::json &extra=nlohmann::json::object())
Parses and processes a JSON RPC request.
constexpr auto create_closure(C inst, F &&f) const
Creates a closure for invoking a member function with JSON parameters.
Definition dispatcher.h:455
std::string process_request(const nlohmann::json &request, const nlohmann::json &extra=nlohmann::json::object())
Processes a JSON RPC request.
virtual void on_request_processed(const std::string &method, int code, const nlohmann::json &extra)
Invoked after the method handler is called.
void add_internal_method(std::string_view method, handler_t &&handler)
Adds a method handler for the specified method.
void add(std::string_view method, F &&f)
Adds a method handler f for the method method.
Definition dispatcher.h:192
virtual void on_method(const std::string &method, const nlohmann::json &extra)
Invoked right before the method handler is called.
void add(std::string_view method, F &&f, C instance)
Adds a method to the dispatcher with the specified instance and function.
Definition dispatcher.h:210
dispatcher()
Class constructor.
dispatcher(const dispatcher &)=delete
dispatcher & operator=(const dispatcher &)=delete
virtual ~dispatcher()
Class destructor.
void add_ex(std::string_view method, F &&f)
Adds a method handler with an extra parameter.
Definition dispatcher.h:254
std::unique_ptr< dispatcher_private > d_ptr
Pointer to the implementation (Pimpl idiom).
Definition dispatcher.h:413
JSON RPC Exception class.
Definition exception.h:79
exception(const exception &)=default
Default copy constructor.
~exception() override
Default destructor.
nlohmann::json m_data
Custom data associated with the error.
Definition exception.h:224
std::string m_message
Error message.
Definition exception.h:223
int code() const noexcept
Returns the error code.
Definition exception.h:176
const std::string & message() const noexcept
Returns the error message.
Definition exception.h:183
exception(exception &&)=default
Default move constructor.
exception(int code, std::string_view message)
Construct a new exception object.
Definition exception.h:138
exception & operator=(const exception &rhs)=default
Default copy assignment operator.
const nlohmann::json & data() const noexcept
Returns custom data associated with the error.
Definition exception.h:190
exception & operator=(exception &&rhs)=default
Default move assignment operator.
const char * what() const noexcept override
Returns the error message.
Definition exception.h:200
nlohmann::json to_json() const
Returns the error message as an Error Object.
Definition exception.h:208
exception(int code, std::string_view message, const T &data)
Construct a new exception object with additional data.
Definition exception.h:127
int m_code
Error code.
Definition exception.h:225
#define WWA_JSONRPC_EXPORT
Macro for exporting symbols when building the library dynamically or importing symbols when using the...
Definition export.h:42
static constexpr int INVALID_PARAMS
Invalid method parameter(s).
Definition exception.h:108
static constexpr int INTERNAL_ERROR
Internal JSON-RPC error.
Definition exception.h:113
static constexpr int METHOD_NOT_FOUND
The method does not exist or is not available.
Definition exception.h:103
static constexpr int INVALID_REQUEST
The JSON sent is not a valid Request object.
Definition exception.h:98
static constexpr int PARSE_ERROR
Invalid JSON was received by the server.
Definition exception.h:93
static constexpr std::string_view err_method_not_found
Error message for when the method is not found.
Definition exception.h:43
static constexpr std::string_view err_not_jsonrpc_2_0_request
Error message for when the request is not a JSON-RPC 2.0 request.
Definition exception.h:31
static constexpr std::string_view err_empty_batch
Error message for when the batch request is empty.
Definition exception.h:70
static constexpr std::string_view err_empty_method
Error message for when the method is empty.
Definition exception.h:49
static constexpr std::string_view err_bad_params_type
Error message for when the parameters are not an array or an object.
Definition exception.h:55
static constexpr std::string_view err_invalid_params_passed_to_method
Error message for when the parameters passed to the method are not correct.
Definition exception.h:37
static constexpr std::string_view err_bad_id_type
Error message for when the ID is not a number, a string, or null.
Definition exception.h:63
constexpr auto convert_args(const nlohmann::json &params, std::index_sequence< Indices... >)
Converts JSON parameters to a tuple of arguments based on the specified types.
Definition details.h:346
constexpr auto make_inst_tuple(C inst)
Creates a tuple containing the instance if it is a class pointer, or an empty tuple if it is a null p...
Definition details.h:239
Contains the implementation details of the JSON RPC library.
Definition details.h:31
Primary template for function traits.
Definition details.h:172