JSON RPC
JSON-RPC 2.0 library for C++
wwa::json_rpc::exception Class Reference

JSON RPC Exception class. More...

#include <exception.h>

+ Inheritance diagram for wwa::json_rpc::exception:
+ Collaboration diagram for wwa::json_rpc::exception:

Public Member Functions

 exception (const exception &)=default
 Default copy constructor.
 
 exception (exception &&)=default
 Default move constructor.
 
 exception (int code, std::string_view message)
 Construct a new exception object.
 
template<typename T >
 exception (int code, std::string_view message, const T &data)
 Construct a new exception object with additional data.
 
 ~exception () override
 Default destructor.
 
int code () const noexcept
 Returns the error code.
 
const nlohmann::json & data () const noexcept
 Returns custom data associated with the error.
 
const std::string & message () const noexcept
 Returns the error message.
 
exceptionoperator= (const exception &rhs)=default
 Default copy assignment operator.
 
exceptionoperator= (exception &&rhs)=default
 Default move assignment operator.
 
nlohmann::json to_json () const
 Returns the error message as an Error Object.
 
const char * what () const noexcept override
 Returns the error message.
 

Static Public Attributes

static constexpr int INTERNAL_ERROR = -32603
 Internal JSON-RPC error.
 
static constexpr int INVALID_PARAMS = -32602
 Invalid method parameter(s).
 
static constexpr int INVALID_REQUEST = -32600
 The JSON sent is not a valid Request object.
 
static constexpr int METHOD_NOT_FOUND = -32601
 The method does not exist or is not available.
 
static constexpr int PARSE_ERROR = -32700
 Invalid JSON was received by the server.
 

Private Attributes

int m_code
 Error code.
 
nlohmann::json m_data
 Custom data associated with the error.
 
std::string m_message
 Error message.
 

Detailed Description

JSON RPC Exception class.

This class represents an exception that can occur during the processing of JSON RPC requests. It includes an error code, a message, and optional additional data.

Definition at line 79 of file exception.h.

Constructor & Destructor Documentation

◆ exception() [1/4]

template<typename T >
wwa::json_rpc::exception::exception ( int code,
std::string_view message,
const T & data )
inline

Construct a new exception object with additional data.

Template Parameters
TType of the data. Must be convertible to nlohmann::json.
Parameters
codeIndicates the error type that occurred.
messageProvides a short description of the error. The message SHOULD be limited to a concise single sentence.
dataAdditional information about the error.
See also
https://www.jsonrpc.org/specification#error_object

Definition at line 127 of file exception.h.

128 {}
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
const nlohmann::json & data() const noexcept
Returns custom data associated with the error.
Definition exception.h:190
int m_code
Error code.
Definition exception.h:225

◆ exception() [2/4]

wwa::json_rpc::exception::exception ( int code,
std::string_view message )
inline

Construct a new exception object.

Parameters
codeIndicates the error type that occurred.
messageProvides a short description of the error. The message SHOULD be limited to a concise single sentence.
See also
https://www.jsonrpc.org/specification#error_object

Definition at line 138 of file exception.h.

◆ exception() [3/4]

wwa::json_rpc::exception::exception ( const exception & )
default

Default copy constructor.

◆ exception() [4/4]

wwa::json_rpc::exception::exception ( exception && )
default

Default move constructor.

◆ ~exception()

wwa::json_rpc::exception::~exception ( )
overridedefault

Default destructor.

Member Function Documentation

◆ code()

int wwa::json_rpc::exception::code ( ) const
inlinenodiscardnoexcept

Returns the error code.

Returns
Error code.

Definition at line 176 of file exception.h.

176{ return this->m_code; }

◆ data()

const nlohmann::json & wwa::json_rpc::exception::data ( ) const
inlinenodiscardnoexcept

Returns custom data associated with the error.

Returns
Custom data in JSON format.

Definition at line 190 of file exception.h.

190{ return this->m_data; }

◆ message()

const std::string & wwa::json_rpc::exception::message ( ) const
inlinenodiscardnoexcept

Returns the error message.

Returns
Error message.

Definition at line 183 of file exception.h.

183{ return this->m_message; }

◆ operator=() [1/2]

exception & wwa::json_rpc::exception::operator= ( const exception & rhs)
default

Default copy assignment operator.

Parameters
rhsRight-hand side of the assignment.
Returns
Reference to this object.

◆ operator=() [2/2]

exception & wwa::json_rpc::exception::operator= ( exception && rhs)
default

Default move assignment operator.

Parameters
rhsRight-hand side of the assignment.
Returns
Reference to this object.

◆ to_json()

nlohmann::json wwa::json_rpc::exception::to_json ( ) const
inlinenodiscard

Returns the error message as an Error Object.

See also
https://www.jsonrpc.org/specification#error_object
Returns
Error Object as JSON.

Definition at line 208 of file exception.h.

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 }

◆ what()

const char * wwa::json_rpc::exception::what ( ) const
inlinenodiscardoverridenoexcept

Returns the error message.

See also
https://en.cppreference.com/w/cpp/error/exception/what
Returns
Pointer to a null-terminated string with explanatory information.
See also
message()

Definition at line 200 of file exception.h.

200{ return this->m_message.c_str(); }

Member Data Documentation

◆ m_code

int wwa::json_rpc::exception::m_code
private

Error code.

Definition at line 225 of file exception.h.

◆ m_data

nlohmann::json wwa::json_rpc::exception::m_data
private

Custom data associated with the error.

Definition at line 224 of file exception.h.

◆ m_message

std::string wwa::json_rpc::exception::m_message
private

Error message.

Definition at line 223 of file exception.h.


The documentation for this class was generated from the following files: