Skip to content

Commit

Permalink
Merge pull request #25 from telefonicaid/feature/5759_error_messages
Browse files Browse the repository at this point in the history
improve message output, change error response from html to plain/text
  • Loading branch information
hmunfru committed Feb 18, 2016
2 parents 3c085f9 + 81bc7f3 commit 8f2c89b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 46 deletions.
36 changes: 15 additions & 21 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,33 @@ function setup (app) {

// catch 404 and forward to error handler
app.use(function(req, res) {
var err = new Error('Path not found');
err.status = 404;
logger.warn(err);
err.stack = '';
res.status(err.status);
res.render('error', {
message: err.message,
error: err
});
var err = new Error('Path not found');
err.status = 404;
logger.warn(err);
err.stack = '';
res.status(err.status);
res.setHeader('content-type', 'plain/text');
res.send(err.message);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
app.use(function(err, req, res) {
res.status(err.status || 500);
res.setHeader('content-type', 'plain/text');
res.send(err.message);
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
res.status(err.status || 500);
res.send(err.message);
res.setHeader('content-type', 'plain/text');
});
}

Expand Down
12 changes: 9 additions & 3 deletions lib/routes/openstack.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,21 @@ function getValidateToken(adminToken, token, callback) {
var region = parseRegionNameFromUserName(resultObject.token.user.name);
callback(region);
} else {
var message = 'Error validating token: ' + resultObject.error.message;
var message = 'Problem validating token: ' + resultObject.error.message;
logger.warn(message);
var error = new Error();
error.statusCode = 401;
error.message = message;
throw error;
}
} catch (ex) {
logger.warn('Error validating token or saving key file: %s', ex.message);
Error.stackTraceLimit = 0;
logger.warn('Problem validating token or saving key file: ' + ex.message);
throw ex;
}
});


});
req.on('error', function (e) {
if (e.code === 'ECONNRESET') {
Expand All @@ -126,10 +129,13 @@ function getValidateToken(adminToken, token, callback) {
logger.warn('Timeout in the connection with keystone. Time exceed');
});

req.on('finish', function handleFinishEvent() {
logger.debug('Request finished successfully.');
}
);

req.end();


}

/**
Expand Down
19 changes: 9 additions & 10 deletions lib/routes/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ function setKeysPath(path) {
/**
* validate exists region
* @param {String} region name
*/
function validateRegion(name) {
* @param {String} key value {sshkey | gpgkey}
* */
function validateRegion(name, key) {
var result = false;
var files = fs.readdirSync(keysPath);

var regex = new RegExp(name + '.*key');
var regex = new RegExp(name + '.' + key);
files.forEach(function (item) {
if (regex.test(item)) {
result = true;
Expand Down Expand Up @@ -100,7 +101,7 @@ function getKey(req, res) {
logger.info('Request ' + key + ' for ' + region);
var statusCode = 404;

if (validateKey(key) && validateRegion(region)) {
if (validateKey(key) && validateRegion(region, key)) {
var file = fs.readFileSync(keysPath + region + '.' + key, 'binary');
res.type('text/plain');
res.setHeader('Content-Length', file.length);
Expand Down Expand Up @@ -197,7 +198,7 @@ function postKey(req, res) {
}else {
res.status(statusCode);
}
res.end();
res.send(ex.message);
}

}
Expand All @@ -209,13 +210,11 @@ function postKey(req, res) {
function methodNotAllowed(req, res) {
var err = new Error('Method not allowed');
err.status = 405;
logger.warn(err);
logger.warn('Method not allowed:' + err);
err.stack = '';
res.setHeader('content-type', 'plain/text');
res.status(err.status);
res.render('error', {
message: err.message,
error: err
});
res.send(err.message);
}

/* GET v1 page. */
Expand Down
6 changes: 0 additions & 6 deletions lib/views/error.jade

This file was deleted.

14 changes: 8 additions & 6 deletions test/unit/test_v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ suite('v1', function () {

test('should_return_error_with_invalid_region', function () {

var result = v1.validateRegion('invalidName');
var result = v1.validateRegion('invalidName', 'sshkey');
assert(result === false);

});

test('should_return_true_with_valid_region', function () {

var result = v1.validateRegion('region1');
var result = v1.validateRegion('region1', 'sshkey');

assert(result === true);

Expand Down Expand Up @@ -246,7 +246,7 @@ suite('v1', function () {
res = sinon.stub();

res.status = sinon.stub();
res.end = sinon.spy();
res.send = sinon.stub();
req.is = sinon.stub();
req.is.withArgs('text').returns(false);

Expand All @@ -255,7 +255,7 @@ suite('v1', function () {

//then
assert(res.status.withArgs(415).calledOnce);
assert(res.end.calledOnce);
assert(res.send.calledOnce);

});

Expand All @@ -265,14 +265,16 @@ suite('v1', function () {
res = sinon.stub();

res.status = sinon.stub();
res.render = sinon.stub();
res.setHeader = sinon.stub();
res.send = sinon.stub();

//when
v1.methodNotAllowed(req, res);

//then
assert(res.status.withArgs(405).calledOnce);
assert(res.render.calledOnce);
assert(res.setHeader.withArgs('content-type', 'plain/text').calledOnce);
assert(res.send.calledOnce);

});

Expand Down

0 comments on commit 8f2c89b

Please sign in to comment.