added the errorcodes namespace

This commit is contained in:
Andrei Yankovich 2020-10-29 20:23:50 +03:00
parent 5c794703ad
commit aa4b51ca8d
14 changed files with 243 additions and 40 deletions

View File

@ -0,0 +1,25 @@
#ifndef AbstractERRORCODES_H
#define AbstractERRORCODES_H
namespace QH {
/**
* @brief ErrorCodes This namesapce contains all error codes of the Heart Library.
*/
namespace ErrorCodes {
/**
* @brief The AbstractErrorCodes enum This enum with dafault error codes.
*/
enum AbstractErrorCodes: unsigned char {
/// This is unknown error. Default value.
UnknownError = 0,
/// The sendet to remoute nodes request is invalid. This is default value.
InvalidRequest,
/// This case using for inheritance new enum classes.
AbstractErrorCodes
};
}
}
#endif // ERRORCODES_H

View File

@ -407,7 +407,7 @@ ParserResult AbstractNode::parsePackage(const Package &pkg,
BadRequest cmd(pkg); BadRequest cmd(pkg);
incomingData(&cmd, sender->networkAddress()); incomingData(&cmd, sender->networkAddress());
emit requestError(cmd.err()); emit requestError(cmd.errCode(), cmd.err());
return ParserResult::Processed; return ParserResult::Processed;
@ -497,7 +497,7 @@ bool AbstractNode::sendData(const AbstractData *resp,
} }
void AbstractNode::badRequest(const HostAddress &address, const Header &req, void AbstractNode::badRequest(const HostAddress &address, const Header &req,
const QString msg, quint8 diff) { const ErrorData &err, quint8 diff) {
auto client = getInfoPtr(address); auto client = getInfoPtr(address);
if (!client) { if (!client) {
@ -517,7 +517,7 @@ void AbstractNode::badRequest(const HostAddress &address, const Header &req,
return; return;
} }
auto bad = BadRequest(msg); auto bad = BadRequest(err);
if (!sendData(&bad, address, &req)) { if (!sendData(&bad, address, &req)) {
return; return;
} }

View File

@ -24,6 +24,7 @@
#include "icrypto.h" #include "icrypto.h"
#include "heart_global.h" #include "heart_global.h"
#include "packagemanager.h" #include "packagemanager.h"
#include "abstracterrorcodes.h"
class QSslCertificate; class QSslCertificate;
class QSslKey; class QSslKey;
@ -34,6 +35,10 @@ namespace QH {
class DataSender; class DataSender;
class ReceiveData; class ReceiveData;
namespace PKG {
class ErrorData;
}
/** /**
* @brief The ParserResult enum * @brief The ParserResult enum
* Error - parser detect a errorob package * Error - parser detect a errorob package
@ -221,9 +226,10 @@ public:
signals: signals:
/** /**
* @brief requestError This signal emited when client or node received from remoute server or node the BadRequest package. * @brief requestError This signal emited when client or node received from remoute server or node the BadRequest package.
* @param code This is code of error.
* @param msg - received text of remoute node (server). * @param msg - received text of remoute node (server).
*/ */
void requestError(QString msg); void requestError(unsigned char code, const QString& msg);
protected: protected:
@ -341,12 +347,12 @@ protected:
* @brief badRequest This method is send data about error of request * @brief badRequest This method is send data about error of request
* @param address This is addrees of receiver * @param address This is addrees of receiver
* @param req This is header of incomming request * @param req This is header of incomming request
* @param msg This is message of error * @param err This is message and code of error. For more information see the ErrorData struct.
* @param diff This is difference of current trust (currenTrus += diff) * @param diff This is difference of current trust (currenTrus += diff)
* By default diff equals REQUEST_ERROR * By default diff equals REQUEST_ERROR
*/ */
virtual void badRequest(const HostAddress &address, const Header &req, virtual void badRequest(const HostAddress &address, const Header &req,
const QString msg = "", quint8 diff = REQUEST_ERROR); const PKG::ErrorData& err, quint8 diff = REQUEST_ERROR);
/** /**
* @brief getWorkStateString This method generate string about work state of server. * @brief getWorkStateString This method generate string about work state of server.

View File

@ -13,11 +13,13 @@ namespace QH{
namespace PKG { namespace PKG {
BadRequest::BadRequest(const QString &err):AbstractData() { BadRequest::BadRequest(unsigned char errocode, const QString &err) {
setErr(err); setErr(err);
setErrCode(errocode);
}
BadRequest::BadRequest(const ErrorData &data):
BadRequest(data.code, data.msg) {
} }
BadRequest::BadRequest(const Package &package): BadRequest::BadRequest(const Package &package):
@ -28,6 +30,7 @@ BadRequest::BadRequest(const Package &package):
QDataStream &BadRequest::fromStream(QDataStream &stream) { QDataStream &BadRequest::fromStream(QDataStream &stream) {
AbstractData::fromStream(stream); AbstractData::fromStream(stream);
stream >> _errCode;
stream >> _err; stream >> _err;
return stream; return stream;
@ -36,11 +39,21 @@ QDataStream &BadRequest::fromStream(QDataStream &stream) {
QDataStream &BadRequest::toStream(QDataStream &stream) const { QDataStream &BadRequest::toStream(QDataStream &stream) const {
AbstractData::toStream(stream); AbstractData::toStream(stream);
stream << _errCode;
stream << _err; stream << _err;
return stream; return stream;
} }
unsigned char BadRequest::errCode() const {
return _errCode;
}
void BadRequest::setErrCode(unsigned char code) {
_errCode = code;
}
QString BadRequest::err() const { QString BadRequest::err() const {
return _err; return _err;
} }

View File

@ -9,21 +9,44 @@
#define BADREQUEST_H #define BADREQUEST_H
#include "abstractdata.h" #include "abstractdata.h"
#include "abstracterrorcodes.h"
namespace QH{ namespace QH{
namespace PKG { namespace PKG {
/**
* @brief The ErrorData struct is simple structure for contains data of the error.
*/
struct ErrorData {
/**
* @brief code This is code of error. By Default thim member equals EccorCodes::UnknownError.
*/
unsigned char code = 0;
/**
* @brief msg This is message of error.
*/
QString msg;
};
/** /**
* @brief The BadRequest class send response about error to client * @brief The BadRequest class send response about error to client
*/ */
class BadRequest : public AbstractData class BadRequest : public AbstractData
{ {
public: public:
/** /**
* @brief BadRequest * @brief BadRequest
* @param errocode This is error code.
* @param err This is error message. * @param err This is error message.
*/ */
explicit BadRequest(const QString & err = ""); explicit BadRequest(unsigned char errocode = ErrorCodes::InvalidRequest, const QString & err = "");
/**
* @brief BadRequest Init BadRequest from the ErrorData struct.
* @param data this is error data. for more information see the ErrorData struct.
*/
explicit BadRequest(const ErrorData& data);
explicit BadRequest(const Package& package); explicit BadRequest(const Package& package);
/** /**
@ -42,8 +65,21 @@ public:
QDataStream &fromStream(QDataStream &stream); QDataStream &fromStream(QDataStream &stream);
QDataStream &toStream(QDataStream &stream) const; QDataStream &toStream(QDataStream &stream) const;
/**
* @brief errCode This method return code of error.
* @return code of error.
*/
unsigned char errCode() const;
/**
* @brief setErrCode This method set error code.
* @param code this is new value of error.
*/
void setErrCode(unsigned char code);
private: private:
QString _err; QString _err;
unsigned char _errCode;
}; };
} }

View File

@ -197,12 +197,12 @@ bool DataBaseNode::sendData(const AbstractData *resp, const BaseId &nodeId, cons
} }
void DataBaseNode::badRequest(const HostAddress &address, const Header &req, void DataBaseNode::badRequest(const HostAddress &address, const Header &req,
const QString msg, quint8 diff) { const ErrorData &err, quint8 diff) {
AbstractNode::badRequest(address, req, msg, diff); AbstractNode::badRequest(address, req, err, diff);
} }
void DataBaseNode::badRequest(const BaseId &address, const Header &req, void DataBaseNode::badRequest(const BaseId &address, const Header &req,
const QString msg, quint8 diff) { const ErrorData &err, quint8 diff) {
if (!changeTrust(address, diff)) { if (!changeTrust(address, diff)) {
@ -213,7 +213,7 @@ void DataBaseNode::badRequest(const BaseId &address, const Header &req,
return; return;
} }
auto bad = BadRequest(msg); auto bad = BadRequest(err);
if (!sendData(&bad, address, &req)) { if (!sendData(&bad, address, &req)) {
return; return;
} }
@ -260,12 +260,19 @@ ParserResult DataBaseNode::parsePackage(const Package &pkg,
BaseId requesterId = getSender(sender, &obj); BaseId requesterId = getSender(sender, &obj);
if (!obj.isValid()) { if (!obj.isValid()) {
badRequest(sender->networkAddress(), pkg.hdr); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InvalidRequest,
"WebSocket request is invalid"
});
return ParserResult::Error; return ParserResult::Error;
} }
if (!workWithSubscribe(obj, requesterId, *sender)) { if (!workWithSubscribe(obj, requesterId, *sender)) {
badRequest(sender->networkAddress(), pkg.hdr); badRequest(sender->networkAddress(), pkg.hdr, {
ErrorCodes::InvalidRequest,
"WebSocket request is invalid"
});
return ParserResult::Error; return ParserResult::Error;
} }
@ -274,7 +281,10 @@ ParserResult DataBaseNode::parsePackage(const Package &pkg,
} else if (H_16<WebSocketSubscriptions>() == pkg.hdr.command) { } else if (H_16<WebSocketSubscriptions>() == pkg.hdr.command) {
WebSocketSubscriptions obj(pkg); WebSocketSubscriptions obj(pkg);
if (!obj.isValid()) { if (!obj.isValid()) {
badRequest(sender->networkAddress(), pkg.hdr); badRequest(sender->networkAddress(), pkg.hdr, {
ErrorCodes::InvalidRequest,
"WebSocketSubscriptions request is invalid"
});
return ParserResult::Error; return ParserResult::Error;
} }

View File

@ -133,19 +133,19 @@ protected:
AbstractNodeInfo *createNodeInfo(QAbstractSocket *socket, const HostAddress *clientAddress) const override; AbstractNodeInfo *createNodeInfo(QAbstractSocket *socket, const HostAddress *clientAddress) const override;
void badRequest(const HostAddress &address, const Header &req, void badRequest(const HostAddress &address, const Header &req,
const QString msg = "", quint8 diff = REQUEST_ERROR) override; const PKG::ErrorData& err, quint8 diff = REQUEST_ERROR) override;
/** /**
* @brief badRequest This implementation of the AbstractNode::badRequest method * @brief badRequest This implementation of the AbstractNode::badRequest method
* send bad request to node with id. * send bad request to node with id.
* @param address This is id of target node or client * @param address This is id of target node or client
* @param req This is header of request. * @param req This is header of request.
* @param msg This is message for target node about error. * @param err This is message and code for target node about error. For more onformation see the PKG::ErrorData struct.
* @param diff This is difference of current trust (currenTrus += diff) * @param diff This is difference of current trust (currenTrus += diff)
* By default diff equals REQUEST_ERROR * By default diff equals REQUEST_ERROR
*/ */
virtual void badRequest(const BaseId &address, const Header &req, virtual void badRequest(const BaseId &address, const Header &req,
const QString msg = "", quint8 diff = REQUEST_ERROR); const PKG::ErrorData& err, quint8 diff = REQUEST_ERROR);
bool changeTrust(const HostAddress &id, int diff) override; bool changeTrust(const HostAddress &id, int diff) override;

View File

@ -0,0 +1,30 @@
#ifndef SingleERRORCODES_H
#define SingleERRORCODES_H
#include "abstracterrorcodes.h"
namespace QH {
namespace ErrorCodes {
/**
* @brief The DBErrorCodes enum This is AuthRequest error codes. For more indormation see the QH::AuthRequest class.
*/
enum DBErrorCodes: unsigned char {
/// User not registered because database not inited or other error occurred.
InternalError = AbstractErrorCodes::AbstractErrorCodes,
/// User not registered because user already exists.
UserExits,
/// User not logined because you need register user befor login.
UserNotExits,
/// User not logined because have an invalid password.
UserInvalidPasswoed,
/// User Already Logged.
UserAlreadyLogged,
/// This case using for inheritance new enum classes.
DBErrorCodes
};
}
}
#endif // SingleERRORCODES_H

View File

@ -3,6 +3,7 @@
#include <authrequest.h> #include <authrequest.h>
#include <sqldbcache.h> #include <sqldbcache.h>
#include <basenodeinfo.h> #include <basenodeinfo.h>
#include <badrequest.h>
namespace QH { namespace QH {
@ -91,7 +92,11 @@ ParserResult SingleServer::parsePackage(const Package &pkg, const AbstractNodeIn
QH::PKG::AuthRequest obj(pkg); QH::PKG::AuthRequest obj(pkg);
if (!obj.isValid()) { if (!obj.isValid()) {
badRequest(sender->networkAddress(), pkg.hdr); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InvalidRequest,
"AuthRequest is invalid"
});
return ParserResult::Error; return ParserResult::Error;
} }
@ -130,34 +135,58 @@ bool SingleServer::workWithUserRequest(const PKG::UserMember* obj,
case RegisteruserResult::InternalError: { case RegisteruserResult::InternalError: {
QuasarAppUtils::Params::log("Internal error ocured in the loginUser or registerNewUser method.", QuasarAppUtils::Params::log("Internal error ocured in the loginUser or registerNewUser method.",
QuasarAppUtils::Error); QuasarAppUtils::Error);
badRequest(sender->networkAddress(), pkg.hdr, "Internal server error." badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InternalError,
"Internal server error."
" Please create issue about this problem in the support page " " Please create issue about this problem in the support page "
" https://github.com/QuasarApp/Heart/issues/new", " https://github.com/QuasarApp/Heart/issues/new"
},
REQUEST_INTERNAL_ERROR); REQUEST_INTERNAL_ERROR);
return false; return false;
} }
case RegisteruserResult::UserExits: { case RegisteruserResult::UserExits: {
badRequest(sender->networkAddress(), pkg.hdr, "Such user already exists ", REQUEST_LOGIN_ERROR); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InternalError,
"Such user already exists "
},
REQUEST_LOGIN_ERROR);
return true; return true;
} }
case RegisteruserResult::UserNotExits: { case RegisteruserResult::UserNotExits: {
badRequest(sender->networkAddress(), pkg.hdr, "Such user not exists ", REQUEST_LOGIN_ERROR); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::UserNotExits,
"Such user not exists "
},
REQUEST_LOGIN_ERROR);
return true; return true;
} }
case RegisteruserResult::UserInvalidPasswoed: { case RegisteruserResult::UserInvalidPasswoed: {
badRequest(sender->networkAddress(), pkg.hdr, "Invalid password ", REQUEST_LOGIN_ERROR); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::UserInvalidPasswoed,
"Invalid password "
},
REQUEST_LOGIN_ERROR);
return true; return true;
} }
case RegisteruserResult::UserAlreadyLogged: { case RegisteruserResult::UserAlreadyLogged: {
badRequest(sender->networkAddress(), pkg.hdr, "User Already Logged", REQUEST_LOGIN_ERROR); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::UserInvalidPasswoed,
"User Already Logged"
},
REQUEST_LOGIN_ERROR);
return true; return true;
} }
@ -167,9 +196,13 @@ bool SingleServer::workWithUserRequest(const PKG::UserMember* obj,
} }
} }
badRequest(sender->networkAddress(), pkg.hdr, "Internal server error. RegisteruserResult return invalid value!" badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InternalError,
"Internal server error."
" Please create issue about this problem in the support page " " Please create issue about this problem in the support page "
" https://github.com/QuasarApp/Heart/issues/new", " https://github.com/QuasarApp/Heart/issues/new"
},
REQUEST_INTERNAL_ERROR); REQUEST_INTERNAL_ERROR);
return false; return false;
} }

View File

@ -3,6 +3,7 @@
#include <databasenode.h> #include <databasenode.h>
#include <accesstoken.h> #include <accesstoken.h>
#include "dberrorcodes.h"
namespace QH { namespace QH {
@ -50,6 +51,7 @@ class SingleServer : public DataBaseNode
{ {
Q_OBJECT Q_OBJECT
public: public:
SingleServer(); SingleServer();
// AbstractNode interface // AbstractNode interface

View File

@ -0,0 +1,21 @@
#ifndef NETWORKERRORCODES_H
#define NETWORKERRORCODES_H
#include "dberrorcodes.h"
namespace QH {
namespace ErrorCodes {
/**
* @brief The AbstractEccorCodes enum This enum with dafault error codes.
*/
enum NetworkErrorCodes: unsigned char {
/// This case using for inheritance new enum classes.
NetworkErrorCodes = DBErrorCodes::DBErrorCodes,
};
}
}
#endif // NETWORKERRORCODES_H

View File

@ -32,6 +32,8 @@
#include <networkmember.h> #include <networkmember.h>
#include <networknodeinfo.h> #include <networknodeinfo.h>
#include <nodeobject.h> #include <nodeobject.h>
#include <permisioncontrolmember.h>
#include "networkerrorcodes.h"
#define THIS_NODE "this_node_key" #define THIS_NODE "this_node_key"
@ -96,7 +98,7 @@ bool NetworkNode::checkSignOfRequest(const AbstractData *request) {
return false; return false;
} }
auto node = db()->getObject(NetworkMember{dbObject->senderID()}); auto node = db()->getObject(PermisionControlMember{dbObject->senderID()});
return _nodeKeys->check(_nodeKeys->concatSign(object->dataForSigned(), return _nodeKeys->check(_nodeKeys->concatSign(object->dataForSigned(),
object->sign()), node->authenticationData()); object->sign()), node->authenticationData());
} }
@ -218,7 +220,7 @@ ParserResult NetworkNode::parsePackage(const Package &pkg,
BadNodeRequest cmd(pkg); BadNodeRequest cmd(pkg);
incomingData(&cmd, baseSender->selfId()); incomingData(&cmd, baseSender->selfId());
emit requestError(cmd.err()); emit requestError(cmd.errCode(), cmd.err());
return ParserResult::Processed; return ParserResult::Processed;
@ -230,12 +232,22 @@ ParserResult NetworkNode::parsePackage(const Package &pkg,
} else if (H_16<NodeObject>() == pkg.hdr.command) { } else if (H_16<NodeObject>() == pkg.hdr.command) {
NodeObject obj(pkg); NodeObject obj(pkg);
if (!obj.isValid()) { if (!obj.isValid()) {
badRequest(sender->networkAddress(), pkg.hdr); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InvalidRequest,
"NodeObject request is invalid"
}
);
return ParserResult::Error; return ParserResult::Error;
} }
if (!workWithNodeObjectData(obj, sender)) { if (!workWithNodeObjectData(obj, sender)) {
badRequest(obj.senderID(), pkg.hdr); badRequest(obj.senderID(), pkg.hdr,
{
ErrorCodes::InvalidRequest,
"NodeObject request is invalid"
}
);
return ParserResult::Error; return ParserResult::Error;
} }
@ -243,12 +255,22 @@ ParserResult NetworkNode::parsePackage(const Package &pkg,
} else if (H_16<KnowAddresses>() == pkg.hdr.command) { } else if (H_16<KnowAddresses>() == pkg.hdr.command) {
KnowAddresses obj(pkg); KnowAddresses obj(pkg);
if (!obj.isValid()) { if (!obj.isValid()) {
badRequest(sender->networkAddress(), pkg.hdr); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InvalidRequest,
"KnowAddresses request is invalid"
}
);
return ParserResult::Error; return ParserResult::Error;
} }
if (!workWithKnowAddresses(obj, sender)) { if (!workWithKnowAddresses(obj, sender)) {
badRequest(sender->networkAddress(), pkg.hdr); badRequest(sender->networkAddress(), pkg.hdr,
{
ErrorCodes::InvalidRequest,
"KnowAddresses request is invalid"
}
);
return ParserResult::Error; return ParserResult::Error;
} }
@ -267,7 +289,12 @@ ParserResult NetworkNode::parsePackage(const Package &pkg,
NetworkRequest cmd(pkg); NetworkRequest cmd(pkg);
if (!cmd.isValid()) { if (!cmd.isValid()) {
badRequest(baseSender->selfId(), pkg.hdr); badRequest(baseSender->selfId(), pkg.hdr,
{
ErrorCodes::InvalidRequest,
"NetworkRequest request is invalid"
}
);
return ParserResult::Error; return ParserResult::Error;
} }

View File

@ -13,7 +13,7 @@ BadNodeRequest::BadNodeRequest() {
} }
BadNodeRequest::BadNodeRequest(const QString &err):BadRequest(err) { BadNodeRequest::BadNodeRequest(const ErrorData &err):BadRequest(err) {
} }

View File

@ -22,7 +22,7 @@ class HEARTSHARED_EXPORT BadNodeRequest: public BadRequest, public SenderData
{ {
public: public:
explicit BadNodeRequest(); explicit BadNodeRequest();
explicit BadNodeRequest(const QString & err = ""); explicit BadNodeRequest(const ErrorData &err);
explicit BadNodeRequest(const Package& package); explicit BadNodeRequest(const Package& package);
// AbstractData interface // AbstractData interface