Skip to content
This repository has been archived by the owner on Jan 7, 2018. It is now read-only.

Better error handling for if error data is unexpectedly not an object. #18

Merged
merged 1 commit into from
Jun 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/gatekeeper/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ exports.errorHandler = function(request, response, error, data) {
// leading space before the XML declaration.
var templateContent = settings.error_templates[format].replace(/^\s+|\s+$/g, '');
var errorData = settings.error_data[error];
if(!errorData) {
if(!errorData || !_.isPlainObject(errorData)) {
errorData = settings.error_data.internal_server_error;
logger.error({ error_type: error }, 'Error data not found for error type: ' + error);
}
Expand Down
73 changes: 72 additions & 1 deletion test/server/formatted_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,78 @@ describe('formatted error responses', function() {
});
});

describe('invalid data', function() {
shared.runServer({
apis: [
{
frontend_host: 'localhost',
backend_host: 'example.com',
url_matches: [
{
frontend_prefix: '/',
backend_prefix: '/',
}
],
settings: {
error_data: {
api_key_missing: 'Foo',
api_key_invalid: 9,
api_key_unauthorized: [],
api_key_disabled: null,
},
},
sub_settings: [
{
http_method: 'any',
regex: '^/private',
settings: {
required_roles: ['private'],
},
},
],
},
],
});

it('returns internal error when error data is unexpectedly a string', function(done) {
request.get('http://localhost:9333/hello.json', function(error, response, body) {
response.statusCode.should.eql(500);
var data = JSON.parse(body);
data.error.code.should.eql('INTERNAL_SERVER_ERROR');
done();
});
});

it('returns internal error when error data is unexpectedly a number', function(done) {
request.get('http://localhost:9333/hello.json?api_key=invalid-key', function(error, response, body) {
response.statusCode.should.eql(500);
var data = JSON.parse(body);
data.error.code.should.eql('INTERNAL_SERVER_ERROR');
done();
});
});

it('returns internal error when error data is unexpectedly an array', function(done) {
request.get('http://localhost:9333/private.json?api_key=' + this.apiKey, function(error, response, body) {
response.statusCode.should.eql(500);
var data = JSON.parse(body);
data.error.code.should.eql('INTERNAL_SERVER_ERROR');
done();
});
});

it('returns default error data when the error data is null', function(done) {
Factory.create('api_user', { disabled_at: new Date() }, function(user) {
request.get('http://localhost:9333/hello.json?api_key=' + user.api_key, function(error, response, body) {
response.statusCode.should.eql(500);
var data = JSON.parse(body);
data.error.code.should.eql('INTERNAL_SERVER_ERROR');
done();
});
});
});
});

describe('invalid templates', function() {
shared.runServer({
apis: [
Expand Down Expand Up @@ -259,6 +331,5 @@ describe('formatted error responses', function() {
done();
});
});

});
});