Log more script errors

This commit is contained in:
GriffinR 2021-11-19 22:32:14 -05:00 committed by huderlem
parent a6b490cf15
commit beb1f6ef27
4 changed files with 21 additions and 23 deletions

View file

@ -72,7 +72,7 @@ The grass-randomizer script above happens implicitly when the user paints on the
.. code-block:: js .. code-block:: js
function applyNightTint() { export function applyNightTint() {
// Apply night palette tinting... // Apply night palette tinting...
} }
@ -556,7 +556,7 @@ These are some miscellaneous functions that can be very useful when building cus
.. js:function:: map.registerAction(functionName, actionName, shortcut = "") .. js:function:: map.registerAction(functionName, actionName, shortcut = "")
Registers a JavaScript function to an action that can be manually triggered in Porymap's ``Tools`` menu. Optionally, a keyboard shortcut (e.g. ``"Ctrl+P"``) can also be specified, assuming it doesn't collide with any existing shortcuts used by Porymap. Registers a JavaScript function to an action that can be manually triggered in Porymap's ``Tools`` menu. Optionally, a keyboard shortcut (e.g. ``"Ctrl+P"``) can also be specified, assuming it doesn't collide with any existing shortcuts used by Porymap. The function specified by ``functionName`` must have the ``export`` keyword.
:param string functionName: name of the JavaScript function :param string functionName: name of the JavaScript function
:param string actionName: name of the action that will be displayed in the ``Tools`` menu :param string actionName: name of the action that will be displayed in the ``Tools`` menu

View file

@ -30,6 +30,7 @@ public:
static void cb_ProjectClosed(QString projectPath); static void cb_ProjectClosed(QString projectPath);
static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock); static void cb_MetatileChanged(int x, int y, Block prevBlock, Block newBlock);
static void cb_MapOpened(QString mapName); static void cb_MapOpened(QString mapName);
static bool tryErrorJS(QJSValue js);
private: private:
QJSEngine *engine; QJSEngine *engine;

View file

@ -507,7 +507,7 @@ void MainWindow::setTimeout(QJSValue callback, int milliseconds) {
} }
void MainWindow::invokeCallback(QJSValue callback) { void MainWindow::invokeCallback(QJSValue callback) {
callback.call(); Scripting::tryErrorJS(callback.call());
} }
void MainWindow::log(QString message) { void MainWindow::log(QString message) {

View file

@ -51,23 +51,25 @@ void Scripting::loadModules(QStringList moduleFiles) {
} }
} }
bool Scripting::tryErrorJS(QJSValue js) {
if (!js.isError()) return false;
QFileInfo file(js.property("fileName").toString());
logError(QString("Error in custom script '%1' at line %2: '%3'")
.arg(file.fileName())
.arg(js.property("lineNumber").toString())
.arg(js.toString()));
return true;
}
void Scripting::invokeCallback(CallbackType type, QJSValueList args) { void Scripting::invokeCallback(CallbackType type, QJSValueList args) {
for (QJSValue module : this->modules) { for (QJSValue module : this->modules) {
QString functionName = callbackFunctions[type]; QString functionName = callbackFunctions[type];
QJSValue callbackFunction = module.property(functionName); QJSValue callbackFunction = module.property(functionName);
if (callbackFunction.isError()) { if (tryErrorJS(callbackFunction)) continue;
continue;
}
QJSValue result = callbackFunction.call(args); QJSValue result = callbackFunction.call(args);
if (result.isError()) { if (tryErrorJS(result)) continue;
QFileInfo file(result.property("fileName").toString());
logError(QString("Error in custom script '%1' at line %2: '%3'")
.arg(file.fileName())
.arg(result.property("lineNumber").toString())
.arg(result.toString()));
continue;
}
} }
} }
@ -88,19 +90,14 @@ void Scripting::invokeAction(QString actionName) {
QString functionName = instance->registeredActions.value(actionName); QString functionName = instance->registeredActions.value(actionName);
for (QJSValue module : instance->modules) { for (QJSValue module : instance->modules) {
QJSValue callbackFunction = module.property(functionName); QJSValue callbackFunction = module.property(functionName);
if (callbackFunction.isError()) { if (callbackFunction.isUndefined() || !callbackFunction.isCallable()) {
logError(QString("Unknown custom script function '%1'").arg(functionName));
continue; continue;
} }
if (tryErrorJS(callbackFunction)) continue;
QJSValue result = callbackFunction.call(QJSValueList()); QJSValue result = callbackFunction.call(QJSValueList());
if (result.isError()) { if (tryErrorJS(result)) continue;
QFileInfo file(result.property("fileName").toString());
logError(QString("Error in custom script '%1' at line %2: '%3'")
.arg(file.fileName())
.arg(result.property("lineNumber").toString())
.arg(result.toString()));
continue;
}
} }
} }