From d220cedfeb4e570b0c5baaf464c2feb36ddc180f Mon Sep 17 00:00:00 2001 From: hacksalot Date: Mon, 18 Jan 2016 18:35:38 -0500 Subject: [PATCH] Improve behavior of PEEK command. --- src/cli/error.js | 12 +++++++++--- src/cli/out.js | 4 ++-- src/core/resume-factory.js | 1 + src/utils/safe-json-loader.js | 17 +++++++---------- src/verbs/peek.js | 19 ++++++++++++++----- 5 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/cli/error.js b/src/cli/error.js index 80c2fd9..7492e06 100644 --- a/src/cli/error.js +++ b/src/cli/error.js @@ -65,7 +65,7 @@ Error-handling routines for HackMyResume. } // Quit if necessary - if( objError.quit ) { + if( ex.quit || objError.quit ) { this.debug && o( chalk.cyan('Exiting with error code ' + ex.fluenterror.toString())); if( this.assert ) { ex.pass = true; throw ex; } @@ -85,6 +85,7 @@ Error-handling routines for HackMyResume. }, formatError: function( msg ) { + msg = msg || ''; return chalk.red.bold( msg.toUpperCase().startsWith('ERROR:') ? msg : 'Error: ' + msg ); }, @@ -102,7 +103,7 @@ Error-handling routines for HackMyResume. function assembleError( ex ) { - var msg = '', withStack = false, isError = false, quit = true, warn = true; + var msg = '', withStack = false, isError = false, quit = false, warn = true; if( this.debug ) withStack = true; switch( ex.fluenterror ) { @@ -190,7 +191,7 @@ Error-handling routines for HackMyResume. break; case HMSTATUS.readError: - 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(); warn = false; break; @@ -202,10 +203,15 @@ Error-handling routines for HackMyResume. case HMSTATUS.parseError: if( SyntaxErrorEx.is( ex.inner )) { + console.error( printf( M2C(this.msgs.readError.msg, 'red'), ex.file ) ); var se = new SyntaxErrorEx( ex, ex.raw ); msg = printf( M2C( this.msgs.parseError.msg, 'red' ), se.line, se.col); } + else if( ex.inner && ex.inner.line !== undefined && ex.inner.col !== undefined ) { + msg = printf( M2C( this.msgs.parseError.msg, 'red' ), + ex.inner.line, ex.inner.col); + } else { msg = ex; } diff --git a/src/cli/out.js b/src/cli/out.js index 1014595..20913e3 100644 --- a/src/cli/out.js +++ b/src/cli/out.js @@ -198,9 +198,9 @@ Output routines for HackMyResume. case HME.beforePeek: if( evt.target ) - L(M2C(this.msgs.beforePeek.msg[0], 'cyan'), evt.target, evt.file); + L(M2C(this.msgs.beforePeek.msg[0], evt.isError ? 'red' : 'green'), evt.target, evt.file); else - L(M2C(this.msgs.beforePeek.msg[1], 'cyan'), evt.file); + L(M2C(this.msgs.beforePeek.msg[1], evt.isError ? 'red' : 'green'), evt.file); break; case HME.afterPeek: diff --git a/src/core/resume-factory.js b/src/core/resume-factory.js index fd77341..2fbe8a6 100644 --- a/src/core/resume-factory.js +++ b/src/core/resume-factory.js @@ -125,6 +125,7 @@ Definition of the ResumeFactory class. fluenterror: rawData ? HACKMYSTATUS.parseError : HACKMYSTATUS.readError, inner: e, raw: rawData, file: fileName, shouldExit: false }; + opts.quit && (ex.quit = true); eve && eve.err( ex.fluenterror, ex ); if( opts.throw ) throw ex; return ex; diff --git a/src/utils/safe-json-loader.js b/src/utils/safe-json-loader.js index e393186..ab96048 100644 --- a/src/utils/safe-json-loader.js +++ b/src/utils/safe-json-loader.js @@ -11,7 +11,6 @@ Definition of the SafeJsonLoader class. var FS = require('fs') - , HMSTATUS = require('../core/status-codes') , SyntaxErrorEx = require('./syntax-error-ex'); @@ -29,15 +28,13 @@ Definition of the SafeJsonLoader class. // If we get here, either FS.readFileSync or JSON.parse failed. // We'll return HMSTATUS.readError or HMSTATUS.parseError. - ret.ex = ( ret.raw && ret.raw.trim() ) ? - { // JSON.parse failed, likely because of a SyntaxError - fluenterror: HMSTATUS.parseError, - inner: SyntaxErrorEx.is( ex ) ? new SyntaxErrorEx( ex ) : ex - } : - { // FS.readFileSync failed, likely because of ENOENT or EACCES - fluenterror: HMSTATUS.readError, - inner: ex - }; + var retRaw = ret.raw && ret.raw.trim(); + + ret.ex = { + operation: retRaw ? 'parse' : 'read', + inner: SyntaxErrorEx.is( ex ) ? (new SyntaxErrorEx( ex, retRaw )) : ex, + file: file + }; } diff --git a/src/verbs/peek.js b/src/verbs/peek.js index 6254525..27b6e04 100644 --- a/src/verbs/peek.js +++ b/src/verbs/peek.js @@ -45,15 +45,24 @@ Implementation of the 'peek' verb for HackMyResume. var objPath = (dst && dst[0]) || ''; _.each( src, function( t ) { - this.stat( HMEVENT.beforePeek, { file: t, target: objPath } ); var obj = safeLoadJSON( t ); - if( obj.ex ) { - this.err( obj.ex.fluenterror, obj.ex ); - } - var targ = objPath ? __.get( obj.json, objPath ) : obj.json; + this.stat( HMEVENT.beforePeek, { file: t, target: objPath, isError: obj.ex } ); + if( obj.ex ) { + if( obj.ex.operation === 'parse' ) + this.err( HMSTATUS.parseError, obj.ex ); + else { + obj.ex.quiet = true; + this.err( HMSTATUS.readError, obj.ex ); + } + return; + } + + var targ = objPath ? __.get( obj.json, objPath ) : obj.json; this.stat( HMEVENT.afterPeek, { file: t, requested: objPath, target: targ } ); + + }, this); }