Refactor error handling (interim).

This commit is contained in:
hacksalot 2016-01-14 11:47:05 -05:00
parent 7765e85336
commit a9c685c6a4
5 changed files with 27 additions and 18 deletions

View File

@ -28,33 +28,43 @@ Error-handling routines for HackMyResume.
*/
var ErrorHandler = module.exports = {
init: function( debug ) {
init: function( debug, assert ) {
this.debug = debug;
this.assert = assert;
return this;
},
err: function( ex, shouldExit ) {
if( ex.pass )
throw ex;
if( ex.fluenterror ) {
// Output the error message
var objError = assembleError( ex );
o( this[ 'format' + (objError.warning ? 'Warning' : 'Error')]( objError.msg ) );
o( this[ 'format' + (objError.warning ? 'Warning' : 'Error')](
objError.msg
));
// Output the stack (sometimes)
if( objError.showStack )
o( chalk.red( ex.stack || ex.inner.stack ) );
// Quit if necessary
if( objError.quit ) {
this.debug && o(
chalk.cyan('Exiting with error code ' + ex.fluenterror.toString()));
if( this.assert ) { ex.pass = true; throw ex; }
process.exit( ex.fluenterror );
}
}
else {
o( ex );
var stackTrace = ex.stack || (ex.inner && ex.inner.stack);
if( stackTrace && this.debug )
o( ex.stack || ex.inner.stack );
// if( this.debug )
// o( ex.stack || ex.inner.stack );
}

View File

@ -50,6 +50,7 @@ Definition of the `main` function.
.option('--no-color', 'Disable colors')
.option('--color', 'Enable colors')
.option('-d --debug', 'Enable diagnostics', false)
.option('-a --assert', 'Treat warnings as errors', false)
.option('-v --version', 'Show the version')
.allowUnknownOption();
program.jsonArgs = initInfo.options;
@ -69,7 +70,6 @@ Definition of the `main` function.
program
.command('validate')
.arguments('<sources...>')
.option('-a --assert', 'Treat validation warnings as errors', false)
.description('Validate a resume in FRESH or JSON RESUME format.')
.action(function(sources) {
execute.call( this, sources, [], this.opts(), logMsg);
@ -217,11 +217,14 @@ Definition of the `main` function.
function execute( src, dst, opts, log ) {
loadOptions.call( this, opts, this.parent.jsonArgs );
var hand = require( './error-handler' ).init( _opts.debug );
var hand = require( './error-handler' );
hand.init( _opts.debug, _opts.assert );
var v = new HMR.verbs[ this.name() ]();
_out.init( _opts );
v.on( 'hmr:status', function() { _out.do.apply( _out, arguments ); });
v.on( 'hmr:error', function() { hand.err.apply( hand, arguments ); });
v.on( 'hmr:error', function() {
hand.err.apply( hand, arguments );
});
v.invoke.call( v, src, dst, _opts, log );
}
@ -250,6 +253,8 @@ Definition of the `main` function.
o.silent = this.parent.silent;
if( this.parent.debug !== undefined && this.parent.debug !== null)
o.debug = this.parent.debug;
if( this.parent.assert !== undefined && this.parent.assert !== null)
o.assert = this.parent.assert;
if( o.debug ) {
logMsg(chalk.cyan('OPTIONS:') + '\n');

View File

@ -71,16 +71,6 @@ Definition of the FRESHResume class.
*/
FreshResume.prototype.parseJSON = function( rep, opts ) {
// // Convert JSON Resume to FRESH if necessary
// // TODO: Not sure if this code path is still executed. JRS resumes should
// // be loaded via JRSResume, not here.
// if( rep.basics ) {
// throw "Invalid resume conversion path";
// rep = CONVERTER.toFRESH( rep );
// rep.imp = rep.imp || { };
// rep.imp.orgFormat = 'JRS';
// }
// Now apply the resume representation onto this object
extend( true, this, rep );

View File

@ -57,13 +57,13 @@ Definition of the Verb class.
/**
Fire the 'hmr:error' error event.
Handle an error condition.
*/
err: function( errorCode, payload, hot ) {
payload = payload || { };
payload.sub = payload.fluenterror = errorCode;
payload.throw = hot;
this.fire('error', payload);
this.fire( 'error', payload );
if( hot ) throw payload;
return true;
},

View File

@ -10,6 +10,7 @@ var chai = require('chai')
, FRESHResume = require('../src/core/fresh-resume')
, FCMD = require( '../src/hackmyapi')
, validator = require('is-my-json-valid')
, HMRMAIN = require('../src/cli/main')
, EXTEND = require('../src/utils/extend');
chai.config.includeStack = false;
@ -74,6 +75,9 @@ describe('Testing CLI interface', function () {
function runIt() {
try {
var v = new FCMD.verbs[verb]();
v.on('hmr:error', function(ex) {
throw ex;
});
v.invoke( src, dst, opts, opts.silent ?
function(){} : function(msg){ msg = msg || ''; console.log(msg); } );
}