Skip to content

Commit

Permalink
[examples] improved dynamic jsonrpc errormessage example
Browse files Browse the repository at this point in the history
  • Loading branch information
MFransen69 committed Oct 24, 2024
1 parent eeba487 commit efa2bf1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
19 changes: 15 additions & 4 deletions examples/DynamicJSONRPCErrorMessage/DynamicJSONRPCErrorMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,33 @@ namespace Plugin {
return {};
}

uint32_t DynamicJSONRPCErrorMessage::OnJSONRPCError(const Core::JSONRPC::Context&, const string& designator, const string& parameters, string& errormessage) {
uint32_t DynamicJSONRPCErrorMessage::OnJSONRPCError(const Core::JSONRPC::Context&, const string& method, const string& parameters, string& errormessage) {
uint32_t result = Core::ERROR_GENERAL;

string method(Core::JSONRPC::Message::Method(designator));

if(method == _T("add")) {
JsonData::Math::AddParamsInfo addparams;
addparams.FromString(parameters);
std::stringstream message;
message <<_T("Error handling add method for some peculiar reason values: ") << addparams.A << _T(" and ") << addparams.B;
message <<_T("Error handling add method failed for some peculiar reason, values: ") << addparams.A << _T(" and ") << addparams.B;
errormessage = message.str();
result = Core::ERROR_INVALID_PARAMETER;
}
return result;
}

uint32_t DynamicJSONRPCErrorMessage::OnJSONRPCErrorMethod(const Core::JSONRPC::Context&, const string& method, const string& parameters, string& errormessage) {
uint32_t result = Core::ERROR_GENERAL;

if(method == _T("add")) {
JsonData::Math::AddParamsInfo addparams;
addparams.FromString(parameters);
std::stringstream message;
message <<_T("Error handling (method version) add method failed for some peculiar reason, values: ") << addparams.A << _T(" and ") << addparams.B;
errormessage = message.str();
result = Core::ERROR_INVALID_PARAMETER;
}
return result;
}

} // namespace Plugin

Expand Down
36 changes: 28 additions & 8 deletions examples/DynamicJSONRPCErrorMessage/DynamicJSONRPCErrorMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,31 @@ namespace Thunder {

namespace Plugin {


// some examples:
// use 2nd line to call a simple (static) funtion
// use 3rd line to call a more complex function (lamda, bind())
// use 4th line to have the normal errorhandling
// of course enable the correct accompanying line/part in the c'tor

class DynamicJSONRPCErrorMessage : public PluginHost::IPlugin
, public PluginHost::JSONRPCErrorAssessor
, Exchange::IMath {
, public PluginHost::JSONRPCErrorAssessor<PluginHost::JSONRPCErrorAssessorTypes::FunctionCallbackType>
// , public PluginHost::JSONRPCErrorAssessor<PluginHost::JSONRPCErrorAssessorTypes::StdFunctionCallbackType>
// , public PluginHost::JSONRPC
, public Exchange::IMath {

public:
DynamicJSONRPCErrorMessage() = default;
DynamicJSONRPCErrorMessage()
: PluginHost::IPlugin()
, PluginHost::JSONRPCErrorAssessor<PluginHost::JSONRPCErrorAssessorTypes::FunctionCallbackType>(DynamicJSONRPCErrorMessage::OnJSONRPCError)
/* , PluginHost::JSONRPCErrorAssessor<PluginHost::JSONRPCErrorAssessorTypes::StdFunctionCallbackType>([this](const Core::JSONRPC::Context& context, const string& method, const string& params, string& result) -> uint32_t
{
return OnJSONRPCErrorMethod(context, method, params, result);
})*/
// , PluginHost::JSONRPC()
, Exchange::IMath()
{
}
~DynamicJSONRPCErrorMessage() override = default;

DynamicJSONRPCErrorMessage(const DynamicJSONRPCErrorMessage&) = delete;
Expand All @@ -48,15 +67,16 @@ namespace Plugin {

// IMath overrides
uint32_t Add(const uint16_t, const uint16_t, uint16_t&) const override {
return Core::ERROR_USER_DEFINED_JSONRPC;
return Core::ERROR_NOT_SUPPORTED;
};

uint32_t Sub(const uint16_t, const uint16_t, uint16_t&) const override {
return Core::ERROR_USER_DEFINED_JSONRPC;
uint32_t Sub(const uint16_t a, const uint16_t b, uint16_t& result) const override {
result = a - b;
return Core::ERROR_NONE;
}

// JSONRPCErrorAssessor overrides
uint32_t OnJSONRPCError(const Core::JSONRPC::Context& context, const string& designator, const string& parameters, string& errormessage) override;
uint32_t OnJSONRPCErrorMethod(const Core::JSONRPC::Context& context, const string& method, const string& parameters, string& errormessage);
static uint32_t OnJSONRPCError(const Core::JSONRPC::Context& context, const string& method, const string& parameters, string& errormessage);

public:
BEGIN_INTERFACE_MAP(DynamicJSONRPCErrorMessage)
Expand Down

0 comments on commit efa2bf1

Please sign in to comment.