Skip to content

Commit 6c0c5fd

Browse files
author
Matt Karl
committed
Better error handling for JSON errors
1 parent f43b65b commit 6c0c5fd

File tree

1 file changed

+99
-36
lines changed

1 file changed

+99
-36
lines changed

src/springroll/captions/data/Project.js

Lines changed: 99 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
(function(undefined){
22

3+
if (APP)
4+
{
5+
var fs = require('fs');
6+
var path = require('path');
7+
var glob = require('glob');
8+
}
9+
310
/**
411
* Class for managing the project data
512
* @class Project
@@ -74,32 +81,34 @@
7481
{
7582
this.close();
7683
this.dir = dir;
77-
var self = this;
7884

7985
if (APP)
8086
{
81-
var path = require('path');
82-
var fs = require('fs');
87+
var data;
8388

8489
// Convert the old project file to the new format
8590
var oldFile = path.join(dir, OLD_PROJECT_FILE);
8691
var file = path.join(dir, PROJECT_FILE);
8792

93+
// backward compatibility with old project file
8894
if (fs.existsSync(oldFile))
8995
{
90-
// Read in the project json
91-
var data = JSON.parse(fs.readFileSync(oldFile));
96+
data = parseJSON(oldFile);
97+
98+
if (!data)
99+
{
100+
callback(null);
101+
return;
102+
}
92103

93104
// Write the new project JSON file
94-
fs.writeFileSync(
95-
file,
96-
JSON.stringify({ captions: data }, null, "\t")
97-
);
105+
writeJSON(file, { captions: data });
98106

99107
// Remove the old project file
100108
fs.unlinkSync(oldFile);
101109
}
102110

111+
// Check for a project file that doesn't exist
103112
if (!fs.existsSync(file))
104113
{
105114
if (DEBUG)
@@ -110,16 +119,15 @@
110119
this.create(file, callback);
111120
return;
112121
}
113-
fs.readFile(file, function(err, data){
114122

115-
if (err)
116-
{
117-
callback(null);
118-
throw err;
119-
}
120-
// Finish the loading
121-
self.onProjectLoaded(JSON.parse(data), callback);
122-
});
123+
data = parseJSON(file);
124+
125+
if (!data)
126+
{
127+
callback(null);
128+
return;
129+
}
130+
this.onProjectLoaded(data, callback);
123131
}
124132
if (WEB)
125133
{
@@ -128,12 +136,63 @@
128136
dir + "/"+PROJECT_FILE+"?cb=" + Math.random() * 10000,
129137
function(data)
130138
{
131-
self.onProjectLoaded(data, callback);
132-
}
139+
this.onProjectLoaded(data, callback);
140+
}.bind(this)
133141
);
134142
}
135143
};
136144

145+
/**
146+
* Handle the parse with an alert
147+
* @method parseJSON
148+
* @static
149+
* @private
150+
* @param {string} str The JSON string
151+
* @param {string} [create] Default contents if file doesn't exist
152+
* @return {object} The JSON object or null
153+
*/
154+
var parseJSON = function(file, create)
155+
{
156+
if (!fs.existsSync(file))
157+
{
158+
if (create)
159+
{
160+
fs.writeFileSync(file, create);
161+
}
162+
else
163+
{
164+
alert("File " + file + " does not exist. Manually create it.");
165+
return;
166+
}
167+
}
168+
var str = fs.readFileSync(file);
169+
var data;
170+
try
171+
{
172+
data = JSON.parse(str);
173+
}
174+
catch(e)
175+
{
176+
alert("Unable to parse " + file +
177+
". Manually fix JSON errors and try again.");
178+
return;
179+
}
180+
return data;
181+
};
182+
183+
/**
184+
* Write the JSON
185+
* @method writeJSON
186+
* @static
187+
* @private
188+
* @param {string} file Output file location
189+
* @param {*} data The output data
190+
*/
191+
var writeJSON = function(file, data)
192+
{
193+
fs.writeFileSync(file, JSON.stringify(data, null, "\t"));
194+
};
195+
137196
/**
138197
* The project is loadd
139198
* @method onProjectLoaded
@@ -164,10 +223,7 @@
164223
*/
165224
p.create = function(file, callback)
166225
{
167-
var fs = require('fs'),
168-
path = require('path'),
169-
glob = require('glob'),
170-
self = this,
226+
var self = this,
171227
dir = path.dirname(file);
172228

173229
this.modal.open(dir, function(result){
@@ -226,7 +282,7 @@
226282
};
227283

228284
// Write the project to file
229-
fs.writeFileSync(file, JSON.stringify(data, null, "\t"));
285+
writeJSON(file, data);
230286

231287
// Continue with loading the new created project
232288
self.onProjectLoaded(data, callback);
@@ -259,10 +315,15 @@
259315
var exportPath;
260316
if (APP)
261317
{
262-
var path = require('path');
263-
var fs = require('fs');
264318
exportPath = path.join(this.dir, locale.export);
265-
this.captions = JSON.parse(fs.readFileSync(exportPath) || '{}');
319+
320+
this.captions = parseJSON(exportPath, '{}');
321+
322+
if (!this.captions)
323+
{
324+
callback(null);
325+
return;
326+
}
266327
callback(this);
267328
}
268329
if (WEB)
@@ -289,24 +350,26 @@
289350

290351
if (APP)
291352
{
292-
var fs = require('fs');
293-
var path = require('path');
294353
var projectFile = path.join(this.dir, PROJECT_FILE);
295354
var exportFile = path.join(this.dir, locale.export);
296355

297356
// Re-export the captions file
298-
fs.writeFileSync(
299-
exportFile,
300-
JSON.stringify(this.captions, null, "\t")
301-
);
357+
writeJSON(exportFile, this.captions);
302358

303359
// Get the project JSON file and update it
304-
var project = JSON.parse(fs.readFileSync(projectFile));
360+
var project = parseJSON(projectFile);
361+
362+
// Ignore if invalid project JSON
363+
if (!project)
364+
{
365+
return;
366+
}
367+
305368
project.captions.locales = this.locales;
306369
project.captions.assets = this.assets;
307370

308371
// Update the project file
309-
fs.writeFileSync(projectFile, JSON.stringify(project, null, "\t"));
372+
writeJSON(projectFile, project);
310373
}
311374

312375
if (WEB && DEBUG)

0 commit comments

Comments
 (0)