From 62d545d4f94661c5dd381022ce89c94b4b864384 Mon Sep 17 00:00:00 2001 From: Eric Tuvesson Date: Thu, 9 May 2024 14:09:37 +0200 Subject: [PATCH] Fix: Javascript node error logs (#14) In some cases the error is a string, and when logging it will write "undefined" into the console instead of providing a useful error message. --- .../src/nodes/std-library/simplejavascript.js | 8 +++----- packages/noodl-runtime/src/utils.js | 20 ++++++++++++++++++- .../src/nodes/std-library/javascript.js | 9 +++++---- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/noodl-runtime/src/nodes/std-library/simplejavascript.js b/packages/noodl-runtime/src/nodes/std-library/simplejavascript.js index 8f32035..96dea3d 100644 --- a/packages/noodl-runtime/src/nodes/std-library/simplejavascript.js +++ b/packages/noodl-runtime/src/nodes/std-library/simplejavascript.js @@ -1,4 +1,5 @@ const JavascriptNodeParser = require('../../javascriptnodeparser'); +const { logJavaScriptNodeError } = require('../../utils'); const SimpleJavascriptNode = { name: 'JavaScriptFunction', @@ -139,11 +140,8 @@ const SimpleJavascriptNode = { JavascriptNodeParser.getComponentScopeForNode(this) ]); } catch (e) { - console.log( - 'Error in JS node run code.', - Object.getPrototypeOf(e).constructor.name + ': ' + e.message, - e.stack - ); + logJavaScriptNodeError(e); + if (this.context.editorConnection && this.context.isWarningTypeEnabled('javascriptExecution')) { this.context.editorConnection.sendWarning( this.nodeScope.componentOwner.name, diff --git a/packages/noodl-runtime/src/utils.js b/packages/noodl-runtime/src/utils.js index cca2efd..c767ac7 100644 --- a/packages/noodl-runtime/src/utils.js +++ b/packages/noodl-runtime/src/utils.js @@ -12,6 +12,24 @@ function getAbsoluteUrl(_url) { return (Noodl.baseUrl || '/') + url; } +/** + * Log an error thrown by the JavaScript nodes. + * + * @param {any} error + */ +function logJavaScriptNodeError(error) { + if (typeof error === 'string') { + console.log('Error in JS node run code.', error); + } else { + console.log( + 'Error in JS node run code.', + Object.getPrototypeOf(error).constructor.name + ': ' + error.message, + error.stack + ); + } +} + module.exports = { - getAbsoluteUrl + getAbsoluteUrl, + logJavaScriptNodeError }; diff --git a/packages/noodl-viewer-react/src/nodes/std-library/javascript.js b/packages/noodl-viewer-react/src/nodes/std-library/javascript.js index 0dd33cc..72a7d85 100644 --- a/packages/noodl-viewer-react/src/nodes/std-library/javascript.js +++ b/packages/noodl-viewer-react/src/nodes/std-library/javascript.js @@ -1,8 +1,8 @@ 'use strict'; -const Node = require('@noodl/runtime').Node, - JavascriptNodeParser = require('@noodl/runtime/src/javascriptnodeparser'); - +const Node = require('@noodl/runtime').Node; +const JavascriptNodeParser = require('@noodl/runtime/src/javascriptnodeparser'); +const { logJavaScriptNodeError } = require('@noodl/runtime/src/utils'); const guid = require('../../guid'); /*const defaultCode = "define({\n"+ @@ -372,7 +372,8 @@ var Javascript = { internal.changedInputs ); } catch (e) { - console.log('Error in JS node run code.', Object.getPrototypeOf(e).constructor.name + ': ' + e.message); + logJavaScriptNodeError(e); + if (this.context.editorConnection && this.context.isWarningTypeEnabled('javascriptExecution')) { this.context.editorConnection.sendWarning(this.nodeScope.componentOwner.name, this.id, 'js-run-waring', { showGlobally: true,