Add error checking for incorrect files
This commit is contained in:
parent
71d540bc43
commit
31e53661d0
45
src/index.ts
45
src/index.ts
@ -233,7 +233,7 @@ function chooseFile(title, for_save=false) {
|
||||
title: title,
|
||||
defaultPath: <string>store.get("default_dir", app.getPath("documents")),
|
||||
filters: [
|
||||
{name: 'json', extensions: ['json']},
|
||||
{name: 'json', extensions: ['ewe', 'json']},
|
||||
{name: 'All Files', extensions: ['*']}
|
||||
]
|
||||
}
|
||||
@ -271,6 +271,8 @@ function openFile(file_path="", make_new_window=false) {
|
||||
if (window && window.title == "Web Editor - splash") {
|
||||
window.close();
|
||||
}
|
||||
} else {
|
||||
dialog.showErrorBox("Invalid File", `The file you tried to open "${file_path}" is not usable by this application.`)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -334,25 +336,34 @@ function updateMenuBar() {
|
||||
|
||||
function loadJson(file_path) {
|
||||
let file = fs.readFileSync(file_path);
|
||||
return JSON.parse(file.toString());
|
||||
try {
|
||||
return JSON.parse(file.toString());
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function checkFile(file_path) {
|
||||
let json = loadJson(file_path);
|
||||
let result;
|
||||
let valid_result = false;
|
||||
for (let schema_path of all_schemas) {
|
||||
let schema = loadJson(schema_path);
|
||||
result = validator.validate(json, schema);
|
||||
if (result.valid) {
|
||||
let split_schema = schema_path.split(/[\\/]/);
|
||||
result = {"schema": split_schema[split_schema.length-1], "result": result};
|
||||
valid_result = true;
|
||||
break;
|
||||
if (json != null) {
|
||||
let result;
|
||||
let valid_result = false;
|
||||
for (let schema_path of all_schemas) {
|
||||
let schema = loadJson(schema_path);
|
||||
result = validator.validate(json, schema);
|
||||
if (result.valid) {
|
||||
let split_schema = schema_path.split(/[\\/]/);
|
||||
result = {"schema": split_schema[split_schema.length-1], "result": result};
|
||||
valid_result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (valid_result) {
|
||||
return result;
|
||||
if (valid_result) {
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -436,12 +447,12 @@ ipcMain.on("open", (event, file_path="", new_window=false) => {
|
||||
|
||||
ipcMain.on("quit", (event) => {
|
||||
if (BrowserWindow.getAllWindows().length > 1) {
|
||||
BrowserWindow.getFocusedWindow().destroy();
|
||||
BrowserWindow.fromWebContents(event.sender).destroy();
|
||||
} else {
|
||||
app.exit();
|
||||
}
|
||||
})
|
||||
|
||||
ipcMain.on("close-window", (event) => {
|
||||
BrowserWindow.getFocusedWindow().destroy();
|
||||
BrowserWindow.fromWebContents(event.sender).destroy();
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user