MXS-1300: Improve overall error handling

All errors are now returned as rejected Promises. This will make the error
reporting more consistent with the tested output as well as the actual
output.

Added more tests for error cases and fixed minor argument processing bugs.
This commit is contained in:
Markus Mäkelä 2017-07-22 03:57:37 +03:00
parent c9f3d014d6
commit ba5a321ec3
2 changed files with 69 additions and 15 deletions

View File

@ -35,6 +35,10 @@ module.exports = function() {
this.argv = argv
if (!argv.hosts || argv.hosts.length < 1) {
argv.reject("No hosts defined")
}
return pingCluster(argv.hosts)
.then(function() {
var promises = []
@ -45,11 +49,11 @@ module.exports = function() {
return Promise.all(promises)
.catch(function(err) {
argv.reject()
argv.reject(err)
})
}, function(err) {
// One of the HTTP request pings to the cluster failed, log the error
logError(JSON.stringify(err.error, null, 4))
argv.reject(JSON.stringify(err.error, null, 4))
})
}
@ -189,15 +193,14 @@ module.exports = function() {
}
}, function(err) {
if (err.response && err.response.body) {
logError(JSON.stringify(err.response.body, null, 4))
return error(JSON.stringify(err.response.body, null, 4))
} else if (err.statusCode) {
logError('Server responded with ' + err.statusCode)
return error('Server responded with: ' + err.statusCode)
} else if (err.error) {
logError(JSON.stringify(err.error, null, 4))
return error(JSON.stringify(err.error, null, 4))
} else {
logError('Undefined error: ' + JSON.stringify(err, null, 4))
return error('Undefined error: ' + JSON.stringify(err, null, 4))
}
return Promise.reject()
})
}
@ -206,13 +209,8 @@ module.exports = function() {
.then(this.argv.resolve, this.argv.reject)
}
this.logError = function(err) {
this.logger.error(colors.red('Error:'), err)
}
this.error = function(err) {
logger.log(colors.red('Error:'), err)
return Promise.reject()
return Promise.reject(colors.red('Error: ') + err)
}
}
@ -270,7 +268,7 @@ function pingCluster(hosts) {
var promises = []
hosts.forEach(function(i) {
promises.push(request('http://' + i + '/v1'))
promises.push(request(getUri(i, false, '')))
})
return Promise.all(promises)

View File

@ -4,9 +4,9 @@ describe("Library invocation", function() {
before(startMaxScale)
var ctrl = require('../lib/core.js')
var opts = { extra_args: [ '--quiet'] }
it('extra options', function() {
var opts = { extra_args: [ '--quiet'] }
return ctrl.execute('list servers'.split(' '), opts)
.should.be.fulfilled
})
@ -16,5 +16,61 @@ describe("Library invocation", function() {
.should.be.fulfilled
})
it('multiple hosts', function() {
var opts = { extra_args: [ '--quiet', '--hosts', '127.0.0.1:8989', 'localhost:8989'] }
return ctrl.execute('list servers'.split(' '), opts)
.should.be.fulfilled
})
it('no hosts', function() {
var opts = { extra_args: [ '--quiet', '--hosts'] }
return ctrl.execute('list servers'.split(' '), opts)
.should.be.rejected
})
it('TSV output', function() {
var opts = { extra_args: [ '--quiet', '--tsv'] }
return ctrl.execute('list servers'.split(' '), opts)
.then(function() {
return ctrl.execute('show server server1'.split(' '), opts)
})
.should.be.fulfilled
})
it('secure mode', function() {
// The test is run in HTTP mode so a HTTPS request should fail
var opts = { extra_args: [ '--quiet', '--secure'] }
return ctrl.execute('list servers'.split(' '), opts)
.should.be.rejected
})
// These should be last
it('user credentials', function() {
var opts1 = { extra_args: [ '--quiet'] }
var opts2 = { extra_args: [ '--quiet', '--user', 'test', '--password', 'test'] }
return ctrl.execute('create user test test'.split(' '), opts1)
.then(function() {
return ctrl.execute('alter maxscale admin_auth true'.split(' '), opts1)
})
.then(function() {
return ctrl.execute('list servers'.split(' '), opts2)
})
.should.be.fulfilled
})
it('bad user credentials', function() {
var opts = { extra_args: [ '--quiet', '--user', 'not-a-user', '--password', 'not-a-password'] }
return ctrl.execute('list servers'.split(' '), opts)
.should.be.rejected
})
it('connection failure', function() {
stopMaxScale()
.then(function() {
return ctrl.execute('list servers'.split(' '))
.should.be.rejected
})
})
after(stopMaxScale)
});