mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
add option to get errors as payload for json request return
This commit is contained in:
parent
483d3dc444
commit
12b6a2628c
@ -1,61 +1,63 @@
|
||||
/*!
|
||||
*
|
||||
* \author: einhornimmond
|
||||
*
|
||||
* \date: 07.03.19
|
||||
*
|
||||
* \brief: error data
|
||||
*/
|
||||
|
||||
#ifndef DR_LUA_WEB_MODULE_ERROR_ERROR_H
|
||||
#define DR_LUA_WEB_MODULE_ERROR_ERROR_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
Error(const char* functionName, const char* message);
|
||||
~Error();
|
||||
|
||||
const char* getFunctionName() { return mFunctionName.data(); }
|
||||
const char* getMessage() { return mMessage.data(); }
|
||||
virtual std::string getString(bool withNewline = true);
|
||||
virtual std::string getHtmlString();
|
||||
|
||||
protected:
|
||||
std::string mFunctionName;
|
||||
std::string mMessage;
|
||||
};
|
||||
|
||||
class ParamError : public Error
|
||||
{
|
||||
public:
|
||||
ParamError(const char* functionName, const char* message, const char* param)
|
||||
: Error(functionName, message), mParam(param) {}
|
||||
ParamError(const char* functionName, const char* message, const std::string& param)
|
||||
: Error(functionName, message), mParam(param) {}
|
||||
|
||||
ParamError(const char* functioName, const char* message, int param)
|
||||
: Error(functioName, message) {
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
mParam = ss.str();
|
||||
}
|
||||
|
||||
virtual std::string getString(bool withNewline = true);
|
||||
virtual std::string getHtmlString();
|
||||
protected:
|
||||
std::string mParam;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class IErrorCollection
|
||||
{
|
||||
public:
|
||||
virtual void addError(Error*, bool log = true) = 0;
|
||||
};
|
||||
|
||||
#endif // DR_LUA_WEB_MODULE_ERROR_ERROR_H
|
||||
/*!
|
||||
*
|
||||
* \author: einhornimmond
|
||||
*
|
||||
* \date: 07.03.19
|
||||
*
|
||||
* \brief: error data
|
||||
*/
|
||||
|
||||
#ifndef DR_LUA_WEB_MODULE_ERROR_ERROR_H
|
||||
#define DR_LUA_WEB_MODULE_ERROR_ERROR_H
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
class Error
|
||||
{
|
||||
public:
|
||||
Error(const char* functionName, const char* message);
|
||||
~Error();
|
||||
|
||||
const char* getFunctionName() { return mFunctionName.data(); }
|
||||
const char* getMessage() { return mMessage.data(); }
|
||||
virtual std::string getString(bool withNewline = true);
|
||||
virtual std::string getHtmlString();
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
std::string mFunctionName;
|
||||
std::string mMessage;
|
||||
};
|
||||
|
||||
class ParamError : public Error
|
||||
{
|
||||
public:
|
||||
ParamError(const char* functionName, const char* message, const char* param)
|
||||
: Error(functionName, message), mParam(param) {}
|
||||
ParamError(const char* functionName, const char* message, const std::string& param)
|
||||
: Error(functionName, message), mParam(param) {}
|
||||
|
||||
ParamError(const char* functioName, const char* message, int param)
|
||||
: Error(functioName, message) {
|
||||
std::stringstream ss;
|
||||
ss << param;
|
||||
mParam = ss.str();
|
||||
}
|
||||
|
||||
virtual std::string getString(bool withNewline = true);
|
||||
virtual std::string getHtmlString();
|
||||
protected:
|
||||
std::string mParam;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class IErrorCollection
|
||||
{
|
||||
public:
|
||||
virtual void addError(Error*, bool log = true) = 0;
|
||||
};
|
||||
|
||||
#endif // DR_LUA_WEB_MODULE_ERROR_ERROR_H
|
||||
|
||||
@ -1,186 +1,201 @@
|
||||
#include "ErrorList.h"
|
||||
|
||||
#include "../ServerConfig.h"
|
||||
|
||||
//#include "Poco/Net/MailMessage.h"
|
||||
#include "Poco/Net/MediaType.h"
|
||||
|
||||
#include "../SingletonManager/EmailManager.h"
|
||||
|
||||
SendErrorMessage::~SendErrorMessage()
|
||||
{
|
||||
if (mMessage) {
|
||||
delete mMessage;
|
||||
mMessage = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
int SendErrorMessage::run()
|
||||
{
|
||||
if (ServerConfig::g_disableEmail) return 0;
|
||||
|
||||
auto mailClientSession = new Poco::Net::SecureSMTPClientSession(ServerConfig::g_EmailAccount.url, ServerConfig::g_EmailAccount.port);
|
||||
mailClientSession->login();
|
||||
mailClientSession->startTLS(ServerConfig::g_SSL_CLient_Context);
|
||||
|
||||
|
||||
mailClientSession->login(Poco::Net::SMTPClientSession::AUTH_LOGIN, ServerConfig::g_EmailAccount.username, ServerConfig::g_EmailAccount.password);
|
||||
|
||||
try {
|
||||
mMessage->setSender(ServerConfig::g_EmailAccount.sender);
|
||||
mailClientSession->sendMessage(*mMessage);
|
||||
mailClientSession->close();
|
||||
}
|
||||
catch (Poco::Exception& exc) {
|
||||
printf("[SendErrorMessage::%s] error sending error message to admin: %s\n",
|
||||
__FUNCTION__, exc.displayText().data());
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorList::ErrorList()
|
||||
: mLogging(Poco::Logger::get("errorLog"))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ErrorList::~ErrorList()
|
||||
{
|
||||
while (mErrorStack.size() > 0) {
|
||||
delete mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void ErrorList::addError(Error* 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));
|
||||
|
||||
}
|
||||
mErrorStack.push(error);
|
||||
}
|
||||
|
||||
Error* ErrorList::getLastError()
|
||||
{
|
||||
if (mErrorStack.size() == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Error* error = mErrorStack.top();
|
||||
if (error) {
|
||||
mErrorStack.pop();
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void ErrorList::clearErrors()
|
||||
{
|
||||
while (mErrorStack.size()) {
|
||||
auto error = mErrorStack.top();
|
||||
if (error) {
|
||||
delete error;
|
||||
}
|
||||
mErrorStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int ErrorList::getErrors(ErrorList* send)
|
||||
{
|
||||
Error* error = nullptr;
|
||||
int iCount = 0;
|
||||
while (error = send->getLastError()) {
|
||||
addError(error, false);
|
||||
iCount++;
|
||||
}
|
||||
return iCount;
|
||||
}
|
||||
|
||||
void ErrorList::printErrors()
|
||||
{
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
printf(error->getString().data());
|
||||
delete error;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ErrorList::getErrorsHtml()
|
||||
{
|
||||
std::string res;
|
||||
res = "<ul class='grd-no-style'>";
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
res += "<li class='grd-error'>";
|
||||
res += error->getHtmlString();
|
||||
res += "</li>";
|
||||
delete error;
|
||||
}
|
||||
res += "</ul>";
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string ErrorList::getErrorsHtmlNewFormat()
|
||||
{
|
||||
std::string html;
|
||||
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = std::unique_ptr<Error>(mErrorStack.top());
|
||||
mErrorStack.pop();
|
||||
html += "<div class=\"alert alert-error\" role=\"alert\">";
|
||||
html += "<i class=\"material-icons-outlined\">report_problem</i>";
|
||||
html += "<span>";
|
||||
html += error->getHtmlString();
|
||||
html += "</span>";
|
||||
html += "</div>";
|
||||
}
|
||||
return html;
|
||||
}
|
||||
/*
|
||||
<div class="alert alert-error" role="alert">
|
||||
<i class="material-icons-outlined">report_problem</i>
|
||||
<span>Der Empfänger wurde nicht auf dem Login-Server gefunden, hat er sein Konto schon angelegt?</span>
|
||||
</div>
|
||||
*/
|
||||
|
||||
|
||||
void ErrorList::sendErrorsAsEmail(std::string rawHtml/* = ""*/)
|
||||
{
|
||||
auto em = EmailManager::getInstance();
|
||||
/*auto message = new Poco::Net::MailMessage();
|
||||
message->setSender("gradido_loginServer@gradido.net");
|
||||
message->addRecipient(Poco::Net::MailRecipient(Poco::Net::MailRecipient::PRIMARY_RECIPIENT, "***REMOVED***"));
|
||||
message->setSubject("Error from Gradido Login Server");
|
||||
*/
|
||||
std::string content;
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
content += error->getString();
|
||||
delete error;
|
||||
}
|
||||
auto email = new model::Email(content, model::EMAIL_ERROR);
|
||||
|
||||
//message->addContent(new Poco::Net::StringPartSource(content));
|
||||
if (rawHtml != "") {
|
||||
Poco::Net::MediaType mt("text", "html");
|
||||
mt.setParameter("charset", "utf-8");
|
||||
|
||||
email->addContent(new Poco::Net::StringPartSource(rawHtml, mt.toString()));
|
||||
}
|
||||
em->addEmail(email);
|
||||
|
||||
//UniLib::controller::TaskPtr sendErrorMessageTask(new SendErrorMessage(message, ServerConfig::g_CPUScheduler));
|
||||
//sendErrorMessageTask->scheduleTask(sendErrorMessageTask);
|
||||
|
||||
#include "ErrorList.h"
|
||||
|
||||
#include "../ServerConfig.h"
|
||||
|
||||
//#include "Poco/Net/MailMessage.h"
|
||||
#include "Poco/Net/MediaType.h"
|
||||
|
||||
#include "../SingletonManager/EmailManager.h"
|
||||
|
||||
SendErrorMessage::~SendErrorMessage()
|
||||
{
|
||||
if (mMessage) {
|
||||
delete mMessage;
|
||||
mMessage = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
int SendErrorMessage::run()
|
||||
{
|
||||
if (ServerConfig::g_disableEmail) return 0;
|
||||
|
||||
auto mailClientSession = new Poco::Net::SecureSMTPClientSession(ServerConfig::g_EmailAccount.url, ServerConfig::g_EmailAccount.port);
|
||||
mailClientSession->login();
|
||||
mailClientSession->startTLS(ServerConfig::g_SSL_CLient_Context);
|
||||
|
||||
|
||||
mailClientSession->login(Poco::Net::SMTPClientSession::AUTH_LOGIN, ServerConfig::g_EmailAccount.username, ServerConfig::g_EmailAccount.password);
|
||||
|
||||
try {
|
||||
mMessage->setSender(ServerConfig::g_EmailAccount.sender);
|
||||
mailClientSession->sendMessage(*mMessage);
|
||||
mailClientSession->close();
|
||||
}
|
||||
catch (Poco::Exception& exc) {
|
||||
printf("[SendErrorMessage::%s] error sending error message to admin: %s\n",
|
||||
__FUNCTION__, exc.displayText().data());
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorList::ErrorList()
|
||||
: mLogging(Poco::Logger::get("errorLog"))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ErrorList::~ErrorList()
|
||||
{
|
||||
while (mErrorStack.size() > 0) {
|
||||
delete mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void ErrorList::addError(Error* 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));
|
||||
|
||||
}
|
||||
mErrorStack.push(error);
|
||||
}
|
||||
|
||||
Error* ErrorList::getLastError()
|
||||
{
|
||||
if (mErrorStack.size() == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Error* error = mErrorStack.top();
|
||||
if (error) {
|
||||
mErrorStack.pop();
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void ErrorList::clearErrors()
|
||||
{
|
||||
while (mErrorStack.size()) {
|
||||
auto error = mErrorStack.top();
|
||||
if (error) {
|
||||
delete error;
|
||||
}
|
||||
mErrorStack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int ErrorList::getErrors(ErrorList* send)
|
||||
{
|
||||
Error* error = nullptr;
|
||||
int iCount = 0;
|
||||
while (error = send->getLastError()) {
|
||||
addError(error, false);
|
||||
iCount++;
|
||||
}
|
||||
return iCount;
|
||||
}
|
||||
|
||||
void ErrorList::printErrors()
|
||||
{
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
printf(error->getString().data());
|
||||
delete error;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> ErrorList::getErrorsArray()
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
result.reserve(mErrorStack.size());
|
||||
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
//result->add(error->getString());
|
||||
result.push_back(error->getString());
|
||||
delete error;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string ErrorList::getErrorsHtml()
|
||||
{
|
||||
std::string res;
|
||||
res = "<ul class='grd-no-style'>";
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
res += "<li class='grd-error'>";
|
||||
res += error->getHtmlString();
|
||||
res += "</li>";
|
||||
delete error;
|
||||
}
|
||||
res += "</ul>";
|
||||
return res;
|
||||
}
|
||||
|
||||
std::string ErrorList::getErrorsHtmlNewFormat()
|
||||
{
|
||||
std::string html;
|
||||
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = std::unique_ptr<Error>(mErrorStack.top());
|
||||
mErrorStack.pop();
|
||||
html += "<div class=\"alert alert-error\" role=\"alert\">";
|
||||
html += "<i class=\"material-icons-outlined\">report_problem</i>";
|
||||
html += "<span>";
|
||||
html += error->getHtmlString();
|
||||
html += "</span>";
|
||||
html += "</div>";
|
||||
}
|
||||
return html;
|
||||
}
|
||||
/*
|
||||
<div class="alert alert-error" role="alert">
|
||||
<i class="material-icons-outlined">report_problem</i>
|
||||
<span>Der Empfänger wurde nicht auf dem Login-Server gefunden, hat er sein Konto schon angelegt?</span>
|
||||
</div>
|
||||
*/
|
||||
|
||||
|
||||
void ErrorList::sendErrorsAsEmail(std::string rawHtml/* = ""*/)
|
||||
{
|
||||
auto em = EmailManager::getInstance();
|
||||
/*auto message = new Poco::Net::MailMessage();
|
||||
message->setSender("gradido_loginServer@gradido.net");
|
||||
message->addRecipient(Poco::Net::MailRecipient(Poco::Net::MailRecipient::PRIMARY_RECIPIENT, "***REMOVED***"));
|
||||
message->setSubject("Error from Gradido Login Server");
|
||||
*/
|
||||
std::string content;
|
||||
while (mErrorStack.size() > 0) {
|
||||
auto error = mErrorStack.top();
|
||||
mErrorStack.pop();
|
||||
content += error->getString();
|
||||
delete error;
|
||||
}
|
||||
auto email = new model::Email(content, model::EMAIL_ERROR);
|
||||
|
||||
//message->addContent(new Poco::Net::StringPartSource(content));
|
||||
if (rawHtml != "") {
|
||||
Poco::Net::MediaType mt("text", "html");
|
||||
mt.setParameter("charset", "utf-8");
|
||||
|
||||
email->addContent(new Poco::Net::StringPartSource(rawHtml, mt.toString()));
|
||||
}
|
||||
em->addEmail(email);
|
||||
|
||||
//UniLib::controller::TaskPtr sendErrorMessageTask(new SendErrorMessage(message, ServerConfig::g_CPUScheduler));
|
||||
//sendErrorMessageTask->scheduleTask(sendErrorMessageTask);
|
||||
|
||||
}
|
||||
@ -1,73 +1,76 @@
|
||||
/*!
|
||||
*
|
||||
* \author: einhornimmond
|
||||
*
|
||||
* \date: 07.03.19
|
||||
*
|
||||
* \brief: error
|
||||
*/
|
||||
|
||||
#ifndef DR_LUA_WEB_MODULE_ERROR_ERROR_LIST_H
|
||||
#define DR_LUA_WEB_MODULE_ERROR_ERROR_LIST_H
|
||||
|
||||
#include "Error.h"
|
||||
#include <stack>
|
||||
|
||||
#include "../tasks/CPUTask.h"
|
||||
|
||||
#include "Poco/Net/SecureSMTPClientSession.h"
|
||||
#include "Poco/Net/StringPartSource.h"
|
||||
#include "Poco/Logger.h"
|
||||
|
||||
class ErrorList : public IErrorCollection
|
||||
{
|
||||
public:
|
||||
ErrorList();
|
||||
~ErrorList();
|
||||
|
||||
// push error, error will be deleted in deconstructor
|
||||
virtual void addError(Error* error, bool log = true);
|
||||
|
||||
// return error on top of stack, please delete after using
|
||||
Error* getLastError();
|
||||
|
||||
inline size_t errorCount() { return mErrorStack.size(); }
|
||||
|
||||
// delete all errors
|
||||
void clearErrors();
|
||||
|
||||
static int moveErrors(ErrorList* recv, ErrorList* send) {
|
||||
return recv->getErrors(send);
|
||||
}
|
||||
int getErrors(ErrorList* send);
|
||||
|
||||
void printErrors();
|
||||
std::string getErrorsHtml();
|
||||
std::string getErrorsHtmlNewFormat();
|
||||
|
||||
void sendErrorsAsEmail(std::string rawHtml = "");
|
||||
|
||||
protected:
|
||||
std::stack<Error*> mErrorStack;
|
||||
// poco logging
|
||||
Poco::Logger& mLogging;
|
||||
};
|
||||
|
||||
class SendErrorMessage : public UniLib::controller::CPUTask
|
||||
{
|
||||
public:
|
||||
SendErrorMessage(Poco::Net::MailMessage* message, UniLib::controller::CPUSheduler* scheduler)
|
||||
: UniLib::controller::CPUTask(scheduler), mMessage(message) {}
|
||||
|
||||
~SendErrorMessage();
|
||||
|
||||
virtual int run();
|
||||
const char* getResourceType() const { return "SendErrorMessage"; };
|
||||
|
||||
|
||||
protected:
|
||||
Poco::Net::MailMessage* mMessage;
|
||||
|
||||
};
|
||||
|
||||
#endif // DR_LUA_WEB_MODULE_ERROR_ERROR_LIST_H
|
||||
/*!
|
||||
*
|
||||
* \author: einhornimmond
|
||||
*
|
||||
* \date: 07.03.19
|
||||
*
|
||||
* \brief: error
|
||||
*/
|
||||
|
||||
#ifndef DR_LUA_WEB_MODULE_ERROR_ERROR_LIST_H
|
||||
#define DR_LUA_WEB_MODULE_ERROR_ERROR_LIST_H
|
||||
|
||||
#include "Error.h"
|
||||
#include <stack>
|
||||
|
||||
#include "../tasks/CPUTask.h"
|
||||
|
||||
#include "Poco/Net/SecureSMTPClientSession.h"
|
||||
#include "Poco/Net/StringPartSource.h"
|
||||
#include "Poco/Logger.h"
|
||||
#include "Poco/JSON/Array.h"
|
||||
|
||||
class ErrorList : public IErrorCollection
|
||||
{
|
||||
public:
|
||||
ErrorList();
|
||||
~ErrorList();
|
||||
|
||||
// push error, error will be deleted in deconstructor
|
||||
virtual void addError(Error* error, bool log = true);
|
||||
|
||||
// return error on top of stack, please delete after using
|
||||
Error* getLastError();
|
||||
|
||||
inline size_t errorCount() { return mErrorStack.size(); }
|
||||
|
||||
// delete all errors
|
||||
void clearErrors();
|
||||
|
||||
static int moveErrors(ErrorList* recv, ErrorList* send) {
|
||||
return recv->getErrors(send);
|
||||
}
|
||||
int getErrors(ErrorList* send);
|
||||
|
||||
void printErrors();
|
||||
std::string getErrorsHtml();
|
||||
std::string getErrorsHtmlNewFormat();
|
||||
|
||||
std::vector<std::string> getErrorsArray();
|
||||
|
||||
void sendErrorsAsEmail(std::string rawHtml = "");
|
||||
|
||||
protected:
|
||||
std::stack<Error*> mErrorStack;
|
||||
// poco logging
|
||||
Poco::Logger& mLogging;
|
||||
};
|
||||
|
||||
class SendErrorMessage : public UniLib::controller::CPUTask
|
||||
{
|
||||
public:
|
||||
SendErrorMessage(Poco::Net::MailMessage* message, UniLib::controller::CPUSheduler* scheduler)
|
||||
: UniLib::controller::CPUTask(scheduler), mMessage(message) {}
|
||||
|
||||
~SendErrorMessage();
|
||||
|
||||
virtual int run();
|
||||
const char* getResourceType() const { return "SendErrorMessage"; };
|
||||
|
||||
|
||||
protected:
|
||||
Poco::Net::MailMessage* mMessage;
|
||||
|
||||
};
|
||||
|
||||
#endif // DR_LUA_WEB_MODULE_ERROR_ERROR_LIST_H
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user