Просмотр исходного кода

Merge pull request #73 from AToMPM/fix-missing-files

Handle errors with missing files
BentleyJOakes 6 лет назад
Родитель
Сommit
7c58e56e97
4 измененных файлов с 192 добавлено и 17 удалено
  1. 30 16
      client/data_utils.js
  2. 7 0
      client/file_browser.js
  3. 20 1
      httpwsd.js
  4. 135 0
      tests/08_missing_files.js

+ 30 - 16
client/data_utils.js

@@ -273,19 +273,29 @@ DataUtils = function(){
 	 * 
 	 * @param bm - the button model to load
 	 */
-	this.loadbm = function(bm){
-		HttpUtils.httpReq(
-				'GET',
-				HttpUtils.url(bm,true),
-				undefined,
-				function(statusCode,resp)
-				{
-					GUIUtils.setupAndShowToolbar(
-						bm,
-						eval('('+resp+')'),
-						__BUTTON_TOOLBAR);					
-				});	
-	};
+    this.loadbm = function (bm) {
+        HttpUtils.httpReq(
+            'GET',
+            HttpUtils.url(bm, true),
+            undefined,
+            function (statusCode, resp) {
+                if (!utils.isHttpSuccessCode(statusCode)) {
+
+                    if (resp.includes("ENOENT")) {
+                        let err_msg = "Error! File not found: " + bm;
+                        WindowManagement.openDialog(_ERROR, err_msg);
+                    } else {
+                        WindowManagement.openDialog(_ERROR, resp);
+                    }
+                    return;
+                }
+
+                GUIUtils.setupAndShowToolbar(
+                    bm,
+                    eval('(' + resp + ')'),
+                    __BUTTON_TOOLBAR);
+            });
+    };
 	
 	/* 
 		1. does the deed
@@ -327,10 +337,14 @@ DataUtils = function(){
                                 DataUtils.loadm(fname, insert);
                             }
                         });
+                } else {
+                    if (resp.includes("cannot read")) {
+                        let err_msg = "Error! File cannot be read: " + fname;
+                        WindowManagement.openDialog(_ERROR, err_msg);
+                    } else {
+                        WindowManagement.openDialog(_ERROR, resp);
+                    }
                 }
-                else
-                    WindowManagement.openDialog(_ERROR, resp);
-
 
             });
     };

+ 7 - 0
client/file_browser.js

@@ -6,6 +6,13 @@ class FileBrowser{
             HttpUtils.url('/filelist', __NO_WID),
             undefined,
             function (statusCode, resp) {
+
+                if (statusCode == 404){
+                    let err_msg = "Error! Cannot load file list!";
+                    WindowManagement.openDialog(_ERROR, err_msg);
+                    return;
+                }
+
                 extensions.push('/');
                 var fnames = __localizeFilenames(
                     __filterFilenamesByExtension(

+ 20 - 1
httpwsd.js

@@ -33,12 +33,31 @@ var workerIds2socketIds = {};
 
 
 /************************************ UTILS ***********************************/
+
+/** Remove invalid characters from a string. **/
+function __clean_string(s)
+{
+	if (s == undefined) {
+        return s;
+    }
+
+	s = JSON.stringify(s);
+	s = s.replace(/'/g, '');
+	s = s.replace(/"/g, '');
+	s = s.replace(/‘/g, '');
+	s = s.replace(/’/g, '');
+	s = s.replace(/\\/g, '\\');
+	s = s.replace(/\//g, '\/');
+	s = s.replace(/\\n/g, ' ');
+	return s;
+}
+
 /** Syntactic sugar to build and send HTTP responses **/
 function __respond(response, statusCode, reason, data, headers)
 {
 	response.writeHead(
 			statusCode,
-			JSON.stringify(reason),
+			__clean_string(reason),
 			(headers || {'Content-Type': 'text/plain',
 			'Access-Control-Allow-Origin': '*'}));
 

+ 135 - 0
tests/08_missing_files.js

@@ -0,0 +1,135 @@
+let _fs = require('fs');
+
+let deleteFolderRecursive = function (path) {
+    if (_fs.existsSync(path)) {
+        _fs.readdirSync(path).forEach(function (file, index) {
+            let curPath = path + "/" + file;
+            console.log("Deleting: " + curPath);
+
+            if (_fs.lstatSync(curPath).isDirectory()) { // recurse
+                deleteFolderRecursive(curPath);
+            } else { // delete file
+                _fs.unlinkSync(curPath);
+            }
+        });
+        _fs.rmdirSync(path);
+    }
+};
+
+module.exports = {
+
+
+    'Signup user': function (client) {
+
+        client.url('http://localhost:8124/atompm').pause(300);
+
+        client.execute(
+            function () {
+                UserManagement.validateCredentials('userremove', 'test');
+            }, [], null
+        );
+
+        client.pause(500);
+
+        let user_exists = false;
+        client.getText('div[id=div_login_error]', function (result) {
+            user_exists = result.value.includes('login failed');
+
+        });
+
+        if (user_exists == false) {
+            client.execute(
+                function () {
+                    UserManagement.signup('userremove', 'test');
+                }, [], null
+            );
+
+        }
+
+        client.pause(500);
+
+        client.execute(
+            function () {
+                UserManagement.login('userremove');
+            }, [], null
+        );
+
+        client.pause(500);
+        client.getTitle(function (title) {
+            this.assert.ok(title.includes("AToMPM - [Unnamed]"), "AToMPM is opened");
+        });
+
+    },
+
+    'Load Missing Toolbar': function (client) {
+
+        let filename = './toolbars/missing.metamodel';
+        client.execute(
+            function () {
+                DataUtils.loadbm('./toolbars/missing.metamodel');
+            }, [], null
+        );
+
+        client.waitForElementPresent("#dialog_btn", 2000, "Check for toolbar loading error: " + filename);
+        client.element('css selector', '#dialog_btn', function (result) {
+            if (result.status != -1) {
+                //Dialog has popped up, so check the text and click the button
+                client.assert.containsText("#div_dialog_0", "File not found");
+                client.click("#dialog_btn");
+
+                client.verify.ok(true, "Toolbar: " + filename + " failed to load!"); //don't stop testing
+            }
+        });
+
+    },
+
+    'Load Missing Model': function (client) {
+
+        let filename = './test/missing.model';
+        client.execute(
+            function () {
+                DataUtils.loadm('./test/missing.model');
+            }, [], null
+        );
+
+        client.waitForElementPresent("#dialog_btn", 2000, "Check for model loading error: " + filename);
+        client.element('css selector', '#dialog_btn', function (result) {
+            if (result.status != -1) {
+                //Dialog has popped up, so check the text and click the button
+                client.assert.containsText("#div_dialog_0", "File cannot be read");
+                client.click("#dialog_btn");
+
+                client.verify.ok(true, "Model: " + filename + " failed to load!"); //don't stop testing
+            }
+        });
+
+    },
+
+    'Delete and Click Toolbar': function (client) {
+        client.pause(500);
+        deleteFolderRecursive("./users/userremove");
+
+        client.pause(1000);
+
+        let load_button = "#\\2f Toolbars\\2f MainMenu\\2f MainMenu\\2e buttons\\2e model\\2f loadModel";
+        client.waitForElementPresent(load_button, 1000, "Looking for load button")
+            .click(load_button)
+            .waitForElementPresent("#dialog_btn", 1000, "Load menu opens");
+
+
+        client.waitForElementPresent("#dialog_btn", 2000, "Check for file list loading error");
+        client.element('css selector', '#dialog_btn', function (result) {
+            if (result.status != -1) {
+                //Dialog has popped up, so check the text and click the button
+                client.assert.containsText("#div_dialog_0", "Cannot load file list");
+                client.click("#dialog_btn");
+
+                client.verify.ok(true, "File list failed to load!"); //don't stop testing
+            }
+        });
+    },
+
+    after: function (client) {
+        client.end();
+    },
+};