Add runtime error checking for custom JS

This commit is contained in:
Jack Bond-Preston 2016-08-04 18:16:09 +01:00
parent a1c50b253d
commit 1b2ea3c6bf
3 changed files with 27 additions and 8 deletions

View File

@ -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) {
if (message == "") {
Logger::write("[send_message] Tried to send empty message", Logger::LogLevel::Warning);
}
const std::string url = CHANNELS_URL + "/" + channel_id + "/messages";
json data = {
{ "content", message }

View File

@ -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_close(db);

View File

@ -67,22 +67,37 @@ void V8Instance::exec_js(std::string js, std::string channel_id) {
// compile
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;
if (!Script::Compile(context, source).ToLocal(&script)) {
String::Utf8Value error(try_catch.Exception());
Logger::write("[v8] Compilation error: " + std::string((const char *) *error), Logger::LogLevel::Debug);
String::Utf8Value error(compile_try_catch.Exception());
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;
}
// run
script->Run(context);
Logger::write("[v8] Script compiled and run", Logger::LogLevel::Debug);
if (print_text != "") {
ah->send_message(channel_id, print_text);
print_text = "";
}
}
void V8Instance::js_print(const v8::FunctionCallbackInfo<v8::Value> &args) {