Skip to content

Commit 14bbe10

Browse files
committed
refactor: print error with LeviLamina's interface
1 parent be35a8f commit 14bbe10

File tree

12 files changed

+65
-104
lines changed

12 files changed

+65
-104
lines changed

src/legacy/api/APIHelp.h

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@
1212
#include <exception>
1313
#include <magic_enum.hpp>
1414

15-
// 输出异常信息
16-
inline void PrintException(const script::Exception& e) {
17-
lse::getSelfPluginInstance().getLogger().error("script::Exception: {0}\n{1}", e.message(), e.stacktrace());
18-
}
19-
20-
inline void PrintScriptStackTrace(std::string const& msg = "") {
21-
if (!msg.empty()) {
22-
PrintException(script::Exception(msg));
23-
} else {
24-
lse::getSelfPluginInstance().getLogger().error(script::Exception(msg).stacktrace());
25-
}
26-
}
27-
2815
// 实例类类型检查
2916
template <typename T>
3017
bool inline IsInstanceOf(Local<Value> v) {
@@ -37,7 +24,8 @@ std::string ValueKindToString(const ValueKind& kind);
3724

3825
// 输出脚本调用堆栈,API名称,以及插件名
3926
inline void LOG_ERROR_WITH_SCRIPT_INFO(std::string const& func = "", std::string const& msg = "") {
40-
PrintScriptStackTrace(msg);
27+
auto e = script::Exception(msg);
28+
lse::getSelfPluginInstance().getLogger().error("script::Exception: {0}\n{1}", e.message(), e.stacktrace());
4129
lse::getSelfPluginInstance().getLogger().error("In API: " + func);
4230
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
4331
}
@@ -72,14 +60,13 @@ inline void LOG_WRONG_ARGS_COUNT(std::string const& func = "") {
7260
// 截获引擎异常
7361
#define CATCH(LOG) \
7462
catch (const Exception& e) { \
75-
lse::getSelfPluginInstance().getLogger().error(LOG); \
76-
PrintException(e); \
63+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
64+
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
7765
return Local<Value>(); \
7866
} \
7967
catch (...) { \
80-
lse::getSelfPluginInstance().getLogger().error(LOG); \
8168
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
82-
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
69+
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
8370
return Local<Value>(); \
8471
}
8572

@@ -107,47 +94,44 @@ inline void LOG_WRONG_ARGS_COUNT(std::string const& func = "") {
10794
// 截获引擎异常_Constructor
10895
#define CATCH_C(LOG) \
10996
catch (const Exception& e) { \
110-
lse::getSelfPluginInstance().getLogger().error(LOG); \
111-
PrintException(e); \
97+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
98+
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
11299
return nullptr; \
113100
} \
114101
catch (...) { \
115-
lse::getSelfPluginInstance().getLogger().error(LOG); \
116102
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
117-
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
103+
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
118104
return nullptr; \
119105
}
120106

121107
// 截获引擎异常_Setter
122108
#define CATCH_S(LOG) \
123109
catch (const Exception& e) { \
124-
lse::getSelfPluginInstance().getLogger().error(LOG); \
125-
PrintException(e); \
110+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
111+
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
126112
return; \
127113
} \
128114
catch (...) { \
129-
lse::getSelfPluginInstance().getLogger().error(LOG); \
130115
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
131-
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
116+
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
132117
return; \
133118
}
134119

135120
// 截获引擎异常_Constructor
136121
#define CATCH_WITHOUT_RETURN(LOG) \
137122
catch (const Exception& e) { \
138-
lse::getSelfPluginInstance().getLogger().error(LOG); \
139-
PrintException(e); \
123+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
124+
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
140125
} \
141126
catch (...) { \
142-
lse::getSelfPluginInstance().getLogger().error(LOG); \
143127
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
144-
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__); \
128+
LOG_ERROR_WITH_SCRIPT_INFO(__FUNCTION__, LOG); \
145129
}
146130

147131
// 截获回调函数异常
148132
#define CATCH_IN_CALLBACK(callback) \
149133
catch (const Exception& e) { \
150-
PrintException(e); \
134+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
151135
lse::getSelfPluginInstance().getLogger().error(std::string("In callback for ") + callback); \
152136
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
153137
}

src/legacy/api/DataAPI.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ Local<Value> ConfJsonClass::reload(const Arguments&) {
210210
return Boolean::newBoolean(reload());
211211
} catch (const ordered_json::exception& e) {
212212
lse::getSelfPluginInstance().getLogger().error("Fail to parse json content in file!");
213-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
214-
PrintScriptStackTrace();
213+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
215214
return Boolean::newBoolean(false);
216215
}
217216
CATCH("Fail in confJsonReload!");
@@ -259,7 +258,8 @@ bool ConfJsonClass::reload() {
259258
jsonConf = ordered_json::parse(*jsonTexts, nullptr, true, true);
260259
} catch (...) {
261260
lse::getSelfPluginInstance().getLogger().error("Fail in confJsonReload!");
262-
PrintScriptStackTrace();
261+
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger());
262+
return false;
263263
}
264264

265265
return true;
@@ -538,13 +538,11 @@ Local<Value> MoneyClass::set(const Arguments& args) {
538538
);
539539
} catch (const std::invalid_argument& e) {
540540
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneySet!");
541-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
542-
PrintScriptStackTrace();
541+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
543542
return Boolean::newBoolean(false);
544543
} catch (const std::out_of_range& e) {
545544
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneySet!");
546-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
547-
PrintScriptStackTrace();
545+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
548546
return Boolean::newBoolean(false);
549547
}
550548
CATCH("Fail in MoneySet!");
@@ -558,13 +556,11 @@ Local<Value> MoneyClass::get(const Arguments& args) {
558556
return Number::newNumber(EconomySystem::getMoney(args[0].asString().toString()));
559557
} catch (const std::invalid_argument& e) {
560558
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyGet!");
561-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
562-
PrintScriptStackTrace();
559+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
563560
return Number::newNumber(0);
564561
} catch (const std::out_of_range& e) {
565562
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyGet!");
566-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
567-
PrintScriptStackTrace();
563+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
568564
return Number::newNumber(0);
569565
}
570566
CATCH("Fail in MoneyGet!");
@@ -580,13 +576,11 @@ Local<Value> MoneyClass::add(const Arguments& args) {
580576
);
581577
} catch (const std::invalid_argument& e) {
582578
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyAdd!");
583-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
584-
PrintScriptStackTrace();
579+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
585580
return Boolean::newBoolean(false);
586581
} catch (const std::out_of_range& e) {
587582
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyAdd!");
588-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
589-
PrintScriptStackTrace();
583+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
590584
return Boolean::newBoolean(false);
591585
}
592586
CATCH("Fail in MoneyAdd!");
@@ -603,13 +597,11 @@ Local<Value> MoneyClass::reduce(const Arguments& args) {
603597
);
604598
} catch (const std::invalid_argument& e) {
605599
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyReduce!");
606-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
607-
PrintScriptStackTrace();
600+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
608601
return Boolean::newBoolean(false);
609602
} catch (const std::out_of_range& e) {
610603
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyReduce!");
611-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
612-
PrintScriptStackTrace();
604+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
613605
return Boolean::newBoolean(false);
614606
}
615607
CATCH("Fail in MoneyReduce!");
@@ -632,13 +624,11 @@ Local<Value> MoneyClass::trans(const Arguments& args) {
632624
));
633625
} catch (const std::invalid_argument& e) {
634626
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyTrans!");
635-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
636-
PrintScriptStackTrace();
627+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
637628
return Boolean::newBoolean(false);
638629
} catch (const std::out_of_range& e) {
639630
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyTrans!");
640-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
641-
PrintScriptStackTrace();
631+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
642632
return Boolean::newBoolean(false);
643633
}
644634
CATCH("Fail in MoneyTrans!");
@@ -689,13 +679,11 @@ Local<Value> MoneyClass::getHistory(const Arguments& args) {
689679
return objectificationMoneyHistory(res);
690680
} catch (const std::invalid_argument& e) {
691681
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyGetHintory!");
692-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
693-
PrintScriptStackTrace();
682+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
694683
return Local<Value>();
695684
} catch (const std::out_of_range& e) {
696685
lse::getSelfPluginInstance().getLogger().error("Bad argument in MoneyGetHintory!");
697-
lse::getSelfPluginInstance().getLogger().error(ll::string_utils::tou8str(e.what()));
698-
PrintScriptStackTrace();
686+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger());
699687
return Local<Value>();
700688
}
701689
CATCH("Fail in MoneyGetHintory!");

src/legacy/api/DatabaseAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using namespace DB;
55
#define CATCH_AND_THROW(LOG) \
66
catch (const Exception& e) { \
77
lse::getSelfPluginInstance().getLogger().error(LOG); \
8-
PrintException(e); \
8+
ll::error_utils::printException(e, lse::getSelfPluginInstance().getLogger()); \
99
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
1010
return Local<Value>(); \
1111
} \
@@ -14,7 +14,7 @@ using namespace DB;
1414
} \
1515
catch (...) { \
1616
lse::getSelfPluginInstance().getLogger().error("Uncaught Exception Detected!"); \
17-
PrintScriptStackTrace(); \
17+
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
1818
lse::getSelfPluginInstance().getLogger().error("In API: " __FUNCTION__); \
1919
lse::getSelfPluginInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName); \
2020
return Local<Value>(); \

0 commit comments

Comments
 (0)