Add runtime error checking for custom JS
This commit is contained in:
parent
a1c50b253d
commit
1b2ea3c6bf
@ -15,6 +15,10 @@ APIHelper::APIHelper() : BASE_URL("https://discordapp.com/api"), CHANNELS_URL(BA
|
|||||||
}
|
}
|
||||||
|
|
||||||
void APIHelper::send_message(std::string channel_id, std::string message) {
|
void APIHelper::send_message(std::string channel_id, std::string message) {
|
||||||
|
if (message == "") {
|
||||||
|
Logger::write("[send_message] Tried to send empty message", Logger::LogLevel::Warning);
|
||||||
|
}
|
||||||
|
|
||||||
const std::string url = CHANNELS_URL + "/" + channel_id + "/messages";
|
const std::string url = CHANNELS_URL + "/" + channel_id + "/messages";
|
||||||
json data = {
|
json data = {
|
||||||
{ "content", message }
|
{ "content", message }
|
||||||
|
@ -34,7 +34,7 @@ CommandHelper::CommandHelper() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::write(std::to_string(commands.size()) + " custom command loaded", Logger::LogLevel::Info);
|
Logger::write(std::to_string(commands.size()) + " custom command(s) loaded", Logger::LogLevel::Info);
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
|
@ -67,22 +67,37 @@ void V8Instance::exec_js(std::string js, std::string channel_id) {
|
|||||||
// compile
|
// compile
|
||||||
Logger::write("[v8] Isolate nullptr? " + std::to_string(isolate == nullptr) + " Context empty? " + std::to_string(context.IsEmpty()), Logger::LogLevel::Debug);
|
Logger::write("[v8] Isolate nullptr? " + std::to_string(isolate == nullptr) + " Context empty? " + std::to_string(context.IsEmpty()), Logger::LogLevel::Debug);
|
||||||
|
|
||||||
TryCatch try_catch(isolate);
|
TryCatch compile_try_catch(isolate);
|
||||||
Local<Script> script;
|
Local<Script> script;
|
||||||
|
|
||||||
if (!Script::Compile(context, source).ToLocal(&script)) {
|
if (!Script::Compile(context, source).ToLocal(&script)) {
|
||||||
String::Utf8Value error(try_catch.Exception());
|
String::Utf8Value error(compile_try_catch.Exception());
|
||||||
Logger::write("[v8] Compilation error: " + std::string((const char *) *error), Logger::LogLevel::Debug);
|
|
||||||
|
std::string err_msg = *error;
|
||||||
|
Logger::write("[v8] Compilation error: " + err_msg, Logger::LogLevel::Debug);
|
||||||
|
ah->send_message(channel_id, ":warning: **Compilation error:** `" + err_msg + "`");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
TryCatch run_try_catch(isolate);
|
||||||
|
MaybeLocal<Value> v = script->Run(context);
|
||||||
|
if (v.IsEmpty()) {
|
||||||
|
String::Utf8Value error(run_try_catch.Exception());
|
||||||
|
|
||||||
|
std::string err_msg = *error;
|
||||||
|
Logger::write("[v8] Runtime error: " + err_msg, Logger::LogLevel::Debug);
|
||||||
|
ah->send_message(channel_id, ":warning: **Runtime error:** `" + err_msg + "`");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// run
|
|
||||||
script->Run(context);
|
|
||||||
Logger::write("[v8] Script compiled and run", Logger::LogLevel::Debug);
|
Logger::write("[v8] Script compiled and run", Logger::LogLevel::Debug);
|
||||||
|
|
||||||
|
if (print_text != "") {
|
||||||
ah->send_message(channel_id, print_text);
|
ah->send_message(channel_id, print_text);
|
||||||
print_text = "";
|
print_text = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void V8Instance::js_print(const v8::FunctionCallbackInfo<v8::Value> &args) {
|
void V8Instance::js_print(const v8::FunctionCallbackInfo<v8::Value> &args) {
|
||||||
|
Loading…
Reference in New Issue
Block a user