Skip to content

Commit b340151

Browse files
committed
refactor: optimize
1 parent aa63ce7 commit b340151

File tree

10 files changed

+76
-197
lines changed

10 files changed

+76
-197
lines changed

src/legacy/api/APIHelp.h

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ std::string ValueKindToString(ValueKind const& kind);
2121

2222
// 输出脚本调用堆栈,API名称,以及插件名
2323
inline Exception CreateExceptionWithInfo(std::string const& func, std::string const& msg) {
24-
return Exception(fmt::format("In API: {}, In Plugin: {}, {}", func, getEngineOwnData()->pluginName, msg));
24+
return Exception(fmt::format("In API: {}, In plugin: {}, {}", func, getEngineOwnData()->pluginName, msg));
2525
}
2626

2727
inline void LogErrorWithInfo(std::string const& func) {
28-
lse::LegacyScriptEngine::getLogger().error("In API: {}, In Plugin: {}", func, getEngineOwnData()->pluginName);
28+
lse::LegacyScriptEngine::getLogger().error("In API: {}, In plugin: {}", func, getEngineOwnData()->pluginName);
2929
}
3030

3131
// 参数类型错误输出
@@ -57,11 +57,8 @@ inline Exception WrongArgsCountException(std::string const& func) {
5757

5858
// 截获引擎异常
5959
#define CATCH_AND_THROW \
60-
catch (Exception const&) { \
61-
throw; \
62-
} \
63-
catch (std::exception const& e) { \
64-
throw Exception(e.what()); \
60+
catch (Exception const&) { \
61+
throw; \
6562
} \
6663
catch (...) { \
6764
throw Exception( \
@@ -70,39 +67,20 @@ inline Exception WrongArgsCountException(std::string const& func) {
7067
}
7168

7269
#define CATCH \
73-
catch (const Exception& e) { \
74-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger()); \
75-
LogErrorWithInfo(__FUNCTION__); \
76-
} \
7770
catch (...) { \
7871
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger()); \
7972
LogErrorWithInfo(__FUNCTION__); \
8073
}
8174

82-
#define CATCH_WITH_MESSAGE(LOG) \
83-
catch (const Exception& e) { \
84-
lse::LegacyScriptEngine::getLogger().error(LOG); \
85-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger()); \
86-
LogErrorWithInfo(__FUNCTION__); \
87-
} \
75+
#define CATCH_WITH_MESSAGE(...) \
8876
catch (...) { \
89-
lse::LegacyScriptEngine::getLogger().error(LOG); \
77+
lse::LegacyScriptEngine::getLogger().error(__VA_ARGS__); \
9078
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger()); \
9179
LogErrorWithInfo(__FUNCTION__); \
9280
}
9381

9482
// 截获回调函数异常
95-
#define CATCH_IN_CALLBACK(callback) \
96-
catch (const Exception& e) { \
97-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger()); \
98-
lse::LegacyScriptEngine::getLogger().error(std::string("In callback for ") + callback); \
99-
LogErrorWithInfo(__FUNCTION__); \
100-
} \
101-
catch (...) { \
102-
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger()); \
103-
lse::LegacyScriptEngine::getLogger().error(std::string("In callback for ") + callback); \
104-
LogErrorWithInfo(__FUNCTION__); \
105-
}
83+
#define CATCH_IN_CALLBACK(...) CATCH_WITH_MESSAGE("In callback for", __VA_ARGS__)
10684

10785
// 判断是否为浮点数
10886
bool CheckIsFloat(Local<Value> const& num);

src/legacy/api/DataAPI.cpp

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,6 @@ Local<Value> ConfJsonClass::del(Arguments const& args) {
205205
Local<Value> ConfJsonClass::reload(Arguments const&) {
206206
try {
207207
return Boolean::newBoolean(reload());
208-
} catch (ordered_json::exception const& e) {
209-
lse::LegacyScriptEngine::getLogger().error("Fail to parse json content in file!");
210-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
211-
return Boolean::newBoolean(false);
212208
}
213209
CATCH_AND_THROW
214210
}
@@ -251,15 +247,7 @@ bool ConfJsonClass::close() {
251247
bool ConfJsonClass::reload() {
252248
auto jsonTexts = ll::file_utils::readFile(ll::string_utils::str2u8str(confPath));
253249
if (!jsonTexts) return false;
254-
255-
try {
256-
jsonConf = ordered_json::parse(*jsonTexts, nullptr, true, true);
257-
} catch (...) {
258-
lse::LegacyScriptEngine::getLogger().error("Fail in confJsonReload!");
259-
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger());
260-
return false;
261-
}
262-
250+
jsonConf = ordered_json::parse(*jsonTexts, nullptr, true, true);
263251
return true;
264252
}
265253

@@ -530,14 +518,6 @@ Local<Value> MoneyClass::set(Arguments const& args) {
530518
return Boolean::newBoolean(
531519
EconomySystem::setMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
532520
);
533-
} catch (std::invalid_argument const& e) {
534-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneySet!");
535-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
536-
return Boolean::newBoolean(false);
537-
} catch (std::out_of_range const& e) {
538-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneySet!");
539-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
540-
return Boolean::newBoolean(false);
541521
}
542522
CATCH_AND_THROW
543523
}
@@ -548,14 +528,6 @@ Local<Value> MoneyClass::get(Arguments const& args) {
548528

549529
try {
550530
return Number::newNumber(EconomySystem::getMoney(args[0].asString().toString()));
551-
} catch (std::invalid_argument const& e) {
552-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyGet!");
553-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
554-
return Number::newNumber(0);
555-
} catch (std::out_of_range const& e) {
556-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyGet!");
557-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
558-
return Number::newNumber(0);
559531
}
560532
CATCH_AND_THROW
561533
}
@@ -569,14 +541,6 @@ Local<Value> MoneyClass::add(Arguments const& args) {
569541
return Boolean::newBoolean(
570542
EconomySystem::addMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
571543
);
572-
} catch (std::invalid_argument const& e) {
573-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyAdd!");
574-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
575-
return Boolean::newBoolean(false);
576-
} catch (std::out_of_range const& e) {
577-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyAdd!");
578-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
579-
return Boolean::newBoolean(false);
580544
}
581545
CATCH_AND_THROW
582546
}
@@ -590,14 +554,6 @@ Local<Value> MoneyClass::reduce(Arguments const& args) {
590554
return Boolean::newBoolean(
591555
EconomySystem::reduceMoney(args[0].asString().toString(), args[1].asNumber().toInt64())
592556
);
593-
} catch (std::invalid_argument const& e) {
594-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyReduce!");
595-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
596-
return Boolean::newBoolean(false);
597-
} catch (std::out_of_range const& e) {
598-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyReduce!");
599-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
600-
return Boolean::newBoolean(false);
601557
}
602558
CATCH_AND_THROW
603559
}
@@ -619,14 +575,6 @@ Local<Value> MoneyClass::trans(Arguments const& args) {
619575
note
620576
)
621577
);
622-
} catch (std::invalid_argument const& e) {
623-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyTrans!");
624-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
625-
return Boolean::newBoolean(false);
626-
} catch (std::out_of_range const& e) {
627-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyTrans!");
628-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
629-
return Boolean::newBoolean(false);
630578
}
631579
CATCH_AND_THROW
632580
}
@@ -659,16 +607,8 @@ Local<Value> MoneyClass::getHistory(Arguments const& args) {
659607
CHECK_ARG_TYPE(args[1], ValueKind::kNumber);
660608

661609
try {
662-
string res{EconomySystem::getMoneyHist(args[0].asString().toString(), args[1].asNumber().toInt32())};
610+
string res = EconomySystem::getMoneyHist(args[0].asString().toString(), args[1].asNumber().toInt32());
663611
return objectificationMoneyHistory(res);
664-
} catch (std::invalid_argument const& e) {
665-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyGetHistory!");
666-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
667-
return {};
668-
} catch (std::out_of_range const& e) {
669-
lse::LegacyScriptEngine::getLogger().error("Bad argument in MoneyGetHistory!");
670-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
671-
return {};
672612
}
673613
CATCH_AND_THROW
674614
}

src/legacy/api/EventAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ LLSEAddEventListener(ScriptEngine* engine, std::string const& eventName, Local<F
108108
return {listener};
109109
} catch (...) {
110110
lse::LegacyScriptEngine::getLogger().error("Event {} not found!"_tr(eventName));
111-
lse::LegacyScriptEngine::getLogger().error("In Plugin: " + getEngineData(engine)->pluginName);
111+
lse::LegacyScriptEngine::getLogger().error("In plugin: {}", getEngineData(engine)->pluginName);
112112
return std::nullopt;
113113
}
114114
}

src/legacy/api/EventAPI.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,8 @@ void CallEventImpl(EventListener& listener, bool& returnValue, EVENT_TYPES type,
178178
if (result.isBoolean() && result.asBoolean().value() == false) {
179179
returnValue = false;
180180
}
181-
} catch (Exception const& e) {
182-
lse::LegacyScriptEngine::getLogger().error("CallEvent Callback Failed!");
183-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
184-
lse::LegacyScriptEngine::getLogger().error("In Event: " + EventTypeToString(type));
185-
lse::LegacyScriptEngine::getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
186-
} catch (...) {
187-
lse::LegacyScriptEngine::getLogger().error("CallEvent Callback Failed!");
188-
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger());
189-
lse::LegacyScriptEngine::getLogger().error("In Event: " + EventTypeToString(type));
190-
lse::LegacyScriptEngine::getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
191181
}
182+
CATCH_WITH_MESSAGE("CallEvent Callback Failed! In Event: " + EventTypeToString(type))
192183
}
193184

194185
#define FakeCallEvent(ENGINE, TYPE, ...) \
@@ -203,17 +194,8 @@ void FakeCallEventImpl(EventListener& listener, ScriptEngine* engine, EVENT_TYPE
203194
if (listener.engine == engine) {
204195
try {
205196
listener.func.get().call({}, args...);
206-
} catch (Exception const& e) {
207-
lse::LegacyScriptEngine::getLogger().error("FakeCallEvent Callback Failed!");
208-
ll::error_utils::printException(e, lse::LegacyScriptEngine::getLogger());
209-
lse::LegacyScriptEngine::getLogger().error("In Event: " + EventTypeToString(type));
210-
lse::LegacyScriptEngine::getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
211-
} catch (...) {
212-
lse::LegacyScriptEngine::getLogger().error("FakeCallEvent Callback Failed!");
213-
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger());
214-
lse::LegacyScriptEngine::getLogger().error("In Event: " + EventTypeToString(type));
215-
lse::LegacyScriptEngine::getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
216197
}
198+
CATCH_WITH_MESSAGE("FakeCallEvent Callback Failed!")
217199
}
218200
}
219201

@@ -223,8 +205,7 @@ void FakeCallEventImpl(EventListener& listener, ScriptEngine* engine, EVENT_TYPE
223205
try
224206
#define IF_LISTENED_END(TYPE) \
225207
catch (...) { \
226-
lse::LegacyScriptEngine::getLogger().error("Event Callback Failed!"); \
208+
lse::LegacyScriptEngine::getLogger().error("Event Callback Failed! In Event: " + EventTypeToString(TYPE)); \
227209
ll::error_utils::printCurrentException(lse::LegacyScriptEngine::getLogger()); \
228-
lse::LegacyScriptEngine::getLogger().error("In Event: " + EventTypeToString(TYPE)); \
229210
} \
230211
}

0 commit comments

Comments
 (0)