Skip to content

Commit

Permalink
[JSONRPC] Dynamic error messages (#1782)
Browse files Browse the repository at this point in the history
* [JSONRPC] Dynamic error messages

* [JSONRPC] improved dynamic error handling

* [JSONRPC] improved dynamic error support

* [JSONRPC] can of course be const

* [JSONRPC] add errorcode to dynamic error message callback
  • Loading branch information
MFransen69 authored Oct 25, 2024
1 parent d3a7a28 commit 5c6afdf
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ option(EXCEPTION_CATCHING
"Enable unhandled exception handling catching." OFF)
option(DEADLOCK_DETECTION
"Enable deadlock detection tooling." OFF)
option(DISABLE_USE_COMPLEMENTARY_CODE_SET
"Disable the complementary code set" OFF)


if(HIDE_NON_EXTERNAL_SYMBOLS)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
Expand Down Expand Up @@ -84,6 +87,11 @@ else()
endif()
endif()

if(DISABLE_USE_COMPLEMENTARY_CODE_SET)
add_definitions(-D__DISABLE_USE_COMPLEMENTARY_CODE_SET__)
message(STATUS "complementary code set disabled")
endif()

if(CORE)
add_subdirectory(core)
endif()
Expand Down
6 changes: 6 additions & 0 deletions Source/core/JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace Core {
/* static */ char IElement::TrueTag[5] = { 't', 'r', 'u', 'e', '\0' };
/* static */ char IElement::FalseTag[6] = { 'f', 'a', 'l', 's', 'e', '\0' };

#ifndef __DISABLE_USE_COMPLEMENTARY_CODE_SET__

string Variant::GetDebugString(const TCHAR name[], int indent, int arrayIndex) const
{
std::stringstream ss;
Expand Down Expand Up @@ -78,6 +80,10 @@ namespace Core {
ss << iterator.Current().GetDebugString(iterator.Label(), indent);
return ss.str();
}

#endif // __DISABLE_USE_COMPLEMENTARY_CODE_SET__


}
}

Expand Down
8 changes: 8 additions & 0 deletions Source/core/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -4192,6 +4192,8 @@ namespace Core {
mutable String _fieldName;
};

#ifndef __DISABLE_USE_COMPLEMENTARY_CODE_SET__

class VariantContainer;

class EXTERNAL Variant : public JSON::String {
Expand Down Expand Up @@ -5089,12 +5091,18 @@ namespace Core {
}
};

#endif // __DISABLE_USE_COMPLEMENTARY_CODE_SET__

} // namespace JSON
} // namespace Core
} // namespace Thunder

#ifndef __DISABLE_USE_COMPLEMENTARY_CODE_SET__

using JsonObject = Thunder::Core::JSON::VariantContainer;
using JsonValue = Thunder::Core::JSON::Variant;
using JsonArray = Thunder::Core::JSON::ArrayType<JsonValue>;

#endif // __DISABLE_USE_COMPLEMENTARY_CODE_SET__

#endif // __JSON_H
119 changes: 112 additions & 7 deletions Source/plugins/JSONRPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ namespace Thunder {

namespace PluginHost {

namespace {

template<typename JSONRPCERRORASSESSORTYPE>
uint32_t InvokeOnHandler(const Core::JSONRPC::Context& context, const string& method, const string& parameters, string& response, Core::JSONRPC::Handler& handler, JSONRPCERRORASSESSORTYPE errorhandler)
{
uint32_t result = handler.Invoke(context, method, parameters, response);
if(result != Core::ERROR_NONE) {
result = errorhandler(context, method, parameters, result, response);
}

return result;
}

template<>
uint32_t InvokeOnHandler<void*>(const Core::JSONRPC::Context& context, const string& method, const string& parameters, string& response, Core::JSONRPC::Handler& handler, void*)
{
return handler.Invoke(context, method, parameters, response);
}

}

class EXTERNAL JSONRPC : public IDispatcher {
public:
using SendIfMethod = std::function<bool(const string&)>;
Expand Down Expand Up @@ -448,6 +469,7 @@ namespace PluginHost {
return (result);
}


//
// Register/Unregister methods for incoming method handling on the "base" handler elements.
// ------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -456,11 +478,17 @@ namespace PluginHost {
{
_handlers.front().Property<PARAMETER>(methodName, getter, setter, objectPtr);
}

#ifndef __DISABLE_USE_COMPLEMENTARY_CODE_SET__

template <typename METHOD, typename REALOBJECT>
void Register(const string& methodName, const METHOD& method, REALOBJECT* objectPtr)
{
_handlers.front().Register<Core::JSON::VariantContainer, Core::JSON::VariantContainer, METHOD, REALOBJECT>(methodName, method, objectPtr);
}

#endif // __DISABLE_USE_COMPLEMENTARY_CODE_SET__

template <typename INBOUND, typename OUTBOUND, typename METHOD, typename REALOBJECT>
void Register(const string& methodName, const METHOD& method, REALOBJECT* objectPtr)
{
Expand Down Expand Up @@ -587,7 +615,13 @@ namespace PluginHost {

// Inherited via IDispatcher
// ---------------------------------------------------------------------------------
uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override {
uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override
{
return InvokeHandler(channelId, id, token, method, parameters, response);
}

template<typename JSONRPCERRORASSESSORTYPE = void*>
uint32_t InvokeHandler(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response, JSONRPCERRORASSESSORTYPE errorhandler = nullptr) {
uint32_t result = Core::ERROR_PARSE_FAILURE;

if (method.empty() == false) {
Expand Down Expand Up @@ -649,20 +683,20 @@ namespace PluginHost {
}
else {
Core::JSONRPC::Handler* handler(Handler(realMethod));

if (handler == nullptr) {
result = Core::ERROR_INCORRECT_URL;
}
else {

if (handler != nullptr) {
Core::JSONRPC::Context context(channelId, id, token);
result = handler->Invoke(context, Core::JSONRPC::Message::FullMethod(method), parameters, response);
result = InvokeOnHandler<JSONRPCERRORASSESSORTYPE>(context, Core::JSONRPC::Message::FullMethod(method), parameters, response, *handler, errorhandler);
} else {
result = Core::ERROR_INCORRECT_URL;
}
}
}
}

return (result);
}

Core::hresult Subscribe(ICallback* callback, const string& eventId, const string& designator) override
{
uint32_t result;
Expand Down Expand Up @@ -1002,5 +1036,76 @@ namespace PluginHost {
StatusCallbackMap _observers;
};

#ifndef __DISABLE_USE_COMPLEMENTARY_CODE_SET__

namespace JSONRPCErrorAssessorTypes {

using FunctionCallbackType = uint32_t (*) (const Core::JSONRPC::Context&, const string&, const string&, const uint32_t errorcode, string&);
using StdFunctionCallbackType = std::function<int32_t(const Core::JSONRPC::Context&, const string&, const string&, const uint32_t errorcode, string&)>;
}

template<typename JSONRPCERRORASSESSORTYPE>
class EXTERNAL JSONRPCErrorAssessor : public JSONRPC {
public:

JSONRPCErrorAssessor(JSONRPCERRORASSESSORTYPE errorhandler)
: JSONRPC()
, _errorhandler(errorhandler)
{
}

~JSONRPCErrorAssessor() override = default;

JSONRPCErrorAssessor(const JSONRPCErrorAssessor&) = delete;
JSONRPCErrorAssessor &operator=(const JSONRPCErrorAssessor&) = delete;
JSONRPCErrorAssessor(JSONRPCErrorAssessor&&) = delete;
JSONRPCErrorAssessor &operator=(JSONRPCErrorAssessor&&) = delete;

uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override
{
return JSONRPC::InvokeHandler<JSONRPCERRORASSESSORTYPE>(channelId, id, token, method, parameters, response, _errorhandler);
}


private:
JSONRPCERRORASSESSORTYPE _errorhandler;
};

template<>
class EXTERNAL JSONRPCErrorAssessor<JSONRPCErrorAssessorTypes::StdFunctionCallbackType> : public JSONRPC {
public:

JSONRPCErrorAssessor(const JSONRPCErrorAssessorTypes::StdFunctionCallbackType& errorhandler)
: JSONRPC()
, _errorhandler(errorhandler)
{
}

JSONRPCErrorAssessor(JSONRPCErrorAssessorTypes::StdFunctionCallbackType&& errorhandler)
: JSONRPC()
, _errorhandler(std::move(errorhandler))
{
}

~JSONRPCErrorAssessor() override = default;

JSONRPCErrorAssessor(const JSONRPCErrorAssessor&) = delete;
JSONRPCErrorAssessor &operator=(const JSONRPCErrorAssessor&) = delete;
JSONRPCErrorAssessor(JSONRPCErrorAssessor&&) = delete;
JSONRPCErrorAssessor &operator=(JSONRPCErrorAssessor&&) = delete;

uint32_t Invoke(const uint32_t channelId, const uint32_t id, const string& token, const string& method, const string& parameters, string& response) override
{
return JSONRPC::InvokeHandler<const JSONRPCErrorAssessorTypes::StdFunctionCallbackType&>(channelId, id, token, method, parameters, response, _errorhandler);
}


private:
JSONRPCErrorAssessorTypes::StdFunctionCallbackType _errorhandler;
};

#endif // __DISABLE_USE_COMPLEMENTARY_CODE_SET__

} // namespace Thunder::PluginHost
}

10 changes: 10 additions & 0 deletions Source/websocket/JSONRPCLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,8 @@ namespace Thunder {
return (Send(waitTime, method, parameters, response));
}

#ifndef __DISABLE_USE_COMPLEMENTARY_CODE_SET__

// Generic JSONRPC methods.
// Anything goes!
// these objects have no type chacking, will consume more memory and processing takes more time
Expand Down Expand Up @@ -784,6 +786,8 @@ namespace Thunder {
return InternalInvoke<PARAMETERS>(waitTime, method, parameters, inbound);
}

#endif // __DISABLE_USE_COMPLEMENTARY_CODE_SET__

private:
friend CommunicationChannel;

Expand Down Expand Up @@ -1452,6 +1456,8 @@ namespace Thunder {
return (_connection.Invoke(waitTime, method, parameters, response));
}

#ifndef __DISABLE_USE_COMPLEMENTARY_CODE_SET__

// Opaque JSON structure methods.
// Anything goes!
// ===================================================================================
Expand All @@ -1467,6 +1473,10 @@ namespace Thunder {
{
return (_connection.template Get<Core::JSON::VariantContainer>(waitTime, method, object));
}

#endif // __DISABLE_USE_COMPLEMENTARY_CODE_SET__


bool IsActivated()
{
return (_connection.IsActivated());
Expand Down
5 changes: 5 additions & 0 deletions cmake/project.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ if(VERSIONED_LIBRARY_LOADING)
add_definitions(-DVERSIONED_LIBRARY_LOADING)
endif()

set(DISABLE_USE_COMPLEMENTARY_CODE_SET @DISABLE_USE_COMPLEMENTARY_CODE_SET@)
if(DISABLE_USE_COMPLEMENTARY_CODE_SET)
add_definitions(-D__DISABLE_USE_COMPLEMENTARY_CODE_SET__)
endif()

set(CMAKE_BUILD_TYPE @CMAKE_BUILD_TYPE@ CACHE INTERNAL "" FORCE)

# FIX_ME: Disable fortify source.
Expand Down

0 comments on commit 5c6afdf

Please sign in to comment.