Skip to content

Commit

Permalink
refactor: print error with LeviLamina's interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Jan 6, 2025
1 parent be35a8f commit 14bbe10
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 104 deletions.
46 changes: 15 additions & 31 deletions src/legacy/api/APIHelp.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@
#include <exception>
#include <magic_enum.hpp>

// 输出异常信息
inline void PrintException(const script::Exception& e) {
lse::getSelfPluginInstance().getLogger().error("script::Exception: {0}\n{1}", e.message(), e.stacktrace());
}

inline void PrintScriptStackTrace(std::string const& msg = "") {
if (!msg.empty()) {
PrintException(script::Exception(msg));
} else {
lse::getSelfPluginInstance().getLogger().error(script::Exception(msg).stacktrace());
}
}

// 实例类类型检查
template <typename T>
bool inline IsInstanceOf(Local<Value> v) {
Expand All @@ -37,7 +24,8 @@ std::string ValueKindToString(const ValueKind& kind);

// 输出脚本调用堆栈,API名称,以及插件名
inline void LOG_ERROR_WITH_SCRIPT_INFO(std::string const& func = "", std::string const& msg = "") {
PrintScriptStackTrace(msg);
auto e = script::Exception(msg);
lse::getSelfPluginInstance().getLogger().error("script::Exception: {0}\n{1}", e.message(), e.stacktrace());
lse::getSelfPluginInstance().getLogger().error("In API: " + func);
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
}
Expand Down Expand Up @@ -72,14 +60,13 @@ inline void LOG_WRONG_ARGS_COUNT(std::string const& func = "") {
// 截获引擎异常
#define CATCH(LOG) \
catch (const Exception& e) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
PrintException(e); \
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
return Local<Value>(); \
} \
catch (...) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
return Local<Value>(); \
}

Expand Down Expand Up @@ -107,47 +94,44 @@ inline void LOG_WRONG_ARGS_COUNT(std::string const& func = "") {
// 截获引擎异常_Constructor
#define CATCH_C(LOG) \
catch (const Exception& e) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
PrintException(e); \
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
return nullptr; \
} \
catch (...) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
return nullptr; \
}

// 截获引擎异常_Setter
#define CATCH_S(LOG) \
catch (const Exception& e) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
PrintException(e); \
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
return; \
} \
catch (...) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
return; \
}

// 截获引擎异常_Constructor
#define CATCH_WITHOUT_RETURN(LOG) \
catch (const Exception& e) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
PrintException(e); \
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
} \
catch (...) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
}

// 截获回调函数异常
#define CATCH_IN_CALLBACK(callback) \
catch (const Exception& e) { \
PrintException(e); \
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
lse::getSelfPluginInstance().getLogger().error(std::string("In callback for ") + callback); \
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
}
Expand Down
42 changes: 15 additions & 27 deletions src/legacy/api/DataAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ Local<Value> ConfJsonClass::reload(const Arguments&) {
return Boolean::newBoolean(reload());
} catch (const ordered_json::exception& e) {
lse::getSelfPluginInstance().getLogger().error("Fail to parse json content in file!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
}
CATCH("Fail in confJsonReload!");
Expand Down Expand Up @@ -259,7 +258,8 @@ bool ConfJsonClass::reload() {
jsonConf = ordered_json::parse(*jsonTexts, nullptr, true, true);
} catch (...) {
lse::getSelfPluginInstance().getLogger().error("Fail in confJsonReload!");
PrintScriptStackTrace();
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger());
return false;
}

return true;
Expand Down Expand Up @@ -538,13 +538,11 @@ Local<Value> MoneyClass::set(const Arguments& args) {
);
} catch (const std::invalid_argument& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneySet!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
} catch (const std::out_of_range& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneySet!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
}
CATCH("Fail in MoneySet!");
Expand All @@ -558,13 +556,11 @@ Local<Value> MoneyClass::get(const Arguments& args) {
return Number::newNumber(EconomySystem::getMoney(args[0].asString().toString()));
} catch (const std::invalid_argument& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyGet!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Number::newNumber(0);
} catch (const std::out_of_range& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyGet!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Number::newNumber(0);
}
CATCH("Fail in MoneyGet!");
Expand All @@ -580,13 +576,11 @@ Local<Value> MoneyClass::add(const Arguments& args) {
);
} catch (const std::invalid_argument& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyAdd!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
} catch (const std::out_of_range& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyAdd!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
}
CATCH("Fail in MoneyAdd!");
Expand All @@ -603,13 +597,11 @@ Local<Value> MoneyClass::reduce(const Arguments& args) {
);
} catch (const std::invalid_argument& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyReduce!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
} catch (const std::out_of_range& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyReduce!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
}
CATCH("Fail in MoneyReduce!");
Expand All @@ -632,13 +624,11 @@ Local<Value> MoneyClass::trans(const Arguments& args) {
));
} catch (const std::invalid_argument& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyTrans!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
} catch (const std::out_of_range& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyTrans!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Boolean::newBoolean(false);
}
CATCH("Fail in MoneyTrans!");
Expand Down Expand Up @@ -689,13 +679,11 @@ Local<Value> MoneyClass::getHistory(const Arguments& args) {
return objectificationMoneyHistory(res);
} catch (const std::invalid_argument& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyGetHintory!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Local<Value>();
} catch (const std::out_of_range& e) {
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyGetHintory!");
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
PrintScriptStackTrace();
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
return Local<Value>();
}
CATCH("Fail in MoneyGetHintory!");
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/api/DatabaseAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using namespace DB;
#define CATCH_AND_THROW(LOG) \
catch (const Exception& e) { \
lse::getSelfPluginInstance().getLogger().error(LOG); \
PrintException(e); \
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
return Local<Value>(); \
} \
Expand All @@ -14,7 +14,7 @@ using namespace DB;
} \
catch (...) { \
lse::getSelfPluginInstance().getLogger().error("Uncaught Exception Detected!"); \
PrintScriptStackTrace(); \
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
lse::getSelfPluginInstance().getLogger().error("In API: " __FUNCTION__); \
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
return Local<Value>(); \
Expand Down
Loading

0 comments on commit 14bbe10

Please sign in to comment.