add base exeption
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,6 +1,3 @@
|
|||||||
build
|
build
|
||||||
.cache
|
.cache
|
||||||
subprojects/*
|
|
||||||
!subprojects/gtest.wrap
|
|
||||||
!subprojects/nlohmann_json.wrap
|
|
||||||
|
|
||||||
|
|||||||
94
src/hack/exception/exception.hpp
Normal file
94
src/hack/exception/exception.hpp
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <experimental/source_location>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
#define DEF_LINE() std::to_string(std::experimental::source_location::current().line())
|
||||||
|
#define DEF_LOCATION() std::experimental::source_location::current().file_name() + std::string(" : ") + DEF_LINE()
|
||||||
|
#define DEF_FILE_NAME() std::experimental::source_location::current().file_name()
|
||||||
|
|
||||||
|
namespace hack::exception
|
||||||
|
{
|
||||||
|
const std::string SET_LOG = "set_log";
|
||||||
|
|
||||||
|
template<typename JSON>
|
||||||
|
class exception
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
exception(const std::string loc = DEF_LOCATION()) : m_location { loc } {}
|
||||||
|
~exception() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void message(const std::string v) noexcept { m_message = v; };
|
||||||
|
void description(const std::string v) noexcept { m_description = v; };
|
||||||
|
void error(const std::exception& e) noexcept { m_error = e.what(); };
|
||||||
|
void level(const std::string& v) noexcept { m_level = v; };
|
||||||
|
|
||||||
|
template<typename Param>
|
||||||
|
void set_params(std::string key, Param value) { m_params[key] = value; }
|
||||||
|
|
||||||
|
template<typename Transaction>
|
||||||
|
void set_transaction(Transaction& tr) { m_params["transaction"] = tr.convert_to_json(); }
|
||||||
|
|
||||||
|
// если что-то не значительное, но в больших обемах, то можноиспользовать этот метод
|
||||||
|
// на выходе будет что-то типа { "arg_1" : 123, "arg_2" : "value" }
|
||||||
|
template<typename... Args>
|
||||||
|
void set_variadic_params(Args... args)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
([&] { m_params["arg_" + std::to_string(++i)] = args; }(), ...);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename Database>
|
||||||
|
void commit(const std::filesystem::path file_name = DEF_FILE_NAME(), std::string line = DEF_LINE())
|
||||||
|
{
|
||||||
|
auto r = Database::instance().execute(SET_LOG, convert_to_json());
|
||||||
|
error()(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum class log_level { LOW, MID, HIGHT };
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string log_converter(log_level ll)
|
||||||
|
{
|
||||||
|
std::string r;
|
||||||
|
switch (ll)
|
||||||
|
{
|
||||||
|
case log_level::LOW:
|
||||||
|
r = "LOW";
|
||||||
|
break;;
|
||||||
|
case log_level::MID:
|
||||||
|
r = "MIDLE";
|
||||||
|
break;;
|
||||||
|
default:
|
||||||
|
r = "HIGHT";
|
||||||
|
break;;
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSON convert_to_json() override
|
||||||
|
{
|
||||||
|
JSON j;
|
||||||
|
j["description"] = m_description;
|
||||||
|
j["system_error"] = m_error;
|
||||||
|
j["level"] = m_level;
|
||||||
|
j["location"] = m_location;
|
||||||
|
j["params"] = m_params;
|
||||||
|
j["msg_to_front"] = m_message;
|
||||||
|
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_message;
|
||||||
|
std::string m_description;
|
||||||
|
std::string m_error;
|
||||||
|
std::string m_location;
|
||||||
|
std::string m_level = log_converter(log_level::LOW);
|
||||||
|
JSON m_params;
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user