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:
@ -35,6 +35,10 @@ module.exports = function() {
|
|||||||
|
|
||||||
this.argv = argv
|
this.argv = argv
|
||||||
|
|
||||||
|
if (!argv.hosts || argv.hosts.length < 1) {
|
||||||
|
argv.reject("No hosts defined")
|
||||||
|
}
|
||||||
|
|
||||||
return pingCluster(argv.hosts)
|
return pingCluster(argv.hosts)
|
||||||
.then(function() {
|
.then(function() {
|
||||||
var promises = []
|
var promises = []
|
||||||
@ -45,11 +49,11 @@ module.exports = function() {
|
|||||||
|
|
||||||
return Promise.all(promises)
|
return Promise.all(promises)
|
||||||
.catch(function(err) {
|
.catch(function(err) {
|
||||||
argv.reject()
|
argv.reject(err)
|
||||||
})
|
})
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
// One of the HTTP request pings to the cluster failed, log the error
|
// 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) {
|
}, function(err) {
|
||||||
if (err.response && err.response.body) {
|
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) {
|
} else if (err.statusCode) {
|
||||||
logError('Server responded with ' + err.statusCode)
|
return error('Server responded with: ' + err.statusCode)
|
||||||
} else if (err.error) {
|
} else if (err.error) {
|
||||||
logError(JSON.stringify(err.error, null, 4))
|
return error(JSON.stringify(err.error, null, 4))
|
||||||
} else {
|
} 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)
|
.then(this.argv.resolve, this.argv.reject)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logError = function(err) {
|
|
||||||
this.logger.error(colors.red('Error:'), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.error = function(err) {
|
this.error = function(err) {
|
||||||
logger.log(colors.red('Error:'), err)
|
return Promise.reject(colors.red('Error: ') + err)
|
||||||
return Promise.reject()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,7 +268,7 @@ function pingCluster(hosts) {
|
|||||||
var promises = []
|
var promises = []
|
||||||
|
|
||||||
hosts.forEach(function(i) {
|
hosts.forEach(function(i) {
|
||||||
promises.push(request('http://' + i + '/v1'))
|
promises.push(request(getUri(i, false, '')))
|
||||||
})
|
})
|
||||||
|
|
||||||
return Promise.all(promises)
|
return Promise.all(promises)
|
||||||
|
@ -4,9 +4,9 @@ describe("Library invocation", function() {
|
|||||||
before(startMaxScale)
|
before(startMaxScale)
|
||||||
|
|
||||||
var ctrl = require('../lib/core.js')
|
var ctrl = require('../lib/core.js')
|
||||||
var opts = { extra_args: [ '--quiet'] }
|
|
||||||
|
|
||||||
it('extra options', function() {
|
it('extra options', function() {
|
||||||
|
var opts = { extra_args: [ '--quiet'] }
|
||||||
return ctrl.execute('list servers'.split(' '), opts)
|
return ctrl.execute('list servers'.split(' '), opts)
|
||||||
.should.be.fulfilled
|
.should.be.fulfilled
|
||||||
})
|
})
|
||||||
@ -16,5 +16,61 @@ describe("Library invocation", function() {
|
|||||||
.should.be.fulfilled
|
.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)
|
after(stopMaxScale)
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user