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

288 lines
9.7 KiB
JavaScript
Raw Normal View History

2016-01-25 15:34:57 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Error-handling routines for HackMyResume.
@module cli/error
@license MIT. See LICENSE.md for details.
*/
2018-02-12 09:01:00 +00:00
var FS, HMSTATUS, M2C, PATH, SyntaxErrorEx, WRAP, _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
FS = require('fs');
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
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
2018-02-12 09:01:00 +00:00
/** Error handler for HackMyResume. All errors are handled here.
@class ErrorHandler */
module.exports = {
2016-01-26 15:58:10 +00:00
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;
2018-02-12 05:05:29 +00:00
// Short-circuit logging output if --silent is on
2016-01-26 15:58:10 +00:00
o = this.silent ? function() {} : _defaultLog;
if (ex.pass) {
2018-02-12 05:05:29 +00:00
// Special case; can probably be removed.
2016-01-26 15:58:10 +00:00
throw ex;
}
2018-02-12 05:05:29 +00:00
// Load error messages
2016-01-26 15:58:10 +00:00
this.msgs = this.msgs || require('./msg').errors;
2018-02-12 05:05:29 +00:00
// Handle packaged HMR exceptions
2016-01-26 15:58:10 +00:00
if (ex.fluenterror) {
2018-02-12 05:05:29 +00:00
// Output the error message
2016-01-26 15:58:10 +00:00
objError = assembleError.call(this, ex);
o(this['format_' + objError.etype](objError.msg));
2018-02-12 05:05:29 +00:00
// Output the stack (sometimes)
2016-01-26 15:58:10 +00:00
if (objError.withStack) {
stack = ex.stack || (ex.inner && ex.inner.stack);
stack && o(chalk.gray(stack));
2016-01-25 15:34:57 +00:00
}
2018-02-01 02:11:21 +00:00
if (shouldExit || ex.exit) {
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 {
2018-02-12 05:05:29 +00:00
// Handle raw exceptions
2016-01-26 15:58:10 +00:00
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() {
2018-02-12 09:01:00 +00:00
return console.log.apply(console.log, arguments); // eslint-disable-line no-console
2016-01-26 15:58:10 +00:00
};
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:
2018-02-12 05:05:29 +00:00
//msg = M2C( this.msgs.resumeNotFound.msg, 'yellow' );
2018-02-11 13:13:13 +00:00
msg += M2C(FS.readFileSync(PATH.resolve(__dirname, 'help/' + ex.verb + '.txt'), 'utf8'), 'white', 'yellow');
2016-01-25 15:34:57 +00:00
break;
case HMSTATUS.missingCommand:
2018-02-12 05:05:29 +00:00
// msg = M2C( this.msgs.missingCommand.msg + " (", 'yellow');
// msg += Object.keys( FCMD.verbs ).map( (v, idx, ar) ->
// return ( if idx == ar.length - 1 then chalk.yellow('or ') else '') +
// chalk.yellow.bold(v.toUpperCase());
// ).join( chalk.yellow(', ')) + chalk.yellow(").\n\n");
2018-02-11 13:13:13 +00:00
msg += M2C(FS.readFileSync(PATH.resolve(__dirname, 'help/use.txt'), 'utf8'), 'white', 'yellow');
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) {
2018-02-12 09:01:00 +00:00
// eslint-disable-next-line no-console
2016-01-26 15:58:10 +00:00
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)) {
2018-02-12 09:01:00 +00:00
// eslint-disable-next-line no-console
2016-01-26 15:58:10 +00:00
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:
2018-02-12 05:05:29 +00:00
// inner.code could be EPERM, EACCES, etc
2016-02-02 02:14:36 +00:00
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';
break;
case HMSTATUS.invalidOptionsFile:
msg = M2C(this.msgs.invalidOptionsFile.msg[0]);
if (SyntaxErrorEx.is(ex.inner)) {
2018-02-12 09:01:00 +00:00
// eslint-disable-next-line no-console
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);
} else {
msg += ex;
}
msg += this.msgs.invalidOptionsFile.msg[1];
etype = 'error';
break;
case HMSTATUS.optionsFileNotFound:
msg = M2C(this.msgs.optionsFileNotFound.msg);
etype = 'error';
2018-01-30 07:34:58 +00:00
break;
case HMSTATUS.unknownSchema:
msg = M2C(this.msgs.unknownSchema.msg[0]);
2018-02-12 05:05:29 +00:00
//msg += "\n" + M2C( @msgs.unknownSchema.msg[1], 'yellow' )
2018-01-30 07:34:58 +00:00
etype = 'error';
2018-02-01 02:11:21 +00:00
break;
case HMSTATUS.themeHelperLoad:
msg = printf(M2C(this.msgs.themeHelperLoad.msg), ex.glob);
etype = 'error';
break;
case HMSTATUS.invalidSchemaVersion:
msg = printf(M2C(this.msgs.invalidSchemaVersion.msg), ex.data);
etype = 'error';
2016-01-25 15:34:57 +00:00
}
return {
2018-02-12 05:05:29 +00:00
msg: msg, // The error message to display
withStack: withStack, // Whether to include the stack
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