From a9c685c6a49eda380102f0d0fadb0497adbd61c8 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Thu, 14 Jan 2016 11:47:05 -0500 Subject: [PATCH] Refactor error handling (interim). --- src/cli/error-handler.js | 16 +++++++++++++--- src/cli/main.js | 11 ++++++++--- src/core/fresh-resume.js | 10 ---------- src/verbs/verb.js | 4 ++-- test/test-cli.js | 4 ++++ 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/cli/error-handler.js b/src/cli/error-handler.js index 5af601f..5e564b3 100644 --- a/src/cli/error-handler.js +++ b/src/cli/error-handler.js @@ -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 ); } diff --git a/src/cli/main.js b/src/cli/main.js index 1c6fb40..16b3048 100644 --- a/src/cli/main.js +++ b/src/cli/main.js @@ -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('') - .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'); diff --git a/src/core/fresh-resume.js b/src/core/fresh-resume.js index 8000e53..efbc464 100644 --- a/src/core/fresh-resume.js +++ b/src/core/fresh-resume.js @@ -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 ); diff --git a/src/verbs/verb.js b/src/verbs/verb.js index 2ed6cb4..8d84136 100644 --- a/src/verbs/verb.js +++ b/src/verbs/verb.js @@ -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; }, diff --git a/test/test-cli.js b/test/test-cli.js index b3fc9ec..59e5357 100644 --- a/test/test-cli.js +++ b/test/test-cli.js @@ -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); } ); }