feat: add exceptions
This commit is contained in:
62
L2BotCore/Domain/Exceptions.h
Normal file
62
L2BotCore/Domain/Exceptions.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace L2Bot::Domain
|
||||
{
|
||||
class RuntimeException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
using _Mybase = std::runtime_error;
|
||||
|
||||
explicit RuntimeException(const std::wstring& _Message) : _Mybase(convert(_Message)), m_Message(_Message) {}
|
||||
|
||||
explicit RuntimeException(const wchar_t* _Message) : RuntimeException(std::wstring(_Message)) {}
|
||||
|
||||
/// returns the explanatory string
|
||||
const char* what() const noexcept override
|
||||
{
|
||||
return std::runtime_error::what();
|
||||
}
|
||||
|
||||
const std::wstring& Message() const noexcept
|
||||
{
|
||||
return m_Message;
|
||||
}
|
||||
|
||||
#if !_HAS_EXCEPTIONS
|
||||
protected:
|
||||
virtual void _Doraise() const override { // perform class-specific exception handling
|
||||
_RAISE(*this);
|
||||
}
|
||||
#endif // !_HAS_EXCEPTIONS
|
||||
|
||||
private:
|
||||
const std::string convert(const std::wstring& wmsg) const
|
||||
{
|
||||
const auto size = wmsg.size() + 1;
|
||||
char* msg = new char[size];
|
||||
size_t charsConverted = 0;
|
||||
wcstombs_s(&charsConverted, msg, size, wmsg.c_str(), wmsg.length());
|
||||
|
||||
const std::string result(msg);
|
||||
delete[] msg;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::wstring m_Message;
|
||||
};
|
||||
|
||||
class CriticalRuntimeException : public RuntimeException
|
||||
{
|
||||
public:
|
||||
explicit CriticalRuntimeException(const std::wstring& _Message) : RuntimeException(_Message) {}
|
||||
|
||||
explicit CriticalRuntimeException(const wchar_t* _Message) : RuntimeException(_Message) {}
|
||||
};
|
||||
|
||||
}
|
@@ -33,7 +33,7 @@ namespace L2Bot::Domain::Logger
|
||||
struct tm timeinfo;
|
||||
localtime_s(&timeinfo, &rawTime);
|
||||
std::wstringstream oss;
|
||||
oss << "[" << std::put_time(&timeinfo, L"%Y-%m-%d %H.%M.%S") << "]";
|
||||
oss << "[" << std::put_time(&timeinfo, L"%Y-%m-%d %H:%M:%S") << "]";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
@@ -35,9 +35,30 @@ namespace L2Bot::Domain::Logger
|
||||
Log(LogLevel::app, format, args...);
|
||||
}
|
||||
|
||||
void Error(const std::wstring& message) const
|
||||
{
|
||||
Log(LogLevel::error, message);
|
||||
}
|
||||
void Warning(const std::wstring& message) const
|
||||
{
|
||||
Log(LogLevel::warning, message);
|
||||
}
|
||||
void Info(const std::wstring& message) const
|
||||
{
|
||||
Log(LogLevel::info, message);
|
||||
}
|
||||
void App(const std::wstring& message) const
|
||||
{
|
||||
Log(LogLevel::app, message);
|
||||
}
|
||||
|
||||
private:
|
||||
template <class ... Args>
|
||||
void Log(LogLevel level, const std::wformat_string<Args...> format, Args... args) const
|
||||
{
|
||||
Log(level, std::vformat(format.get(), std::make_wformat_args(args...)));
|
||||
}
|
||||
void Log(LogLevel level, const std::wstring& message) const
|
||||
{
|
||||
std::wstring prefix = L"";
|
||||
if (level == LogLevel::error) {
|
||||
@@ -55,7 +76,7 @@ namespace L2Bot::Domain::Logger
|
||||
|
||||
for (const auto& channel : m_Channels) {
|
||||
if (channel->IsAppropriateLevel(level)) {
|
||||
channel->SendToChannel(prefix + std::vformat(format.get(), std::make_wformat_args(args...)));
|
||||
channel->SendToChannel(prefix + message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,6 +10,8 @@
|
||||
#include "../DTO/Message.h"
|
||||
#include "../Services/IncomingMessageProcessor.h"
|
||||
#include "../Services/OutgoingMessageBuilder.h"
|
||||
#include "../Exceptions.h"
|
||||
#include "../Services/ServiceLocator.h"
|
||||
|
||||
namespace L2Bot::Domain::Services
|
||||
{
|
||||
@@ -69,19 +71,30 @@ namespace L2Bot::Domain::Services
|
||||
{
|
||||
while (!m_Stopped)
|
||||
{
|
||||
const auto& messages = GetOutgoingMessages();
|
||||
try {
|
||||
const auto& messages = GetOutgoingMessages();
|
||||
|
||||
if (m_Transport.IsConnected())
|
||||
{
|
||||
for (const auto& message : messages)
|
||||
if (m_Transport.IsConnected())
|
||||
{
|
||||
m_Transport.Send(
|
||||
m_Serializer.Serialize(message)
|
||||
);
|
||||
for (const auto& message : messages)
|
||||
{
|
||||
m_Transport.Send(
|
||||
m_Serializer.Serialize(message)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
catch (const CriticalRuntimeException& e)
|
||||
{
|
||||
m_Stopped = true;
|
||||
ServiceLocator::GetInstance().GetLogger()->Error(e.Message());
|
||||
}
|
||||
catch (const RuntimeException& e)
|
||||
{
|
||||
ServiceLocator::GetInstance().GetLogger()->Warning(e.Message());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,15 +102,28 @@ namespace L2Bot::Domain::Services
|
||||
{
|
||||
while (!m_Stopped)
|
||||
{
|
||||
if (m_Transport.IsConnected())
|
||||
{
|
||||
const auto messageType = m_IncomingMessageProcessor.Process(m_Transport.Receive());
|
||||
try {
|
||||
if (m_Transport.IsConnected())
|
||||
{
|
||||
const auto& message = m_Transport.Receive();
|
||||
ServiceLocator::GetInstance().GetLogger()->Info(L"received message from client: {}", message);
|
||||
const auto messageType = m_IncomingMessageProcessor.Process(message);
|
||||
|
||||
if (messageType == Serializers::IncomingMessage::Type::invalidate) {
|
||||
Invalidate();
|
||||
if (messageType == Serializers::IncomingMessage::Type::invalidate) {
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
catch (const CriticalRuntimeException& e)
|
||||
{
|
||||
m_Stopped = true;
|
||||
ServiceLocator::GetInstance().GetLogger()->Error(e.Message());
|
||||
}
|
||||
catch (const RuntimeException& e)
|
||||
{
|
||||
ServiceLocator::GetInstance().GetLogger()->Warning(e.Message());
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,11 +131,22 @@ namespace L2Bot::Domain::Services
|
||||
{
|
||||
while (!m_Stopped)
|
||||
{
|
||||
if (!m_Transport.IsConnected())
|
||||
{
|
||||
m_Transport.Connect();
|
||||
try {
|
||||
if (!m_Transport.IsConnected())
|
||||
{
|
||||
m_Transport.Connect();
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
catch (const CriticalRuntimeException& e)
|
||||
{
|
||||
m_Stopped = true;
|
||||
ServiceLocator::GetInstance().GetLogger()->Error(e.Message());
|
||||
}
|
||||
catch (const RuntimeException& e)
|
||||
{
|
||||
ServiceLocator::GetInstance().GetLogger()->Warning(e.Message());
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user