diff --git a/bin/docpub.js b/bin/docpub.js index ef7d97a..da9880e 100755 --- a/bin/docpub.js +++ b/bin/docpub.js @@ -39,7 +39,13 @@ program.on('--help', () => { program.parse(process.argv); -const docpub = new Docpub(program); +let docpub; + +try { + docpub = new Docpub(program); +} catch (e) { + process.exit(1); // in case docpub failed to construct interface instance, unable to continue +} docpub .uploadCategory() diff --git a/lib/docpub.js b/lib/docpub.js index 947b49f..5b9193d 100644 --- a/lib/docpub.js +++ b/lib/docpub.js @@ -31,7 +31,7 @@ module.exports = class Docpub { logger.setup(opts); this._path = path.resolve(opts.path); - this._config = new Config(opts.configPath, this._path); + this._config = this._loadConfig(opts); } /** @@ -63,11 +63,24 @@ module.exports = class Docpub { ); }) .catch(e => { - logger.error(`Upload failed!`); - logger.error(e.message); - logger.error(e.stack); + this._logError(e); return Promise.reject(e); }); } + + _loadConfig(opts) { + try { + return new Config(opts.configPath, this._path); + } catch (e) { + this._logError(e); + throw e; + } + } + + _logError(error) { + logger.error(`Upload failed!`); + logger.error(error.message); + logger.error(error.stack); + } }; diff --git a/test/unit/docpub.js b/test/unit/docpub.js index 7996d20..9d579a3 100644 --- a/test/unit/docpub.js +++ b/test/unit/docpub.js @@ -90,6 +90,14 @@ describe('Docpub', () => { path.resolve('path/to/doc/dir') ); }); + + it('should throw if failed to create config', () => { + const error = new Error('error'); + + Config.throws(error); + + expect(() => new Docpub({path: 'foo/bar'})).to.throw(error); + }); }); describe('uploadCategory', () => {