mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
add warning to able to forward warnings from community server to client
This commit is contained in:
parent
de269e97ea
commit
2fc3fe94a0
@ -135,6 +135,7 @@ Poco::JSON::Object* JsonCreateTransaction::transfer(Poco::Dynamic::Var params)
|
||||
else {
|
||||
printf("user hasn't valid key pair set\n");
|
||||
}
|
||||
Poco::JSON::Array* json_warnings = nullptr;
|
||||
if (!result) {
|
||||
try {
|
||||
auto transaction = model::gradido::Transaction::createTransfer(sender_user, target_pubkey, mTargetGroup, amount, mMemo, mBlockchainType);
|
||||
@ -149,6 +150,10 @@ Poco::JSON::Object* JsonCreateTransaction::transfer(Poco::Dynamic::Var params)
|
||||
if (errors.size() > 0) {
|
||||
return stateError("error by signing transaction", errors);
|
||||
}
|
||||
if (transaction->warningCount() > 0) {
|
||||
json_warnings = new Poco::JSON::Array;
|
||||
json_warnings->add(transaction->getWarningsArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Poco::Exception& ex) {
|
||||
@ -164,6 +169,10 @@ Poco::JSON::Object* JsonCreateTransaction::transfer(Poco::Dynamic::Var params)
|
||||
return stateError("exception");
|
||||
}
|
||||
result = stateSuccess();
|
||||
if (json_warnings) {
|
||||
result->set("warnings", json_warnings);
|
||||
delete json_warnings;
|
||||
}
|
||||
}
|
||||
mm->releaseMemory(target_pubkey);
|
||||
return result;
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
#include "sodium.h"
|
||||
#include "../SingletonManager/MemoryManager.h"
|
||||
#include "DataTypeConverter.h"
|
||||
#include "Warning.h"
|
||||
|
||||
JsonRequest::JsonRequest(const std::string& serverHost, int serverPort)
|
||||
: mServerHost(serverHost), mServerPort(serverPort)
|
||||
@ -125,11 +126,19 @@ JsonRequestReturn JsonRequest::request(const char* methodName, const Poco::JSON:
|
||||
return JSON_REQUEST_RETURN_ERROR;
|
||||
}
|
||||
else if (stateString == "success") {
|
||||
auto warnings_obj = object.get("warnings");
|
||||
if (!warnings_obj.isEmpty()) {
|
||||
Poco::JSON::Object warnings = *parsedJson.extract<Poco::JSON::Object::Ptr>();
|
||||
for (auto it = warnings.begin(); it != warnings.end(); it++) {
|
||||
addWarning(new Warning(it->first, it->second.toString()));
|
||||
}
|
||||
}
|
||||
for (auto it = object.begin(); it != object.end(); it++) {
|
||||
if (it->first == "state") continue;
|
||||
std::string index = it->first;
|
||||
std::string value = it->second.toString();
|
||||
printf("[JsonRequest] %s: %s\n", index.data(), value.data());
|
||||
|
||||
//printf("[JsonRequest] %s: %s\n", index.data(), value.data());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -165,95 +174,5 @@ JsonRequestReturn JsonRequest::request(const char* methodName)
|
||||
return request(methodName, requestJson);
|
||||
}
|
||||
|
||||
#include "Poco/JSON/Stringifier.h"
|
||||
JsonRequestReturn JsonRequest::requestGRPCRelay(const Poco::Net::NameValueCollection& payload)
|
||||
{
|
||||
static const char* functionName = "JsonRequest::requestGRPCRelay";
|
||||
Poco::JSON::Object requestJson;
|
||||
|
||||
for (auto it = payload.begin(); it != payload.end(); it++) {
|
||||
requestJson.set(it->first, it->second);
|
||||
}
|
||||
|
||||
// send post request via https
|
||||
// 443 = HTTPS Default
|
||||
// TODO: adding port into ServerConfig
|
||||
try {
|
||||
Profiler phpRequestTime;
|
||||
Poco::Net::HTTPClientSession httpClientSession(mServerHost, mServerPort);
|
||||
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/hedera_rpc_relay/gRPCProxy.php");
|
||||
|
||||
request.setChunkedTransferEncoding(false);
|
||||
std::ostream& requestStream = httpClientSession.sendRequest(request);
|
||||
requestJson.stringify(requestStream);
|
||||
|
||||
std::stringstream ss;
|
||||
requestJson.stringify(ss);
|
||||
auto f = fopen("grpc.txt", "wt");
|
||||
std::string grpc = ss.str();
|
||||
fwrite(grpc.data(), grpc.size(), 1, f);
|
||||
fclose(f);
|
||||
|
||||
Poco::Net::HTTPResponse response;
|
||||
std::istream& request_stream = httpClientSession.receiveResponse(response);
|
||||
|
||||
// debugging answer
|
||||
|
||||
std::stringstream responseStringStream;
|
||||
for (std::string line; std::getline(request_stream, line); ) {
|
||||
responseStringStream << line << std::endl;
|
||||
}
|
||||
Poco::Logger& speedLog = Poco::Logger::get("SpeedLog");
|
||||
speedLog.information("[gRPC relay] php server time: %s", phpRequestTime.string());
|
||||
|
||||
// extract parameter from request
|
||||
Poco::JSON::Parser jsonParser;
|
||||
Poco::Dynamic::Var parsedJson;
|
||||
try {
|
||||
parsedJson = jsonParser.parse(responseStringStream.str());
|
||||
}
|
||||
catch (Poco::Exception& ex) {
|
||||
addError(new ParamError(functionName, "error parsing request answer grpc relay", ex.displayText().data()));
|
||||
|
||||
std::string fileName = "response_grpc_";
|
||||
fileName += ".html";
|
||||
|
||||
FILE* f = fopen(fileName.data(), "wt");
|
||||
std::string responseString = responseStringStream.str();
|
||||
fwrite(responseString.data(), 1, responseString.size(), f);
|
||||
fclose(f);
|
||||
// */
|
||||
sendErrorsAsEmail(responseStringStream.str());
|
||||
return JSON_REQUEST_RETURN_PARSE_ERROR;
|
||||
}
|
||||
|
||||
Poco::JSON::Object object = *parsedJson.extract<Poco::JSON::Object::Ptr>();
|
||||
auto state = object.get("state");
|
||||
std::string stateString = state.convert<std::string>();
|
||||
if (stateString == "error") {
|
||||
addError(new Error(functionName, "php server return error"));
|
||||
if (!object.isNull("msg")) {
|
||||
addError(new ParamError(functionName, "msg:", object.get("msg").convert<std::string>().data()));
|
||||
}
|
||||
if (!object.isNull("details")) {
|
||||
addError(new ParamError(functionName, "details:", object.get("details").convert<std::string>().data()));
|
||||
}
|
||||
// send copy of errors as email, to have result also in db
|
||||
sendErrorsAsEmail("", true);
|
||||
return JSON_REQUEST_RETURN_ERROR;
|
||||
}
|
||||
ss.clear();
|
||||
Poco::JSON::Stringifier::stringify(object, ss);
|
||||
printf("json request result: %s\n", ss.str().data());
|
||||
}
|
||||
catch (Poco::Exception& e) {
|
||||
addError(new ParamError(functionName, "connect error to php server", e.displayText().data()));
|
||||
sendErrorsAsEmail();
|
||||
return JSON_REQUEST_CONNECT_ERROR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return JSON_REQUEST_RETURN_OK;
|
||||
}
|
||||
|
||||
|
||||
@ -32,7 +32,6 @@ public:
|
||||
JsonRequestReturn request(const char* methodName, const Poco::Net::NameValueCollection& payload);
|
||||
JsonRequestReturn request(const char* methodName, const Poco::JSON::Object& payload);
|
||||
JsonRequestReturn request(const char* methodName);
|
||||
JsonRequestReturn requestGRPCRelay(const Poco::Net::NameValueCollection& payload);
|
||||
|
||||
protected:
|
||||
int mServerPort;
|
||||
|
||||
@ -10,4 +10,10 @@ Notification::Notification(const char* functionName, const std::string& message)
|
||||
: mFunctionName(functionName), mMessage(message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Notification::Notification(const std::string& functionName, const std::string& message)
|
||||
: mFunctionName(functionName), mMessage(message)
|
||||
{
|
||||
|
||||
}
|
||||
@ -8,6 +8,7 @@ class Notification
|
||||
public:
|
||||
Notification(const char* functionName, const char* message);
|
||||
Notification(const char* functionName, const std::string& message);
|
||||
Notification(const std::string& functionName, const std::string& message);
|
||||
|
||||
const char* getFunctionName() { return mFunctionName.data(); }
|
||||
const char* getMessage() { return mMessage.data(); }
|
||||
@ -16,6 +17,7 @@ public:
|
||||
|
||||
virtual bool isError() { return false; }
|
||||
virtual bool isSuccess() { return false; }
|
||||
virtual bool isWarning() { return false; }
|
||||
|
||||
protected:
|
||||
std::string mFunctionName;
|
||||
|
||||
@ -54,6 +54,11 @@ NotificationList::~NotificationList()
|
||||
delete mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
}
|
||||
|
||||
while (mWarningStack.size() > 0) {
|
||||
delete mWarningStack.top();
|
||||
mWarningStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void NotificationList::addError(Notification* error, bool log/* = true */)
|
||||
@ -61,12 +66,21 @@ void NotificationList::addError(Notification* error, bool log/* = true */)
|
||||
|
||||
if (log) {
|
||||
std::string dateTimeString = Poco::DateTimeFormatter::format(Poco::DateTime(), "%d.%m.%y %H:%M:%S");
|
||||
mLogging.error("%s [ErrorList::addError] %s", dateTimeString, error->getString(false));
|
||||
mLogging.error("%s [NotificationList::addError] %s", dateTimeString, error->getString(false));
|
||||
|
||||
}
|
||||
mErrorStack.push(error);
|
||||
}
|
||||
|
||||
void NotificationList::addWarning(Warning* warning, bool log/* = true*/)
|
||||
{
|
||||
if (log) {
|
||||
std::string dateTimeString = Poco::DateTimeFormatter::format(Poco::DateTime(), "%d.%m.%y %H:%M:%S");
|
||||
mLogging.warning("%s [NotificationList::addWarning] %s", dateTimeString, warning->getString(false));
|
||||
}
|
||||
mWarningStack.push(warning);
|
||||
}
|
||||
|
||||
void NotificationList::addNotification(Notification* notification)
|
||||
{
|
||||
mErrorStack.push(notification);
|
||||
@ -86,6 +100,20 @@ Notification* NotificationList::getLastError()
|
||||
return error;
|
||||
}
|
||||
|
||||
Warning* NotificationList::getLastWarning()
|
||||
{
|
||||
if (mWarningStack.size() == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Warning* warning = mWarningStack.top();
|
||||
if (warning) {
|
||||
mWarningStack.pop();
|
||||
}
|
||||
|
||||
return warning;
|
||||
}
|
||||
|
||||
void NotificationList::clearErrors()
|
||||
{
|
||||
while (mErrorStack.size()) {
|
||||
@ -109,6 +137,17 @@ int NotificationList::getErrors(NotificationList* send)
|
||||
return iCount;
|
||||
}
|
||||
|
||||
int NotificationList::getWarnings(NotificationList* send)
|
||||
{
|
||||
Warning* warning = nullptr;
|
||||
int iCount = 0;
|
||||
while (warning = send->getLastWarning()) {
|
||||
addWarning(warning, false);
|
||||
iCount++;
|
||||
}
|
||||
return iCount;
|
||||
}
|
||||
|
||||
void NotificationList::printErrors()
|
||||
{
|
||||
while (mErrorStack.size() > 0) {
|
||||
@ -134,6 +173,21 @@ std::vector<std::string> NotificationList::getErrorsArray()
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> NotificationList::getWarningsArray()
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
result.reserve(mWarningStack.size());
|
||||
|
||||
while (mWarningStack.size() > 0) {
|
||||
auto warning = mWarningStack.top();
|
||||
mWarningStack.pop();
|
||||
//result->add(error->getString());
|
||||
result.push_back(warning->getString());
|
||||
delete warning;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string NotificationList::getErrorsHtml()
|
||||
{
|
||||
std::string res;
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
#define DR_LUA_WEB_MODULE_ERROR_ERROR_LIST_H
|
||||
|
||||
#include "Error.h"
|
||||
#include "Warning.h"
|
||||
#include <stack>
|
||||
|
||||
#include "../tasks/CPUTask.h"
|
||||
@ -28,11 +29,14 @@ public:
|
||||
// push error, error will be deleted in deconstructor
|
||||
virtual void addError(Notification* error, bool log = true);
|
||||
void addNotification(Notification* notification);
|
||||
virtual void addWarning(Warning* warning, bool log = true);
|
||||
|
||||
// return error on top of stack, please delete after using
|
||||
Notification* getLastError();
|
||||
Warning* getLastWarning();
|
||||
|
||||
inline size_t errorCount() { return mErrorStack.size(); }
|
||||
inline size_t warningCount() { return mWarningStack.size(); }
|
||||
|
||||
// delete all errors
|
||||
void clearErrors();
|
||||
@ -41,16 +45,19 @@ public:
|
||||
return recv->getErrors(send);
|
||||
}
|
||||
int getErrors(NotificationList* send);
|
||||
int getWarnings(NotificationList* send);
|
||||
|
||||
void printErrors();
|
||||
std::string getErrorsHtml();
|
||||
std::string getErrorsHtmlNewFormat();
|
||||
std::vector<std::string> getErrorsArray();
|
||||
std::vector<std::string> getWarningsArray();
|
||||
|
||||
void sendErrorsAsEmail(std::string rawHtml = "", bool copy = false);
|
||||
|
||||
protected:
|
||||
std::stack<Notification*> mErrorStack;
|
||||
std::stack<Warning*> mWarningStack;
|
||||
// poco logging
|
||||
Poco::Logger& mLogging;
|
||||
};
|
||||
|
||||
58
login_server/src/cpp/lib/Warning.cpp
Normal file
58
login_server/src/cpp/lib/Warning.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include "Warning.h"
|
||||
#include <sstream>
|
||||
|
||||
Warning::Warning(const char* functionName, const char* message)
|
||||
: Notification(functionName, message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Warning::Warning(const std::string& functionName, const std::string& message)
|
||||
: Notification(functionName, message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string Warning::getString(bool withNewline/* = true*/) const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << mFunctionName << ": " << mMessage;
|
||||
if (withNewline) ss << std::endl;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
std::string Warning::getHtmlString() const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << mFunctionName << ": " << mMessage;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
ParamWarning::ParamWarning(const char* functionName, const char* message, std::string param)
|
||||
: Warning(functionName, message), mParam(param)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ParamWarning::ParamWarning(const char* functionName, const char* message, int param)
|
||||
: Warning(functionName, message), mParam(std::to_string(param))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string ParamWarning::getString(bool withNewline/* = true*/) const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << mFunctionName << ": " << mMessage << " " << mParam;
|
||||
if (withNewline) ss << std::endl;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
std::string ParamWarning::getHtmlString() const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << mFunctionName << ": " << mMessage << " " << mParam;
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
31
login_server/src/cpp/lib/Warning.h
Normal file
31
login_server/src/cpp/lib/Warning.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef GRADIDO_LOGIN_SERVER_LIB_WARNING_H
|
||||
#define GRADIDO_LOGIN_SERVER_LIB_WARNING_H
|
||||
|
||||
#include "Notification.h"
|
||||
|
||||
class Warning : public Notification
|
||||
{
|
||||
public:
|
||||
Warning(const char* functionName, const char* message);
|
||||
Warning(const std::string& functionName, const std::string& message);
|
||||
|
||||
std::string getString(bool withNewline = true) const;
|
||||
std::string getHtmlString() const;
|
||||
|
||||
virtual bool isWarning() { return true; }
|
||||
};
|
||||
|
||||
class ParamWarning : public Warning
|
||||
{
|
||||
public:
|
||||
ParamWarning(const char* functionName, const char* message, std::string param);
|
||||
ParamWarning(const char* functionName, const char* message, int param);
|
||||
|
||||
std::string getString(bool withNewline = true) const;
|
||||
std::string getHtmlString() const;
|
||||
|
||||
protected:
|
||||
std::string mParam;
|
||||
};
|
||||
|
||||
#endif //GRADIDO_LOGIN_SERVER_LIB_WARNING_H
|
||||
@ -561,17 +561,21 @@ namespace model {
|
||||
Poco::Net::NameValueCollection param;
|
||||
param.set("transaction", base_64_message);
|
||||
auto result = json_request.request("putTransaction", param);
|
||||
if (JSON_REQUEST_RETURN_OK == result) {
|
||||
json_request.getWarnings(&json_request);
|
||||
|
||||
if (JSON_REQUEST_RETURN_OK == result)
|
||||
{
|
||||
if (!json_request.errorCount()) {
|
||||
finishSuccess();
|
||||
}
|
||||
else {
|
||||
getErrors(&json_request);
|
||||
getErrors(&json_request);
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
json_request.getWarnings(&json_request);
|
||||
getErrors(&json_request);
|
||||
|
||||
return -1;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user