From 8591a0807e968e28aefddb3dcaee5e8eb1df792a Mon Sep 17 00:00:00 2001 From: Romain Deltour Date: Mon, 25 Sep 2017 00:42:04 +0200 Subject: [PATCH] fix: improve error handling - Do not terminate the process in code, but throw errors - Return rejected promises --- src/checker/checker-nightmare.js | 8 ++++---- src/cli/cli.js | 8 ++++++-- src/core/ace.js | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/checker/checker-nightmare.js b/src/checker/checker-nightmare.js index 82b2369c..46cdd01d 100644 --- a/src/checker/checker-nightmare.js +++ b/src/checker/checker-nightmare.js @@ -10,25 +10,25 @@ const winston = require('winston'); const PATH_TO_AXE = path.join(path.dirname(require.resolve('axe-core')), 'axe.min.js'); if (!fs.existsSync(PATH_TO_AXE)) { winston.verbose(PATH_TO_AXE); - process.exit(1); + throw new Error('Can’t find aXe'); } const PATH_TO_H5O = path.join(path.dirname(require.resolve('h5o')), 'dist/outliner.min.js'); if (!fs.existsSync(PATH_TO_H5O)) { winston.verbose(PATH_TO_H5O); - process.exit(1); + throw new Error('Can’t find h5o'); } const PATH_TO_ACE_AXE = path.join(__dirname, '../scripts/ace-axe.js'); if (!fs.existsSync(PATH_TO_ACE_AXE)) { winston.verbose(PATH_TO_ACE_AXE); - process.exit(1); + throw new Error('Can’t find ace-axe script'); } const PATH_TO_ACE_EXTRACTION = path.join(__dirname, '../scripts/ace-extraction.js'); if (!fs.existsSync(PATH_TO_ACE_EXTRACTION)) { winston.verbose(PATH_TO_ACE_EXTRACTION); - process.exit(1); + throw new Error('Can’t find ace-extraction script'); } // EMXIF diff --git a/src/cli/cli.js b/src/cli/cli.js index 1efe29a8..34bf7fda 100644 --- a/src/cli/cli.js +++ b/src/cli/cli.js @@ -4,6 +4,7 @@ const ace = require('../core/ace.js'); const meow = require('meow'); +const winston = require('winston'); const cli = meow(` Usage @@ -40,6 +41,9 @@ ace(cli.input[0], { tmpdir: cli.flags.tempdir, verbose: cli.flags.verbose, silent: cli.flags.silent, - jobId: '' + jobId: '', }) -.catch(() => process.exit(1)); +.catch((err) => { + winston.error(err.message); + process.exit(1); +}); diff --git a/src/core/ace.js b/src/core/ace.js index 70e86b64..07fdf90c 100644 --- a/src/core/ace.js +++ b/src/core/ace.js @@ -22,7 +22,7 @@ module.exports = function ace(epubPath, options) { // Check that the EPUB exists if (!fs.existsSync(epubPath)) { winston.error(`Couldn’t find EPUB file '${epubPath}'`); - reject(jobId); + return reject(jobId); } // Process options