From 7ab2df669677ae95ae579c24715c9fc07c87222f Mon Sep 17 00:00:00 2001 From: devlinjd Date: Wed, 7 Oct 2015 09:29:41 -0400 Subject: [PATCH] Improve error handling. --- src/fluentcmd.js | 22 +++++++++++----------- src/index.js | 25 +++++++++++++++++++------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/fluentcmd.js b/src/fluentcmd.js index 7ab30b1..ce281cc 100644 --- a/src/fluentcmd.js +++ b/src/fluentcmd.js @@ -9,9 +9,10 @@ module.exports = function () { var path = require( 'path' ) , extend = require( './utils/extend' ) , unused = require('./utils/string') - , FLUENT = require('fluentlib') + , fs = require('fs') , _ = require('underscore') - , rez, _log; + , FLUENT = require('fluentlib') + , rez, _log, _err; /** Given a source JSON resume, a destination resume path, and a theme file, @@ -21,19 +22,21 @@ module.exports = function () { @param theme Friendly name of the resume theme. Defaults to "informatic". @param logger Optional logging override. */ - function gen( src, dst, theme, logger ) { + function gen( src, dst, theme, logger, errHandler ) { _log = logger || console.log; - _opts.theme = theme; - var msg = ''; + _err = errHandler || error; + _opts.theme = (theme && theme.toLowerCase().trim()) || 'informatic'; // Load input resumes... + if(!src || !src.length) { throw { fluenterror: 3 }; } var sheets = src.map( function( res ) { _log( 'Reading JSON resume: ' + res ); return (new FLUENT.Sheet()).open( res ); }); // Merge input resumes... + var msg = ''; rez = _.reduceRight( sheets, function( a, b, idx ) { msg += ((idx == sheets.length - 2) ? 'Merging ' + a.meta.fileName : '') + ' onto ' + b.meta.fileName; @@ -77,18 +80,15 @@ module.exports = function () { return fObj.gen.generate( rez, fOut, _opts.theme ); } catch( ex ) { - err( ex ); + _err( ex ); } } /** Handle an exception. */ - function err( ex ) { - var msg = ex.toString(); - var idx = msg.indexOf('Error: '); - var trimmed = idx === -1 ? msg : msg.substring( idx + 7 ); - _log( 'ERROR: ' + trimmed.toString() ); + function error( ex ) { + throw ex; } /** diff --git a/src/index.js b/src/index.js index 0225688..fa9eea2 100644 --- a/src/index.js +++ b/src/index.js @@ -6,19 +6,32 @@ Command-line interface (CLI) for FluentCMD via Node.js. */ var ARGS = require( 'minimist' ) - , FCMD = require( './fluentcmd'); + , FCMD = require( './fluentcmd') + , PKG = require('../package.json'); try { - console.log( '*** FluentCMD v0.1.0 ***' ); - if( process.argv.length <= 2 ) { throw 'Please specify a JSON resume file.'; } + console.log( '*** FluentCMD v' + PKG.version + ' ***' ); + if( process.argv.length <= 2 ) { throw { fluenterror: 3 }; } var args = ARGS( process.argv.slice(2) ); - var src = args._.filter( function( a ) { return a.endsWith('.json'); }); - var dst = args._.filter( function( a ) { return !a.endsWith('.json'); }); + var src = args._.filter( function( a ) { return a.match(/\.json$/); }); + var dst = args._.filter( function( a ) { return !a.match(/\.json$/); }); FCMD.generate( src, dst, args.t || 'informatic' ); process.platform !== 'win32' && console.log('\n'); } catch( ex ) { - var msg = ex.toString(); + + var msg = ''; + if( ex.fluenterror ){ + switch( ex.fluenterror ) { + case 1: msg = "The specified theme couldn't be found: " + ex.data; break; + case 2: msg = "Couldn't copy CSS file to destination folder"; break; + case 3: msg = "Please specify a valid JSON resume file."; break; + }; + } + else { + msg = ex.toString(); + } + var idx = msg.indexOf('Error: '); var trimmed = idx === -1 ? msg : msg.substring( idx + 7 ); console.log( 'ERROR: ' + trimmed.toString() );