mirror of
https://github.com/JuanCanham/HackMyResume.git
synced 2025-05-04 05:17:08 +01:00
Catch JSON syntax errors for all commands.
...and emit line/column info.
This commit is contained in:
@ -32,6 +32,12 @@ Error-handling routines for HackMyResume.
|
||||
err: function( ex, shouldExit ) {
|
||||
var msg = '', exitCode;
|
||||
|
||||
if( ex.handled ) {
|
||||
if( shouldExit )
|
||||
process.exit( exitCode );
|
||||
return;
|
||||
}
|
||||
|
||||
if( ex.fluenterror ){
|
||||
switch( ex.fluenterror ) { // TODO: Remove magic numbers
|
||||
|
||||
|
@ -14,6 +14,7 @@ Definition of the ResumeFactory class.
|
||||
var FS = require('fs');
|
||||
var ResumeConverter = require('./convert');
|
||||
var chalk = require('chalk');
|
||||
var SyntaxErrorEx = require('../utils/syntax-error-ex');
|
||||
|
||||
|
||||
|
||||
@ -28,12 +29,13 @@ Definition of the ResumeFactory class.
|
||||
/**
|
||||
Load one or more resumes from disk.
|
||||
*/
|
||||
load: function ( sources, log, toFormat, objectify ) {
|
||||
load: function ( sources, opts ) {
|
||||
|
||||
// Loop over all inputs, parsing each to JSON and then to a FRESHResume
|
||||
// or JRSResume object.
|
||||
var that = this;
|
||||
return sources.map( function( src ) {
|
||||
return that.loadOne( src, log, toFormat, objectify );
|
||||
return that.loadOne( src, opts );
|
||||
});
|
||||
|
||||
},
|
||||
@ -43,17 +45,21 @@ Definition of the ResumeFactory class.
|
||||
/**
|
||||
Load a single resume from disk.
|
||||
*/
|
||||
loadOne: function( src, log, toFormat, objectify ) {
|
||||
loadOne: function( src, opts ) {
|
||||
|
||||
var log = opts.log;
|
||||
var toFormat = opts.format;
|
||||
var objectify = opts.objectify;
|
||||
|
||||
// Get the destination format. Can be 'fresh', 'jrs', or null/undefined.
|
||||
toFormat && (toFormat = toFormat.toLowerCase().trim());
|
||||
|
||||
// Load and parse the resume JSON
|
||||
var info = _parse( src, log, toFormat );
|
||||
var info = _parse( src, opts );
|
||||
if( info.error ) return info;
|
||||
var json = info.json;
|
||||
|
||||
// Determine the resume format: FRESH or JRS
|
||||
var json = info.json;
|
||||
var orgFormat = ( json.meta && json.meta.format &&
|
||||
json.meta.format.startsWith('FRESH@') ) ?
|
||||
'fresh' : 'jrs';
|
||||
@ -81,12 +87,12 @@ Definition of the ResumeFactory class.
|
||||
|
||||
|
||||
|
||||
function _parse( fileName, log, toFormat ) {
|
||||
function _parse( fileName, opts ) {
|
||||
var rawData;
|
||||
try {
|
||||
|
||||
// TODO: Core should not log
|
||||
log( chalk.gray('Reading resume: ') + chalk.cyan.bold(fileName) );
|
||||
opts.log( chalk.gray('Reading resume: ') + chalk.cyan.bold(fileName) );
|
||||
|
||||
// Read the file
|
||||
rawData = FS.readFileSync( fileName, 'utf8' );
|
||||
@ -99,12 +105,18 @@ Definition of the ResumeFactory class.
|
||||
}
|
||||
catch( ex ) {
|
||||
|
||||
// If FS.readFileSync threw, pass the exception along.
|
||||
if (!rawData)
|
||||
throw ex;
|
||||
// JSON.parse failed due to invalid JSON
|
||||
if ( ex instanceof SyntaxError) {
|
||||
var info = new SyntaxErrorEx( ex, rawData );
|
||||
opts.log( chalk.red.bold(fileName.toUpperCase() + ' contains invalid JSON on line ' +
|
||||
info.line + ' column ' + info.col + '.' +
|
||||
chalk.red(' Unable to validate.')));
|
||||
opts.log( chalk.red.bold('INTERNAL: ' + ex) );
|
||||
ex.handled = true;
|
||||
}
|
||||
|
||||
// Otherwise if JSON.parse failed: probably a SyntaxError.
|
||||
return {
|
||||
if( opts.throw ) throw ex;
|
||||
else return {
|
||||
error: ex,
|
||||
raw: rawData
|
||||
};
|
||||
|
Reference in New Issue
Block a user