1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-16 22:00:09 +00:00
HackMyResume/dist/cli/error.js

249 lines
7.6 KiB
JavaScript
Raw Normal View History

2016-01-26 15:58:10 +00:00
2016-01-25 15:34:57 +00:00
/**
Error-handling routines for HackMyResume.
@module cli/error
@license MIT. See LICENSE.md for details.
2016-01-26 15:58:10 +00:00
*/
2016-01-25 15:34:57 +00:00
(function() {
2016-01-26 15:58:10 +00:00
var ErrorHandler, FCMD, FS, HMSTATUS, M2C, PATH, PKG, SyntaxErrorEx, WRAP, YAML, _defaultLog, assembleError, chalk, extend, printf;
2016-01-25 15:34:57 +00:00
HMSTATUS = require('../core/status-codes');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
PKG = require('../../package.json');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
FS = require('fs');
2016-01-25 15:34:57 +00:00
FCMD = require('../index');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
PATH = require('path');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
WRAP = require('word-wrap');
2016-01-25 15:34:57 +00:00
M2C = require('../utils/md2chalk');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
chalk = require('chalk');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
extend = require('extend');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
YAML = require('yamljs');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
printf = require('printf');
2016-01-25 15:34:57 +00:00
SyntaxErrorEx = require('../utils/syntax-error-ex');
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
require('string.prototype.startswith');
2016-01-25 15:34:57 +00:00
2016-02-02 02:14:36 +00:00
/** Error handler for HackMyResume. All errors are handled here.
2016-01-26 15:58:10 +00:00
@class ErrorHandler
*/
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
ErrorHandler = module.exports = {
init: function(debug, assert, silent) {
this.debug = debug;
this.assert = assert;
this.silent = silent;
this.msgs = require('./msg').errors;
return this;
},
err: function(ex, shouldExit) {
var o, objError, stack, stackTrace;
o = this.silent ? function() {} : _defaultLog;
if (ex.pass) {
throw ex;
}
this.msgs = this.msgs || require('./msg').errors;
if (ex.fluenterror) {
objError = assembleError.call(this, ex);
o(this['format_' + objError.etype](objError.msg));
if (objError.withStack) {
stack = ex.stack || (ex.inner && ex.inner.stack);
stack && o(chalk.gray(stack));
2016-01-25 15:34:57 +00:00
}
2016-02-02 02:14:36 +00:00
if (shouldExit) {
2016-01-26 15:58:10 +00:00
if (this.debug) {
o(chalk.cyan('Exiting with error code ' + ex.fluenterror.toString()));
}
if (this.assert) {
ex.pass = true;
throw ex;
}
return process.exit(ex.fluenterror);
}
} else {
o(ex);
stackTrace = ex.stack || (ex.inner && ex.inner.stack);
if (stackTrace && this.debug) {
return o(M2C(ex.stack || ex.inner.stack, 'gray'));
2016-01-25 15:34:57 +00:00
}
}
},
2016-01-26 15:58:10 +00:00
format_error: function(msg) {
2016-01-25 15:34:57 +00:00
msg = msg || '';
2016-01-26 15:58:10 +00:00
return chalk.red.bold(msg.toUpperCase().startsWith('ERROR:') ? msg : 'Error: ' + msg);
2016-01-25 15:34:57 +00:00
},
2016-01-26 15:58:10 +00:00
format_warning: function(brief, msg) {
2016-01-25 15:34:57 +00:00
return chalk.yellow(brief) + chalk.yellow(msg || '');
},
2016-01-26 15:58:10 +00:00
format_custom: function(msg) {
2016-01-25 15:34:57 +00:00
return msg;
}
};
2016-01-26 15:58:10 +00:00
_defaultLog = function() {
return console.log.apply(console.log, arguments);
};
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
assembleError = function(ex) {
var etype, msg, quit, se, withStack;
msg = '';
withStack = false;
quit = false;
etype = 'warning';
if (this.debug) {
withStack = true;
}
switch (ex.fluenterror) {
2016-01-25 15:34:57 +00:00
case HMSTATUS.themeNotFound:
2016-01-26 15:58:10 +00:00
msg = printf(M2C(this.msgs.themeNotFound.msg, 'yellow'), ex.data);
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.copyCSS:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.copyCSS.msg, 'red');
2016-01-25 15:34:57 +00:00
quit = false;
break;
case HMSTATUS.resumeNotFound:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.resumeNotFound.msg, 'yellow');
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.missingCommand:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.missingCommand.msg + " (", 'yellow');
msg += Object.keys(FCMD.verbs).map(function(v, idx, ar) {
return (idx === ar.length - 1 ? chalk.yellow('or ') : '') + chalk.yellow.bold(v.toUpperCase());
}).join(chalk.yellow(', ')) + chalk.yellow(").\n\n");
msg += chalk.gray(FS.readFileSync(PATH.resolve(__dirname, '../cli/use.txt'), 'utf8'));
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.invalidCommand:
2016-01-26 15:58:10 +00:00
msg = printf(M2C(this.msgs.invalidCommand.msg, 'yellow'), ex.attempted);
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.resumeNotFoundAlt:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.resumeNotFoundAlt.msg, 'yellow');
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.inputOutputParity:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.inputOutputParity.msg);
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.createNameMissing:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.createNameMissing.msg);
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.pdfGeneration:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.pdfGeneration.msg, 'bold');
if (ex.inner) {
msg += chalk.red('\n' + ex.inner);
}
quit = false;
etype = 'error';
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.invalid:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.invalid.msg, 'red');
2016-01-25 15:34:57 +00:00
etype = 'error';
break;
case HMSTATUS.generateError:
msg = (ex.inner && ex.inner.toString()) || ex;
quit = false;
etype = 'error';
break;
case HMSTATUS.fileSaveError:
2016-01-26 15:58:10 +00:00
msg = printf(M2C(this.msgs.fileSaveError.msg), (ex.inner || ex).toString());
2016-01-25 15:34:57 +00:00
etype = 'error';
quit = false;
break;
case HMSTATUS.invalidFormat:
2016-01-26 15:58:10 +00:00
ex.data.forEach(function(d) {
return msg += printf(M2C(this.msgs.invalidFormat.msg, 'bold'), ex.theme.name.toUpperCase(), d.format.toUpperCase());
2016-01-25 15:34:57 +00:00
}, this);
break;
case HMSTATUS.missingParam:
2016-01-26 15:58:10 +00:00
msg = printf(M2C(this.msgs.missingParam.msg), ex.expected, ex.helper);
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.invalidHelperUse:
2016-01-26 15:58:10 +00:00
msg = printf(M2C(this.msgs.invalidHelperUse.msg), ex.helper);
if (ex.error) {
msg += '\n--> ' + assembleError.call(this, extend(true, {}, ex, {
fluenterror: ex.error
})).msg;
2016-01-25 15:34:57 +00:00
}
quit = false;
etype = 'warning';
break;
case HMSTATUS.notOnPath:
2016-01-26 15:58:10 +00:00
msg = printf(M2C(this.msgs.notOnPath.msg, 'bold'), ex.engine);
2016-01-25 15:34:57 +00:00
quit = false;
etype = 'error';
break;
case HMSTATUS.readError:
2016-01-26 15:58:10 +00:00
if (!ex.quiet) {
console.error(printf(M2C(this.msgs.readError.msg, 'red'), ex.file));
}
2016-01-25 15:34:57 +00:00
msg = ex.inner.toString();
etype = 'error';
break;
case HMSTATUS.mixedMerge:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.mixedMerge.msg);
2016-01-25 15:34:57 +00:00
quit = false;
break;
case HMSTATUS.invokeTemplate:
2016-01-26 15:58:10 +00:00
msg = M2C(this.msgs.invokeTemplate.msg, 'red');
msg += M2C('\n' + WRAP(ex.inner.toString(), {
width: 60,
indent: ' '
}), 'gray');
2016-01-25 15:34:57 +00:00
etype = 'custom';
break;
case HMSTATUS.compileTemplate:
etype = 'error';
break;
case HMSTATUS.themeLoad:
2016-01-26 15:58:10 +00:00
msg = M2C(printf(this.msgs.themeLoad.msg, ex.attempted.toUpperCase()), 'red');
if (ex.inner && ex.inner.fluenterror) {
msg += M2C('\nError: ', 'red') + assembleError.call(this, ex.inner).msg;
2016-01-25 15:34:57 +00:00
}
quit = true;
etype = 'custom';
break;
case HMSTATUS.parseError:
2016-01-26 15:58:10 +00:00
if (SyntaxErrorEx.is(ex.inner)) {
console.error(printf(M2C(this.msgs.readError.msg, 'red'), ex.file));
se = new SyntaxErrorEx(ex, ex.raw);
if ((se.line != null) && (se.col != null)) {
msg = printf(M2C(this.msgs.parseError.msg[0], 'red'), se.line, se.col);
} else if (se.line != null) {
msg = printf(M2C(this.msgs.parseError.msg[1], 'red'), se.line);
} else {
msg = M2C(this.msgs.parseError.msg[2], 'red');
}
} else if (ex.inner && (ex.inner.line != null) && (ex.inner.col != null)) {
msg = printf(M2C(this.msgs.parseError.msg[0], 'red'), ex.inner.line, ex.inner.col);
2016-01-26 15:58:10 +00:00
} else {
2016-01-25 15:34:57 +00:00
msg = ex;
}
etype = 'error';
2016-02-02 02:14:36 +00:00
break;
case HMSTATUS.createError:
msg = printf(M2C(this.msgs.createError.msg), ex.inner.path);
etype = 'error';
2016-02-13 05:11:52 +00:00
break;
case HMSTATUS.validateError:
msg = printf(M2C(this.msgs.validateError.msg), ex.inner.toString());
etype = 'error';
2016-01-25 15:34:57 +00:00
}
return {
2016-01-26 15:58:10 +00:00
msg: msg,
withStack: withStack,
2016-01-25 15:34:57 +00:00
quit: quit,
etype: etype
};
2016-01-26 15:58:10 +00:00
};
2016-01-25 15:34:57 +00:00
2016-01-26 15:58:10 +00:00
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=error.js.map