Skip to content

Commit aa04f8f

Browse files
committed
fix: fix HttpServer exception #53
refactor: add seh translator
1 parent c5f6392 commit aa04f8f

File tree

5 files changed

+104
-68
lines changed

5 files changed

+104
-68
lines changed

src/legacy/api/APIHelp.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414

1515
// 输出异常信息
1616
inline void PrintException(const script::Exception& e) {
17-
std::ostringstream sout;
18-
sout << "script::Exception: ";
19-
sout << e;
20-
lse::getSelfPluginInstance().getLogger().error(sout.str());
17+
lse::getSelfPluginInstance().getLogger().error("script::Exception: {0}\n{1}", e.message(), e.stacktrace());
2118
}
2219

2320
inline void PrintScriptStackTrace(std::string const& msg = "") {
2421
if (!msg.empty()) {
2522
PrintException(script::Exception(msg));
2623
} else {
27-
lse::getSelfPluginInstance().getLogger().error(script::Exception(msg).message());
24+
lse::getSelfPluginInstance().getLogger().error(script::Exception(msg).stacktrace());
2825
}
2926
}
3027

@@ -76,7 +73,6 @@ std::string ValueKindToString(const ValueKind& kind);
7673
catch (const Exception& e) { \
7774
lse::getSelfPluginInstance().getLogger().error(LOG); \
7875
PrintException(e); \
79-
LOG_ERROR_WITH_SCRIPT_INFO(); \
8076
return Local<Value>(); \
8177
} \
8278
catch (...) { \
@@ -112,7 +108,6 @@ std::string ValueKindToString(const ValueKind& kind);
112108
catch (const Exception& e) { \
113109
lse::getSelfPluginInstance().getLogger().error(LOG); \
114110
PrintException(e); \
115-
LOG_ERROR_WITH_SCRIPT_INFO(); \
116111
return nullptr; \
117112
} \
118113
catch (...) { \
@@ -127,7 +122,7 @@ std::string ValueKindToString(const ValueKind& kind);
127122
catch (const Exception& e) { \
128123
lse::getSelfPluginInstance().getLogger().error(LOG); \
129124
PrintException(e); \
130-
LOG_ERROR_WITH_SCRIPT_INFO(); \
125+
return; \
131126
} \
132127
catch (...) { \
133128
lse::getSelfPluginInstance().getLogger().error(LOG); \
@@ -141,12 +136,10 @@ std::string ValueKindToString(const ValueKind& kind);
141136
catch (const Exception& e) { \
142137
lse::getSelfPluginInstance().getLogger().error(LOG); \
143138
PrintException(e); \
144-
LOG_ERROR_WITH_SCRIPT_INFO(); \
145139
} \
146140
catch (...) { \
147141
lse::getSelfPluginInstance().getLogger().error(LOG); \
148142
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
149-
\
150143
LOG_ERROR_WITH_SCRIPT_INFO(); \
151144
}
152145

src/legacy/api/LlAPI.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ ClassDefine<void> LlClassBuilder = defineClass("ll")
3838

3939
// For Compatibility
4040
.function("version", &LlClass::version)
41-
.function("isDebugMode", &LlClass::isDebugModeFunction)
4241
.function("versionStatus", &LlClass::getVersionStatusFunction)
4342
.function("scriptEngineVersion", &LlClass::getScriptEngineVersionFunction)
4443

src/legacy/api/NetworkAPI.cpp

Lines changed: 94 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@
66
#include "ll/api/service/ServerInfo.h"
77
#include "main/SafeGuardRecord.h"
88

9+
#include <ll/api/utils/ErrorUtils.h>
910
#include <string>
1011
#include <vector>
1112

1213
using namespace cyanray;
1314

15+
// Some script::Exception have a problem which can crash the server, and I have no idea, so not output message &
16+
// stacktrace
17+
#define CATCH_CALLBACK(LOG) \
18+
catch (const Exception& e) { \
19+
lse::getSelfPluginInstance().getLogger().error(LOG); \
20+
return; \
21+
} \
22+
catch (...) { \
23+
lse::getSelfPluginInstance().getLogger().error(LOG); \
24+
ll::error_utils::printCurrentException(lse::getSelfPluginInstance().getLogger()); \
25+
LOG_ERROR_WITH_SCRIPT_INFO(); \
26+
return; \
27+
}
28+
1429
//////////////////// Classes ////////////////////
1530

1631
ClassDefine<void> NetworkClassBuilder = defineClass("network")
@@ -153,14 +168,17 @@ void WSClientClass::initListeners_s() {
153168
|| engine->isDestroying())
154169
return;
155170
std::thread([nowList, engine, msg = std::move(msg)]() {
156-
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
157-
|| engine->isDestroying())
158-
return;
159-
EngineScope enter(engine);
160-
if (!nowList->empty())
161-
for (auto& listener : *nowList) {
162-
listener.func.get().call({}, {String::newString(msg)});
163-
}
171+
try {
172+
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
173+
|| engine->isDestroying())
174+
return;
175+
EngineScope enter(engine);
176+
if (!nowList->empty())
177+
for (auto& listener : *nowList) {
178+
listener.func.get().call({}, {String::newString(msg)});
179+
}
180+
}
181+
CATCH_CALLBACK("Fail in OnTextReceived")
164182
}).detach();
165183
});
166184

@@ -170,14 +188,17 @@ void WSClientClass::initListeners_s() {
170188
|| engine->isDestroying())
171189
return;
172190
std::thread([nowList, engine, data = std::move(data)]() mutable {
173-
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
174-
|| engine->isDestroying())
175-
return;
176-
EngineScope enter(engine);
177-
if (!nowList->empty())
178-
for (auto& listener : *nowList) {
179-
listener.func.get().call({}, {ByteBuffer::newByteBuffer(data.data(), data.size())});
180-
}
191+
try {
192+
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
193+
|| engine->isDestroying())
194+
return;
195+
EngineScope enter(engine);
196+
if (!nowList->empty())
197+
for (auto& listener : *nowList) {
198+
listener.func.get().call({}, {ByteBuffer::newByteBuffer(data.data(), data.size())});
199+
}
200+
}
201+
CATCH_CALLBACK("Fail in OnBinaryReceived")
181202
}).detach();
182203
});
183204

@@ -187,14 +208,17 @@ void WSClientClass::initListeners_s() {
187208
|| engine->isDestroying())
188209
return;
189210
std::thread([nowList, engine, msg = std::move(msg)]() {
190-
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
191-
|| engine->isDestroying())
192-
return;
193-
EngineScope enter(engine);
194-
if (!nowList->empty())
195-
for (auto& listener : *nowList) {
196-
listener.func.get().call({}, {String::newString(msg)});
197-
}
211+
try {
212+
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
213+
|| engine->isDestroying())
214+
return;
215+
EngineScope enter(engine);
216+
if (!nowList->empty())
217+
for (auto& listener : *nowList) {
218+
listener.func.get().call({}, {String::newString(msg)});
219+
}
220+
}
221+
CATCH_CALLBACK("Fail in OnError")
198222
}).detach();
199223
});
200224

@@ -204,14 +228,17 @@ void WSClientClass::initListeners_s() {
204228
|| engine->isDestroying())
205229
return;
206230
std::thread([nowList, engine, code]() {
207-
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
208-
|| engine->isDestroying())
209-
return;
210-
EngineScope enter(engine);
211-
if (!nowList->empty())
212-
for (auto& listener : *nowList) {
213-
listener.func.get().call({}, {Number::newNumber(code)});
214-
}
231+
try {
232+
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
233+
|| engine->isDestroying())
234+
return;
235+
EngineScope enter(engine);
236+
if (!nowList->empty())
237+
for (auto& listener : *nowList) {
238+
listener.func.get().call({}, {Number::newNumber(code)});
239+
}
240+
}
241+
CATCH_CALLBACK("Fail in OnLostConnection")
215242
}).detach();
216243
});
217244
}
@@ -289,6 +316,10 @@ Local<Value> WSClientClass::connectAsync(const Arguments& args) {
289316
callback{std::move(callbackFunc)},
290317
engine{EngineScope::currentEngine()},
291318
pluginName{ENGINE_OWN_DATA()->pluginName}]() mutable {
319+
320+
#ifdef NDEBUG
321+
ll::error_utils::setSehTranslator();
322+
#endif
292323
try {
293324
bool result = false;
294325
try {
@@ -397,24 +428,27 @@ using namespace httplib;
397428
|| engine->isDestroying()) \
398429
return; \
399430
std::thread([this, engine, req, &resp] { \
400-
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine) \
401-
|| engine->isDestroying()) \
402-
return; \
403-
EngineScope enter(engine); \
404-
for (auto& [k, v] : this->callbacks) { \
405-
if (v.type != HttpRequestType::method) return; \
406-
std::regex rgx(k); \
407-
std::smatch matches; \
408-
if (std::regex_match(req.path, matches, rgx)) { \
409-
if (matches == req.matches) { \
410-
auto reqObj = new HttpRequestClass(req); \
411-
auto respObj = new HttpResponseClass(resp); \
412-
v.func.get().call({}, reqObj, respObj); \
413-
resp = *respObj->get(); \
414-
break; \
431+
try { \
432+
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine) \
433+
|| engine->isDestroying()) \
434+
return; \
435+
EngineScope enter(engine); \
436+
for (auto& [k, v] : this->callbacks) { \
437+
if (v.type != HttpRequestType::method) return; \
438+
std::regex rgx(k); \
439+
std::smatch matches; \
440+
if (std::regex_match(req.path, matches, rgx)) { \
441+
if (matches == req.matches) { \
442+
auto reqObj = new HttpRequestClass(req); \
443+
auto respObj = new HttpResponseClass(resp); \
444+
v.func.get().call({}, reqObj, respObj); \
445+
resp = *respObj->get(); \
446+
break; \
447+
} \
415448
} \
416449
} \
417450
} \
451+
CATCH_CALLBACK("Fail in NetworkAPI callback") \
418452
}).join(); \
419453
});
420454

@@ -521,17 +555,20 @@ Local<Value> HttpServerClass::onPreRouting(const Arguments& args) {
521555
return Server::HandlerResponse::Unhandled;
522556
bool handled = false;
523557
std::thread([this, engine, req, &resp, &handled] {
524-
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
525-
|| engine->isDestroying())
526-
return;
527-
EngineScope enter(engine);
528-
auto reqObj = new HttpRequestClass(req);
529-
auto respObj = new HttpResponseClass(resp);
530-
auto res = this->preRoutingCallback.func.get().call({}, reqObj, respObj);
531-
if (res.isBoolean() && res.asBoolean().value() == false) {
532-
handled = true;
558+
try {
559+
if ((ll::getServerStatus() != ll::ServerStatus::Running) || !EngineManager::isValid(engine)
560+
|| engine->isDestroying())
561+
return;
562+
EngineScope enter(engine);
563+
auto reqObj = new HttpRequestClass(req);
564+
auto respObj = new HttpResponseClass(resp);
565+
auto res = this->preRoutingCallback.func.get().call({}, reqObj, respObj);
566+
if (res.isBoolean() && res.asBoolean().value() == false) {
567+
handled = true;
568+
}
569+
resp = *respObj->get();
533570
}
534-
resp = *respObj->get();
571+
CATCH_CALLBACK("Fail in onPreRouting");
535572
}).join();
536573
return handled ? Server::HandlerResponse::Handled : Server::HandlerResponse::Unhandled;
537574
});

src/legacy/engine/MessageSystem.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ void MessageSystemLoopOnce() {
345345
}
346346

347347
void InitMessageSystem() {
348+
#ifdef NDEBUG
349+
ll::error_utils::setSehTranslator();
350+
#endif
348351
globalShareData->messageSystemHandlers[LLSE_MODULE_TYPE] = {ModuleMessage::handle, ModuleMessage::cleanup};
349352

350353
ll::event::EventBus::getInstance().emplaceListener<ll::event::ServerStoppingEvent>(

src/lse/Entry.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <ll/api/io/FileUtils.h>
1717
#include <ll/api/plugin/NativePlugin.h>
1818
#include <ll/api/plugin/PluginManagerRegistry.h>
19+
#include <ll/api/utils/ErrorUtils.h>
1920
#include <memory>
2021
#include <stdexcept>
2122

@@ -88,6 +89,9 @@ void initializeLegacyStuff() {
8889

8990
auto load(ll::plugin::NativePlugin& self) -> bool {
9091
auto& logger = self.getLogger();
92+
#ifdef NDEBUG
93+
ll::error_utils::setSehTranslator();
94+
#endif
9195

9296
try {
9397
logger.info("loading...");

0 commit comments

Comments
 (0)