Преглед на файлове

Better error handling in PG for JS

David Catuhe преди 5 години
родител
ревизия
f55b1cfd52
променени са 1 файла, в които са добавени 12 реда и са изтрити 8 реда
  1. 12 8
      Playground/js/main.js

+ 12 - 8
Playground/js/main.js

@@ -1,6 +1,7 @@
 var engine = null;
 var canas = null;
 var scene = null;
+var globalParent = null;
 
 handleException = function(parent, e) {
     parent.utils.showError(e.message, e);
@@ -8,15 +9,17 @@ handleException = function(parent, e) {
     console.error(e);
 }
 
-fastEval = function(code, parent) {
+fastEval = function(code) {
     var head = document.getElementsByTagName('head')[0];
     var script = document.createElement('script');
     script.setAttribute('type', 'text/javascript');
-    script.onerror = (message) => {
-        parent.utils.showError(message);
-    }
 
-    script.innerHTML = code;
+    script.innerHTML = `try {
+        ${code};
+    }
+    catch(e) {
+        handleException(globalParent, e);
+    }`;
 
     head.appendChild(script);
 }
@@ -30,6 +33,7 @@ compileAndRun = function(parent, fpsLabel) {
 
     try {
         parent.menuPG.hideWaitDiv();
+        globalParent = parent;
 
         if (!BABYLON.Engine.isSupported()) {
             parent.utils.showError("Your browser does not support WebGL. Please, try to update it, or install a compatible one.", null);
@@ -89,7 +93,7 @@ compileAndRun = function(parent, fpsLabel) {
                 engine = createDefaultEngine();
                 scene = new BABYLON.Scene(engine);
                 var runScript = null;
-                fastEval("runScript = function(scene, canvas) {" + code + "}", parent);
+                fastEval("runScript = function(scene, canvas) {" + code + "}");
                 runScript(scene, canvas);
 
                 parent.zipTool.ZipCode = "var engine = " + defaultEngineZip + ";\r\nvar scene = new BABYLON.Scene(engine);\r\n\r\n" + code;
@@ -104,14 +108,14 @@ compileAndRun = function(parent, fpsLabel) {
                 }
 
                 // Create engine
-                fastEval("engine = " + createEngineFunction + "()", parent);
+                fastEval("engine = " + createEngineFunction + "()");
                 if (!engine) {
                     parent.utils.showError("createEngine function must return an engine.", null);
                     return;
                 }                
 
                 // Execute the code
-                fastEval(code, parent);
+                fastEval(code);
 
                 if (!scene) {
                     parent.utils.showError(createSceneFunction + " function must return a scene.", null);