From 28c703daf798319ca8f52757e3a916d072378901 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Fri, 8 Jan 2016 05:11:38 -0500 Subject: [PATCH] Improve error handling: PDFs. --- src/core/error-handler.js | 10 +++++++++- src/core/status-codes.js | 3 ++- src/gen/html-pdf-cli-generator.js | 28 +++++++++++++++++++++------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/core/error-handler.js b/src/core/error-handler.js index 36a21dd..ac4efe4 100644 --- a/src/core/error-handler.js +++ b/src/core/error-handler.js @@ -73,7 +73,8 @@ Error-handling routines for HackMyResume. function get_error_msg( ex ) { - var msg = '', withStack = false; + + var msg = '', withStack = false, isError = false; switch( ex.fluenterror ) { case HACKMYSTATUS.themeNotFound: @@ -137,6 +138,10 @@ Error-handling routines for HackMyResume. }); break; + case HACKMYSTATUS.notOnPath: + msg = formatError( ex.engine + " wasn't found on your system path or is inaccessible. PDF not generated." ); + break; + } return { msg: msg, @@ -144,6 +149,9 @@ Error-handling routines for HackMyResume. }; } + function formatError( msg ) { + return chalk.red.bold( 'ERROR: ' + msg ); + } }()); diff --git a/src/core/status-codes.js b/src/core/status-codes.js index b8dccf5..f3655fe 100644 --- a/src/core/status-codes.js +++ b/src/core/status-codes.js @@ -19,7 +19,8 @@ Status codes for HackMyResume. pdfgeneration: 9, missingPackageJSON: 10, invalid: 11, - invalidTarget: 12 + invalidTarget: 12, + notOnPath: 13 }; }()); diff --git a/src/gen/html-pdf-cli-generator.js b/src/gen/html-pdf-cli-generator.js index 4ea30ab..ad360e1 100644 --- a/src/gen/html-pdf-cli-generator.js +++ b/src/gen/html-pdf-cli-generator.js @@ -37,14 +37,16 @@ Definition of the HtmlPdfCLIGenerator class. */ onBeforeSave: function( info ) { try { - engines[ info.opts.pdf || 'wkhtmltopdf' ] - .call( this, info.mk, info.outputFile ); + var safe_eng = info.opts.pdf || 'wkhtmltopdf'; + engines[ safe_eng ].call( this, info.mk, info.outputFile ); return null; // halt further processing } catch(ex) { // { [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', ... } // { [Error: ENOENT] } - throw { fluenterror: this.codes.pdfGeneration, inner: ex }; + throw ( ex.inner && ex.inner.code === 'ENOENT' ) ? + { fluenterror: this.codes.notOnPath, engine: ex.cmd } : + { fluenterror: this.codes.pdfGeneration, inner: ex.inner }; } } @@ -65,10 +67,16 @@ Definition of the HtmlPdfCLIGenerator class. var tempFile = fOut.replace(/\.pdf$/i, '.pdf.html'); FS.writeFileSync( tempFile, markup, 'utf8' ); - var spawn = require('child_process').spawn; - var child = spawn('wkhtmltopdf', [ + var spawn = require('child_process').spawnSync; + var info = spawn('wkhtmltopdf', [ tempFile, fOut ]); + if( info.error ) { + throw { + cmd: 'wkhtmltopdf', + inner: info.error + }; + } // child.stdout.on('data', function(chunk) { // // output will be here in chunks @@ -95,8 +103,14 @@ Definition of the HtmlPdfCLIGenerator class. var sourcePath = SLASH( PATH.relative( process.cwd(), tempFile) ); var destPath = SLASH( PATH.relative( process.cwd(), fOut) ); - var spawn = require('child_process').spawn; - var child = spawn('phantomjs', [ scriptPath, sourcePath, destPath ]); + var spawn = require('child_process').spawnSync; + var info = spawn('1phantomjs', [ scriptPath, sourcePath, destPath ]); + if( info.error ) { + throw { + cmd: 'phantomjs', + inner: info.error + }; + } // child.stdout.on('data', function(chunk) { // // output will be here in chunks