Skip to content

Commit

Permalink
QtScripting: log also errors happening in JS lambda functions
Browse files Browse the repository at this point in the history
  • Loading branch information
HuguesDelorme committed Jul 25, 2024
1 parent be914b3 commit c9dc3c1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ set(CMAKE_CXX_STANDARD 17)

set(
Mayo_CompileDefinitions
# Disable Qt functions deprecated in version 5.15 and earlier
QT_DISABLE_DEPRECATED_BEFORE=0x050F00
# Make QFileInfo constructors implicit instead of explicit
QT_IMPLICIT_QFILEINFO_CONSTRUCTION
# Force Qt to provide information about source code location for qDebug, qInfo(), ...
QT_MESSAGELOGCONTEXT
)
set(Mayo_CompileOptions)

Expand Down
11 changes: 1 addition & 10 deletions src/app/dialog_exec_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,7 @@ void DialogExecScript::startScript()
// Evaluate script program
this->recreateScriptEngine();
auto jsVal = m_jsEngine->evaluate(scriptProgram(m_scriptFilePath), m_scriptFilePath);
if (jsVal.isError()) {
const QString name = jsVal.property("name").toString();
const QString message = jsVal.property("message").toString();
fnAddConsoleOutput(
QtCriticalMsg,
tr("%1: %2").arg(name, message),
jsVal.property("fileName").toString(),
jsVal.property("lineNumber").toInt()
);
}
logScriptError(jsVal);
});
m_taskMgr.run(m_scriptExecTaskId);
}
Expand Down
9 changes: 6 additions & 3 deletions src/qtscripting/script_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include "../qtcommon/qstring_conv.h"
#include "../app/qtgui_utils.h"
#include "script_application.h"
#include "script_global.h"
#include "script_shape.h"
#include "script_tree_node.h"

#include <TDF_Tool.hxx>

#include <QtQml/QJSEngine>
#include <QtCore/QtDebug>
#include <QtQml/QJSEngine>

namespace Mayo {

Expand Down Expand Up @@ -68,7 +69,8 @@ void ScriptDocument::traverseModelTree(QJSValue fn)
return; // Skip: tree node is a product(or "referred" shape)
}
#endif
fn.call({ QJSValue{nodeId} });
auto jsVal = fn.call({ QJSValue{nodeId} });
logScriptError(jsVal, "traverseModelTree()");
});
}

Expand Down Expand Up @@ -99,7 +101,8 @@ void ScriptDocument::traverseShape(QJSValue shape, unsigned shapeTypeFilter, QJS
const auto scriptShape = m_jsApp->jsEngine()->fromScriptValue<ScriptShape>(shape);
BRepUtils::forEachSubShape(scriptShape.shape(), shapeTypeEnum, [&](const TopoDS_Shape& subShape) {
auto jsSubShape = m_jsApp->jsEngine()->toScriptValue(ScriptShape(subShape));
fn.call({ jsSubShape });
auto jsVal = fn.call({ jsSubShape });
logScriptError(jsVal, "traverseShape()");
});
}

Expand Down
14 changes: 14 additions & 0 deletions src/qtscripting/script_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "../qtcommon/qstring_conv.h"
#include "script_application.h"

#include <QtCore/QMessageLogger>
#include <QtQml/QJSEngine>
#include <QtQml/QJSValue>
#include <GeomAbs_BSplKnotDistribution.hxx>
#include <GeomAbs_CurveType.hxx>
#include <GeomAbs_Shape.hxx>
Expand Down Expand Up @@ -133,4 +135,16 @@ QJSEngine* createScriptEngine(const ApplicationPtr& app, QObject* parent)
return jsEngine;
}

void logScriptError(const QJSValue& jsVal, const char* functionName)
{
if (jsVal.isError()) {
const QByteArray name = jsVal.property("name").toString().toUtf8();
const QByteArray message = jsVal.property("message").toString().toUtf8();
const QByteArray fileName = jsVal.property("fileName").toString().toUtf8();
const int lineNumber = jsVal.property("lineNumber").toInt();
const QMessageLogger msgLogger(fileName.constData(), lineNumber, functionName, "js");
msgLogger.critical("%s: %s", name.constData(), message.constData());
}
}

} // namespace Mayo
2 changes: 2 additions & 0 deletions src/qtscripting/script_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include "../base/application_ptr.h"

class QJSEngine;
class QJSValue;
class QObject;

namespace Mayo {

class Application;

QJSEngine* createScriptEngine(const ApplicationPtr& app, QObject* parent = nullptr);
void logScriptError(const QJSValue& jsVal, const char* functionName = nullptr);

} // namespace Mayo

0 comments on commit c9dc3c1

Please sign in to comment.