Skip to content

Commit 7c58e56

Browse files
Merge pull request AToMPM#73 from AToMPM/fix-missing-files
Handle errors with missing files
2 parents 38b8a2d + 8ef79e3 commit 7c58e56

File tree

4 files changed

+192
-17
lines changed

4 files changed

+192
-17
lines changed

client/data_utils.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,29 @@ DataUtils = function(){
273273
*
274274
* @param bm - the button model to load
275275
*/
276-
this.loadbm = function(bm){
277-
HttpUtils.httpReq(
278-
'GET',
279-
HttpUtils.url(bm,true),
280-
undefined,
281-
function(statusCode,resp)
282-
{
283-
GUIUtils.setupAndShowToolbar(
284-
bm,
285-
eval('('+resp+')'),
286-
__BUTTON_TOOLBAR);
287-
});
288-
};
276+
this.loadbm = function (bm) {
277+
HttpUtils.httpReq(
278+
'GET',
279+
HttpUtils.url(bm, true),
280+
undefined,
281+
function (statusCode, resp) {
282+
if (!utils.isHttpSuccessCode(statusCode)) {
283+
284+
if (resp.includes("ENOENT")) {
285+
let err_msg = "Error! File not found: " + bm;
286+
WindowManagement.openDialog(_ERROR, err_msg);
287+
} else {
288+
WindowManagement.openDialog(_ERROR, resp);
289+
}
290+
return;
291+
}
292+
293+
GUIUtils.setupAndShowToolbar(
294+
bm,
295+
eval('(' + resp + ')'),
296+
__BUTTON_TOOLBAR);
297+
});
298+
};
289299

290300
/*
291301
1. does the deed
@@ -327,10 +337,14 @@ DataUtils = function(){
327337
DataUtils.loadm(fname, insert);
328338
}
329339
});
340+
} else {
341+
if (resp.includes("cannot read")) {
342+
let err_msg = "Error! File cannot be read: " + fname;
343+
WindowManagement.openDialog(_ERROR, err_msg);
344+
} else {
345+
WindowManagement.openDialog(_ERROR, resp);
346+
}
330347
}
331-
else
332-
WindowManagement.openDialog(_ERROR, resp);
333-
334348

335349
});
336350
};

client/file_browser.js

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ class FileBrowser{
66
HttpUtils.url('/filelist', __NO_WID),
77
undefined,
88
function (statusCode, resp) {
9+
10+
if (statusCode == 404){
11+
let err_msg = "Error! Cannot load file list!";
12+
WindowManagement.openDialog(_ERROR, err_msg);
13+
return;
14+
}
15+
916
extensions.push('/');
1017
var fnames = __localizeFilenames(
1118
__filterFilenamesByExtension(

httpwsd.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,31 @@ var workerIds2socketIds = {};
3333

3434

3535
/************************************ UTILS ***********************************/
36+
37+
/** Remove invalid characters from a string. **/
38+
function __clean_string(s)
39+
{
40+
if (s == undefined) {
41+
return s;
42+
}
43+
44+
s = JSON.stringify(s);
45+
s = s.replace(/'/g, '');
46+
s = s.replace(/"/g, '');
47+
s = s.replace(//g, '');
48+
s = s.replace(//g, '');
49+
s = s.replace(/\\/g, '\\');
50+
s = s.replace(/\//g, '\/');
51+
s = s.replace(/\\n/g, ' ');
52+
return s;
53+
}
54+
3655
/** Syntactic sugar to build and send HTTP responses **/
3756
function __respond(response, statusCode, reason, data, headers)
3857
{
3958
response.writeHead(
4059
statusCode,
41-
JSON.stringify(reason),
60+
__clean_string(reason),
4261
(headers || {'Content-Type': 'text/plain',
4362
'Access-Control-Allow-Origin': '*'}));
4463

tests/08_missing_files.js

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
let _fs = require('fs');
2+
3+
let deleteFolderRecursive = function (path) {
4+
if (_fs.existsSync(path)) {
5+
_fs.readdirSync(path).forEach(function (file, index) {
6+
let curPath = path + "/" + file;
7+
console.log("Deleting: " + curPath);
8+
9+
if (_fs.lstatSync(curPath).isDirectory()) { // recurse
10+
deleteFolderRecursive(curPath);
11+
} else { // delete file
12+
_fs.unlinkSync(curPath);
13+
}
14+
});
15+
_fs.rmdirSync(path);
16+
}
17+
};
18+
19+
module.exports = {
20+
21+
22+
'Signup user': function (client) {
23+
24+
client.url('http://localhost:8124/atompm').pause(300);
25+
26+
client.execute(
27+
function () {
28+
UserManagement.validateCredentials('userremove', 'test');
29+
}, [], null
30+
);
31+
32+
client.pause(500);
33+
34+
let user_exists = false;
35+
client.getText('div[id=div_login_error]', function (result) {
36+
user_exists = result.value.includes('login failed');
37+
38+
});
39+
40+
if (user_exists == false) {
41+
client.execute(
42+
function () {
43+
UserManagement.signup('userremove', 'test');
44+
}, [], null
45+
);
46+
47+
}
48+
49+
client.pause(500);
50+
51+
client.execute(
52+
function () {
53+
UserManagement.login('userremove');
54+
}, [], null
55+
);
56+
57+
client.pause(500);
58+
client.getTitle(function (title) {
59+
this.assert.ok(title.includes("AToMPM - [Unnamed]"), "AToMPM is opened");
60+
});
61+
62+
},
63+
64+
'Load Missing Toolbar': function (client) {
65+
66+
let filename = './toolbars/missing.metamodel';
67+
client.execute(
68+
function () {
69+
DataUtils.loadbm('./toolbars/missing.metamodel');
70+
}, [], null
71+
);
72+
73+
client.waitForElementPresent("#dialog_btn", 2000, "Check for toolbar loading error: " + filename);
74+
client.element('css selector', '#dialog_btn', function (result) {
75+
if (result.status != -1) {
76+
//Dialog has popped up, so check the text and click the button
77+
client.assert.containsText("#div_dialog_0", "File not found");
78+
client.click("#dialog_btn");
79+
80+
client.verify.ok(true, "Toolbar: " + filename + " failed to load!"); //don't stop testing
81+
}
82+
});
83+
84+
},
85+
86+
'Load Missing Model': function (client) {
87+
88+
let filename = './test/missing.model';
89+
client.execute(
90+
function () {
91+
DataUtils.loadm('./test/missing.model');
92+
}, [], null
93+
);
94+
95+
client.waitForElementPresent("#dialog_btn", 2000, "Check for model loading error: " + filename);
96+
client.element('css selector', '#dialog_btn', function (result) {
97+
if (result.status != -1) {
98+
//Dialog has popped up, so check the text and click the button
99+
client.assert.containsText("#div_dialog_0", "File cannot be read");
100+
client.click("#dialog_btn");
101+
102+
client.verify.ok(true, "Model: " + filename + " failed to load!"); //don't stop testing
103+
}
104+
});
105+
106+
},
107+
108+
'Delete and Click Toolbar': function (client) {
109+
client.pause(500);
110+
deleteFolderRecursive("./users/userremove");
111+
112+
client.pause(1000);
113+
114+
let load_button = "#\\2f Toolbars\\2f MainMenu\\2f MainMenu\\2e buttons\\2e model\\2f loadModel";
115+
client.waitForElementPresent(load_button, 1000, "Looking for load button")
116+
.click(load_button)
117+
.waitForElementPresent("#dialog_btn", 1000, "Load menu opens");
118+
119+
120+
client.waitForElementPresent("#dialog_btn", 2000, "Check for file list loading error");
121+
client.element('css selector', '#dialog_btn', function (result) {
122+
if (result.status != -1) {
123+
//Dialog has popped up, so check the text and click the button
124+
client.assert.containsText("#div_dialog_0", "Cannot load file list");
125+
client.click("#dialog_btn");
126+
127+
client.verify.ok(true, "File list failed to load!"); //don't stop testing
128+
}
129+
});
130+
},
131+
132+
after: function (client) {
133+
client.end();
134+
},
135+
};

0 commit comments

Comments
 (0)