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
1213using 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
1631ClassDefine<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 });
0 commit comments