Highlight errors in red

- Move error translation logic to resin-io/resin-cli-errors.
- Force `process.exit()`.
This commit is contained in:
Juan Cruz Viotti 2015-09-11 14:45:48 +03:00
parent 3c0acaa7da
commit 904b9f07fb
4 changed files with 19 additions and 160 deletions

View File

@ -1,47 +1,21 @@
(function() {
var _, os;
var chalk, errors;
_ = require('lodash');
chalk = require('chalk');
os = require('os');
errors = require('resin-cli-errors');
exports.handle = function(error, exit) {
var errorCode, message;
if (exit == null) {
exit = true;
}
if ((error == null) || !(error instanceof Error)) {
exports.handle = function(error) {
var message;
message = errors.interpret(error);
if (message == null) {
return;
}
if (process.env.DEBUG) {
console.error(error.stack);
} else {
if (error.code === 'EISDIR') {
console.error("File is a directory: " + error.path);
} else if (error.code === 'ENOENT') {
console.error("No such file or directory: " + error.path);
} else if (error.code === 'EACCES' || error.code === 'EPERM') {
message = 'You don\'t have enough privileges to run this operation.\n';
if (os.platform() === 'win32') {
message += 'Run a new Command Prompt as administrator and try running this command again.';
} else {
message += 'Try running this command again prefixing it with `sudo`.';
}
console.error(message);
} else if (error.code === 'ENOGIT') {
console.error('Git is not installed on this system.\nHead over to http://git-scm.com to install it and run this command again.');
} else if (error.message != null) {
console.error(error.message);
}
}
if (_.isNumber(error.exitCode)) {
errorCode = error.exitCode;
} else {
errorCode = 1;
}
if (exit) {
return process.exit(errorCode);
message = error.stack;
}
console.error(chalk.red(message));
return process.exit(error.exitCode || 1);
};
}).call(this);

View File

@ -1,40 +1,12 @@
_ = require('lodash')
os = require('os')
chalk = require('chalk')
errors = require('resin-cli-errors')
exports.handle = (error, exit = true) ->
return if not error? or error not instanceof Error
exports.handle = (error) ->
message = errors.interpret(error)
return if not message?
if process.env.DEBUG
console.error(error.stack)
else
if error.code is 'EISDIR'
console.error("File is a directory: #{error.path}")
message = error.stack
else if error.code is 'ENOENT'
console.error("No such file or directory: #{error.path}")
else if error.code is 'EACCES' or error.code is 'EPERM'
message = 'You don\'t have enough privileges to run this operation.\n'
if os.platform() is 'win32'
message += 'Run a new Command Prompt as administrator and try running this command again.'
else
message += 'Try running this command again prefixing it with `sudo`.'
console.error(message)
else if error.code is 'ENOGIT'
console.error '''
Git is not installed on this system.
Head over to http://git-scm.com to install it and run this command again.
'''
else if error.message?
console.error(error.message)
if _.isNumber(error.exitCode)
errorCode = error.exitCode
else
errorCode = 1
process.exit(errorCode) if exit
console.error(chalk.red(message))
process.exit(error.exitCode or 1)

View File

@ -49,6 +49,7 @@
"nplugm": "^3.0.0",
"npm": "^2.13.0",
"open": "0.0.5",
"resin-cli-errors": "^1.0.0",
"resin-cli-events": "^1.0.2",
"resin-cli-form": "^1.2.1",
"resin-cli-visuals": "^1.2.2",

View File

@ -1,88 +0,0 @@
chai = require('chai')
expect = chai.expect
chai.use(require('sinon-chai'))
_ = require('lodash')
sinon = require('sinon')
errors = require('../lib/errors')
describe 'Errors:', ->
describe '#handle()', ->
it 'should log the error message to stderr', ->
message = 'Hello World'
logErrorStub = sinon.stub(console, 'error')
error = new Error(message)
errors.handle(error, false)
expect(logErrorStub).to.have.been.calledWith(message)
logErrorStub.restore()
it 'should do nothing if error is not an instance of Error', ->
logErrorStub = sinon.stub(console, 'error')
for item in [
undefined
null
[ 1, 2, 3 ]
'Hello'
{ message: 'foo bar' }
]
errors.handle(item, false)
expect(logErrorStub).to.not.have.been.called
logErrorStub.restore()
checkProcessExitOption = (error, value, expectations) ->
processExitStub = sinon.stub(process, 'exit')
logErrorStub = sinon.stub(console, 'error')
errors.handle(error, value)
expectations(processExitStub, logErrorStub)
processExitStub.restore()
logErrorStub.restore()
it 'should exit if the last parameter is true', ->
error = new Error()
checkProcessExitOption error, true, (processExitStub) ->
expect(processExitStub).to.have.been.calledWith(1)
it 'should not exit if the last parameter is false', ->
error = new Error()
checkProcessExitOption error, false, (processExitStub) ->
expect(processExitStub).to.not.have.been.called
it 'should handle a custom error exit code from the error instance', ->
error = new Error()
error.exitCode = 123
checkProcessExitOption error, true, (processExitStub) ->
expect(processExitStub).to.have.been.calledWith(123)
it 'should print stack trace if DEBUG is set', ->
process.env.DEBUG = true
error = new Error()
checkProcessExitOption error, false, (processExitStub, logErrorStub) ->
expect(logErrorStub).to.have.been.calledOnce
expect(logErrorStub).to.have.been.calledWith(error.stack)
delete process.env.DEBUG
it 'should handle EISDIR', ->
error = new Error()
error.code = 'EISDIR'
error.path = 'hello'
checkProcessExitOption error, false, (processExitStub, logErrorStub) ->
expect(logErrorStub).to.have.been.calledOnce
expect(logErrorStub).to.have.been.calledWith('File is a directory: hello')
it 'should handle ENOENT', ->
error = new Error()
error.code = 'ENOENT'
error.path = 'hello'
checkProcessExitOption error, false, (processExitStub, logErrorStub) ->
expect(logErrorStub).to.have.been.calledOnce
expect(logErrorStub).to.have.been.calledWith('No such file or directory: hello')
it 'should handle EACCES', ->
error = new Error()
error.code = 'EACCES'
checkProcessExitOption error, false, (processExitStub, logErrorStub) ->
expect(logErrorStub).to.have.been.calledOnce
expect(logErrorStub.getCall(0).args[0]).to.match(/^You don\'t have enough privileges to run this operation./)