diff --git a/lib/config/read-config-file.js b/lib/config/read-config-file.js index bac96032..9e4cbf78 100644 --- a/lib/config/read-config-file.js +++ b/lib/config/read-config-file.js @@ -16,6 +16,8 @@ var _configFilePath = require('./config-file-path'); var _configFilePath2 = _interopRequireDefault(_configFilePath); +var _error = require('../error'); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var createEmptyConfigFile = _ramda2['default'].curryN(3, _fs2['default'].writeFileSync)(_ramda2['default'].__, _ramda2['default'].__, 'utf8'); @@ -28,7 +30,10 @@ exports['default'] = function (defaults) { try { return readConfigFile(_configFilePath2['default']); } catch (e) { - createEmptyConfigFile(_configFilePath2['default'], JSON.stringify(defaults)); - return defaults; + if (e.code && e.code === 'ENOENT') { + createEmptyConfigFile(_configFilePath2['default'], JSON.stringify(defaults, null, 2)); + return defaults; + } + return (0, _error.handleError)(new _error.ControlledError(_error.errors.config.malformedFile)); } }; \ No newline at end of file diff --git a/lib/error/errors.js b/lib/error/errors.js index b48e2334..82d9cd6b 100644 --- a/lib/error/errors.js +++ b/lib/error/errors.js @@ -5,6 +5,9 @@ Object.defineProperty(exports, "__esModule", { }); /* eslint-disable max-len */ exports['default'] = { + config: { + malformedFile: 'It looks like your config file is not correct. If you have modified it, make sure it is a valid JSON' + }, git: { branchAlreadyExists: 'It looks like you already have a branch for this issue.', couldNotInitializeRepo: 'We have problems accessing the repo in \'${pathToRepo}\'. Make sure it exists.', diff --git a/lib/error/index.js b/lib/error/index.js index 96c6f719..2ee4c8b1 100644 --- a/lib/error/index.js +++ b/lib/error/index.js @@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.errors = exports.catchPromiseAndThrow = exports.handleError = exports.throwControlledError = undefined; +exports.errors = exports.catchPromiseAndThrow = exports.handleError = exports.throwControlledError = exports.ControlledError = undefined; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); @@ -54,7 +54,7 @@ function _extendableBuiltin(cls) { return ExtendableBuiltin; } -var ControlledError = function (_extendableBuiltin2) { +var ControlledError = exports.ControlledError = function (_extendableBuiltin2) { _inherits(ControlledError, _extendableBuiltin2); function ControlledError(message) { diff --git a/src/config/read-config-file.js b/src/config/read-config-file.js index c70f6643..fcbf2d16 100644 --- a/src/config/read-config-file.js +++ b/src/config/read-config-file.js @@ -1,6 +1,7 @@ import fs from 'fs'; import R from 'ramda'; import configFilePath from './config-file-path'; +import { ControlledError, errors, handleError } from '../error'; const createEmptyConfigFile = R.curryN(3, fs.writeFileSync)(R.__, R.__, 'utf8'); const readConfigFile = R.curryN(2, R.compose(JSON.parse, fs.readFileSync))(R.__, 'utf8'); @@ -12,7 +13,10 @@ export default (defaults) => { try { return readConfigFile(configFilePath); } catch (e) { - createEmptyConfigFile(configFilePath, JSON.stringify(defaults)); - return defaults; + if (e.code && e.code === 'ENOENT') { + createEmptyConfigFile(configFilePath, JSON.stringify(defaults, null, 2)); + return defaults; + } + return handleError(new ControlledError(errors.config.malformedFile)); } }; diff --git a/src/error/errors.js b/src/error/errors.js index 7440d720..e62c8268 100644 --- a/src/error/errors.js +++ b/src/error/errors.js @@ -1,5 +1,8 @@ /* eslint-disable max-len */ export default { + config: { + malformedFile: 'It looks like your config file is not correct. If you have modified it, make sure it is a valid JSON' + }, git: { branchAlreadyExists: 'It looks like you already have a branch for this issue.', couldNotInitializeRepo: 'We have problems accessing the repo in \'${pathToRepo}\'. Make sure it exists.', diff --git a/src/error/index.js b/src/error/index.js index ae8ea8ac..5b7d566d 100644 --- a/src/error/index.js +++ b/src/error/index.js @@ -3,7 +3,7 @@ import errors from './errors'; import { debugCurried, error, errorCurried } from '../util'; import reporter from '../reporter'; -class ControlledError extends Error { +export class ControlledError extends Error { constructor(message, parameters = {}) { super(R.compose( R.reduce((msg, [k, v]) => R.replace(`\${${k}}`, v, msg), message),