From 59d1c2a0b37157e5ce22c0ea6ea94e1fb0a96fff Mon Sep 17 00:00:00 2001
From: Karishma Chadha <kchadha@scratch.mit.edu>
Date: Wed, 28 Feb 2018 10:02:02 -0500
Subject: [PATCH] Code cleanup and actually throw errors when project fails to
 load. Depends on scratch-gui loading sb3 version of default project.

---
 src/virtual-machine.js | 53 +++++++-----------------------------------
 1 file changed, 8 insertions(+), 45 deletions(-)

diff --git a/src/virtual-machine.js b/src/virtual-machine.js
index 43aa0561d..4734623c6 100644
--- a/src/virtual-machine.js
+++ b/src/virtual-machine.js
@@ -228,8 +228,9 @@ class VirtualMachine extends EventEmitter {
 
         // Validate & parse
         if (typeof json !== 'string' && typeof json !== 'object') {
-            log.error('Failed to parse project. Invalid type supplied to fromJSON.');
-            return;
+            const invalidTypeError = 'Failed to parse project. Invalid type supplied to fromJSON.';
+            log.error(invalidTypeError);
+            throw new Error(invalidTypeError);
         }
 
         // Establish version, deserialize, and load into runtime
@@ -244,19 +245,17 @@ class VirtualMachine extends EventEmitter {
             deserializer = sb3;
             validatedProject = possibleSb3;
         } else {
-            // @todo need to handle default project
             validate(json, (err, project) => {
                 if (err) {
-                    // @todo Making this a warning for now. Should be updated with error handling
-                    log.warn(
-                        `There was an error in validating the project: ${JSON.stringify(err)}`);
-                    deserializer = sb2;
-                    validatedProject = possibleSb3;
+                    const errorMessage =
+                        `The given project could not be validated, parsing failed with error: ${JSON.stringify(err)}`;
+                    log.error(errorMessage);
+                    throw new Error(errorMessage);
+
                 } else {
                     deserializer = sb2;
                     validatedProject = project;
                 }
-                // handle the error
             });
         }
 
@@ -265,42 +264,6 @@ class VirtualMachine extends EventEmitter {
                 this.installTargets(targets, extensions, true));
     }
 
-
-    // /**
-    //  * Load a project from a Scratch JSON representation.
-    //  * @param {string} json JSON string representing a project.
-    //  * @returns {Promise} Promise that resolves after the project has loaded
-    //  */
-    // fromJSON (json) {
-    //     // Clear the current runtime
-    //     this.clear();
-    //
-    //     // Validate & parse
-    //     if (typeof json !== 'string' && typeof json !== 'object') {
-    //         log.error('Failed to parse project. Invalid type supplied to fromJSON.');
-    //         return;
-    //     }
-    //
-    //     // Attempt to parse JSON if string is supplied
-    //     if (typeof json === 'string') json = JSON.parse(json);
-    //
-    //     // Establish version, deserialize, and load into runtime
-    //     // @todo Support Scratch 1.4
-    //     // @todo This is an extremely naïve / dangerous way of determining version.
-    //     //       See `scratch-parser` for a more sophisticated validation
-    //     //       methodology that should be adapted for use here
-    //     let deserializer;
-    //     if ((typeof json.meta !== 'undefined') && (typeof json.meta.semver !== 'undefined')) {
-    //         deserializer = sb3;
-    //     } else {
-    //         deserializer = sb2;
-    //     }
-    //
-    //     return deserializer.deserialize(json, this.runtime)
-    //         .then(({targets, extensions}) =>
-    //             this.installTargets(targets, extensions, true));
-    // }
-
     /**
      * Install `deserialize` results: zero or more targets after the extensions (if any) used by those targets.
      * @param {Array.<Target>} targets - the targets to be installed