1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-23 00:40:10 +00:00
HackMyResume/dist/utils/safe-json-loader.js

33 lines
686 B
JavaScript
Raw Permalink Normal View History

2016-01-27 10:29:26 +00:00
/**
Definition of the SafeJsonLoader class.
@module utils/safe-json-loader
@license MIT. See LICENSE.md for details.
*/
(function() {
var FS, SyntaxErrorEx;
FS = require('fs');
SyntaxErrorEx = require('./syntax-error-ex');
module.exports = function(file) {
var ret, retRaw;
ret = {};
try {
ret.raw = FS.readFileSync(file, 'utf8');
ret.json = JSON.parse(ret.raw);
} catch (_error) {
retRaw = ret.raw && ret.raw.trim();
ret.ex = {
operation: retRaw ? 'parse' : 'read',
inner: SyntaxErrorEx.is(_error) ? new SyntaxErrorEx(_error, retRaw) : _error,
file: file
};
}
return ret;
};
}).call(this);