1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-24 17:30:10 +00:00

Improve error handling.

This commit is contained in:
hacksalot 2016-01-20 19:59:36 -05:00
parent 57787f1bc7
commit f77cced7f3
5 changed files with 88 additions and 42 deletions

View File

@ -1,9 +1,9 @@
/** /**
Error-handling routines for HackMyResume. Error-handling routines for HackMyResume.
@module error.js @module cli/error
@license MIT. See LICENSE.md for details. @license MIT. See LICENSE.md for details.
*/ */
// TODO: Logging library
(function() { (function() {
@ -31,6 +31,8 @@ Error-handling routines for HackMyResume.
*/ */
var ErrorHandler = module.exports = { var ErrorHandler = module.exports = {
init: function( debug, assert, silent ) { init: function( debug, assert, silent ) {
this.debug = debug; this.debug = debug;
this.assert = assert; this.assert = assert;
@ -39,29 +41,30 @@ Error-handling routines for HackMyResume.
return this; return this;
}, },
err: function( ex, shouldExit ) { err: function( ex, shouldExit ) {
// Short-circuit logging output if --silent is on
var o = this.silent ? function() { } : _defaultLog; var o = this.silent ? function() { } : _defaultLog;
if( !this.msgs ) { // Special case; can probably be removed.
this.msgs = require('./msg.js').errors; if( ex.pass ) throw ex;
}
if( ex.pass ) // Load error messages
throw ex; this.msgs = this.msgs || require('./msg.js').errors;
// Handle packaged HMR exceptions
if( ex.fluenterror ) { if( ex.fluenterror ) {
// Output the error message // Output the error message
var objError = assembleError.call( this, ex ); var objError = assembleError.call( this, ex );
o( this[ 'format' + (objError.warning ? 'Warning' : 'Error')]( o( this[ 'format_' + objError.etype ]( objError.msg ));
objError.msg
));
// Output the stack (sometimes) // Output the stack (sometimes)
if( objError.withStack ) { if( objError.withStack ) {
var stack = ex.stack || (ex.inner && ex.inner.stack); var stack = ex.stack || (ex.inner && ex.inner.stack);
stack && o( chalk.red( stack ) ); stack && o( chalk.gray( stack ) );
} }
// Quit if necessary // Quit if necessary
@ -73,37 +76,51 @@ Error-handling routines for HackMyResume.
} }
} }
// Handle raw exceptions
else { else {
o( ex ); o( ex );
var stackTrace = ex.stack || (ex.inner && ex.inner.stack); var stackTrace = ex.stack || (ex.inner && ex.inner.stack);
if( stackTrace && this.debug ) if( stackTrace && this.debug )
o( ex.stack || ex.inner.stack ); o( M2C(ex.stack || ex.inner.stack, 'gray') );
// if( this.debug )
// o( ex.stack || ex.inner.stack );
} }
}, },
formatError: function( msg ) {
format_error: function( msg ) {
msg = msg || ''; msg = msg || '';
return chalk.red.bold( return chalk.red.bold(
msg.toUpperCase().startsWith('ERROR:') ? msg : 'Error: ' + msg ); msg.toUpperCase().startsWith('ERROR:') ? msg : 'Error: ' + msg );
}, },
formatWarning: function( brief, msg ) {
format_warning: function( brief, msg ) {
return chalk.yellow(brief) + chalk.yellow(msg || ''); return chalk.yellow(brief) + chalk.yellow(msg || '');
},
format_custom: function( msg ) {
return msg;
} }
}; };
function _defaultLog() { function _defaultLog() {
console.log.apply( console.log, arguments ); console.log.apply( console.log, arguments );
} }
function assembleError( ex ) { function assembleError( ex ) {
var msg = '', withStack = false, isError = false, quit = false, warn = true; var msg = '', withStack = false, quit = false, etype = 'warning';
if( this.debug ) withStack = true; if( this.debug ) withStack = true;
switch( ex.fluenterror ) { switch( ex.fluenterror ) {
@ -151,23 +168,23 @@ Error-handling routines for HackMyResume.
case HMSTATUS.pdfGeneration: case HMSTATUS.pdfGeneration:
msg = M2C( this.msgs.pdfGeneration.msg, 'bold' ); msg = M2C( this.msgs.pdfGeneration.msg, 'bold' );
if( ex.inner ) msg += chalk.red('\n' + ex.inner); if( ex.inner ) msg += chalk.red('\n' + ex.inner);
withStack = true; quit = false; warn = false; withStack = true; quit = false; etype = 'error';
break; break;
case HMSTATUS.invalid: case HMSTATUS.invalid:
msg = M2C( this.msgs.invalid.msg, 'red' ); msg = M2C( this.msgs.invalid.msg, 'red' );
warn = false; etype = 'error';
break; break;
case HMSTATUS.generateError: case HMSTATUS.generateError:
msg = (ex.inner && ex.inner.toString()) || ex; msg = (ex.inner && ex.inner.toString()) || ex;
quit = false; quit = false;
warn = false; etype = 'error';
break; break;
case HMSTATUS.fileSaveError: case HMSTATUS.fileSaveError:
msg = printf( M2C( this.msgs.fileSaveError.msg ), (ex.inner || ex).toString() ); msg = printf( M2C( this.msgs.fileSaveError.msg ), (ex.inner || ex).toString() );
warn = false; etype = 'error';
quit = false; quit = false;
break; break;
@ -181,19 +198,20 @@ Error-handling routines for HackMyResume.
case HMSTATUS.invalidHelperUse: case HMSTATUS.invalidHelperUse:
msg = printf( M2C( this.msgs.invalidHelperUse.msg ), ex.helper ); msg = printf( M2C( this.msgs.invalidHelperUse.msg ), ex.helper );
quit = false; quit = false;
warn = true; etype = 'error';
break; break;
case HMSTATUS.notOnPath: case HMSTATUS.notOnPath:
msg = printf( M2C(this.msgs.notOnPath.msg, 'bold'), ex.engine); msg = printf( M2C(this.msgs.notOnPath.msg, 'bold'), ex.engine);
quit = false; quit = false;
warn = false; etype = 'error';
break; break;
case HMSTATUS.readError: case HMSTATUS.readError:
if( !ex.quiet ) console.error( printf( M2C(this.msgs.readError.msg, 'red'), ex.file ) ); if( !ex.quiet )
console.error(printf( M2C(this.msgs.readError.msg, 'red'), ex.file));
msg = ex.inner.toString(); msg = ex.inner.toString();
warn = false; etype = 'error';
break; break;
case HMSTATUS.mixedMerge: case HMSTATUS.mixedMerge:
@ -201,6 +219,17 @@ Error-handling routines for HackMyResume.
quit = false; quit = false;
break; break;
case HMSTATUS.invokeTemplate:
msg = M2C( this.msgs.invokeTemplate.msg, 'red' );
msg += M2C( '\n' + WRAP(ex.inner.toString(), { width: 60, indent: ' ' }), 'gray' );
etype = 'custom';
break;
case HMSTATUS.compileTemplate:
//msg = printf( M2C( this.msgs.compileTemplate.msg ), ex.inner);
etype = 'error';
break;
case HMSTATUS.parseError: case HMSTATUS.parseError:
if( SyntaxErrorEx.is( ex.inner )) { if( SyntaxErrorEx.is( ex.inner )) {
console.error( printf( M2C(this.msgs.readError.msg, 'red'), ex.file ) ); console.error( printf( M2C(this.msgs.readError.msg, 'red'), ex.file ) );
@ -215,16 +244,16 @@ Error-handling routines for HackMyResume.
else { else {
msg = ex; msg = ex;
} }
warn = false; etype = 'error';
break; break;
} }
return { return {
warning: warn, // True if this is a warning, false if error
msg: msg, // The error message to display msg: msg, // The error message to display
withStack: withStack, // Whether to include the stack withStack: withStack, // Whether to include the stack
quit: quit quit: quit,
etype: etype
}; };
} }

View File

@ -86,3 +86,7 @@ errors:
msg: An error occurred while writing %s to disk: %s. msg: An error occurred while writing %s to disk: %s.
mixedMerge: mixedMerge:
msg: "**Warning:** merging mixed resume types. Errors may occur." msg: "**Warning:** merging mixed resume types. Errors may occur."
invokeTemplate:
msg: "An error occurred during template invocation."
compileTemplate:
msg: "An error occurred during template compilation."

View File

@ -26,7 +26,9 @@ Status codes for HackMyResume.
fileSaveError: 16, fileSaveError: 16,
generateError: 17, generateError: 17,
invalidHelperUse: 18, invalidHelperUse: 18,
mixedMerge: 19 mixedMerge: 19,
invokeTemplate: 20,
compileTemplate: 21
}; };
}()); }());

View File

@ -17,6 +17,7 @@ Definition of the HandlebarsGenerator class.
, PATH = require('path') , PATH = require('path')
, parsePath = require('parse-filepath') , parsePath = require('parse-filepath')
, READFILES = require('recursive-readdir-sync') , READFILES = require('recursive-readdir-sync')
, HMSTATUS = require('../core/status-codes')
, SLASH = require('slash'); , SLASH = require('slash');
@ -39,17 +40,27 @@ Definition of the HandlebarsGenerator class.
( format === 'html' || format === 'pdf' ) && (encData = json.markdownify()); ( format === 'html' || format === 'pdf' ) && (encData = json.markdownify());
( format === 'doc' ) && (encData = json.xmlify()); ( format === 'doc' ) && (encData = json.xmlify());
// Compile and run the Handlebars template. try {
var template = HANDLEBARS.compile(jst, { strict: false, assumeObjects: false }); // Compile and run the Handlebars template.
return template({ // TODO: Clean var template = HANDLEBARS.compile(jst, { strict: false, assumeObjects: false });
r: encData, return template({ // TODO: Clean
RAW: json, r: encData,
filt: opts.filters, RAW: json,
cssInfo: cssInfo, filt: opts.filters,
format: format, cssInfo: cssInfo,
opts: opts, format: format,
headFragment: opts.headFragment || '' opts: opts,
}); headFragment: opts.headFragment || ''
});
}
catch( ex ) {
throw {
fluenterror: template ?
HMSTATUS.invokeTemplate : HMSTATUS.compileTemplate,
inner: ex
};
}
} }

View File

@ -160,11 +160,13 @@ Implementation of the 'build' verb for HackMyResume.
} }
function handleInternalError( ex ) { function handleInternalError( ex ) {
console.log(ex); console.log(ex);
} }
/** /**
Generate a single target resume such as "out/rez.html" or "out/rez.doc". Generate a single target resume such as "out/rez.html" or "out/rez.doc".
TODO: Refactor. TODO: Refactor.
@ -194,7 +196,6 @@ Implementation of the 'build' verb for HackMyResume.
function(fmt) { return fmt.name === targInfo.fmt.outFormat; })[0]; function(fmt) { return fmt.name === targInfo.fmt.outFormat; })[0];
MKDIRP.sync( PATH.dirname( f ) ); // Ensure dest folder exists; MKDIRP.sync( PATH.dirname( f ) ); // Ensure dest folder exists;
_opts.targets = finished; _opts.targets = finished;
_opts.errHandler = handleInternalError;
ret = theFormat.gen.generate( _rezObj, f, _opts ); ret = theFormat.gen.generate( _rezObj, f, _opts );
} }
//Otherwise this is an ad-hoc format (JSON, YML, or PNG) that every theme //Otherwise this is an ad-hoc format (JSON, YML, or PNG) that every theme
@ -205,7 +206,6 @@ Implementation of the 'build' verb for HackMyResume.
})[0]; })[0];
var outFolder = PATH.dirname( f ); var outFolder = PATH.dirname( f );
MKDIRP.sync( outFolder ); // Ensure dest folder exists; MKDIRP.sync( outFolder ); // Ensure dest folder exists;
_opts.errHandler = handleInternalError;
ret = theFormat.gen.generate( _rezObj, f, _opts ); ret = theFormat.gen.generate( _rezObj, f, _opts );
} }
} }