diff --git a/lib/gatekeeper/utils.js b/lib/gatekeeper/utils.js index 3915bfee..8bb881d3 100644 --- a/lib/gatekeeper/utils.js +++ b/lib/gatekeeper/utils.js @@ -9,7 +9,7 @@ var _ = require('lodash'), Negotiator = require('negotiator'), url = require('url'); -exports.errorHandler = function(request, response, error, data) { +exports.errorHandler = function(request, response, error, errorSpecificData) { var availableMediaTypes = ['application/json', 'application/xml', 'text/csv', 'text/html']; // Prefer the format from the extension given in the URL. @@ -57,9 +57,9 @@ exports.errorHandler = function(request, response, error, data) { logger.error({ error_type: error }, 'Error data not found for error type: ' + error); } - data = _.merge({ + var data = _.merge({ base_url: request.base, - }, data || {}, commonErrorData, errorData); + }, commonErrorData); // Support legacy camel-case capitalization of variables. Moving forward, // we're trying to clean things up and standardize on snake-case. @@ -73,6 +73,8 @@ exports.errorHandler = function(request, response, error, data) { data.contactUrl = data.contact_url; } + data = _.merge(data, errorSpecificData || {}, errorData); + var prop; for(prop in data) { try { diff --git a/test/server/formatted_errors.js b/test/server/formatted_errors.js index 2a434477..cb403bbf 100644 --- a/test/server/formatted_errors.js +++ b/test/server/formatted_errors.js @@ -114,6 +114,7 @@ describe('formatted error responses', function() { error_data: { api_key_missing: { embedded: 'base_url: {{base_url}} signup_url: {{signup_url}} contact_url: {{contact_url}}', + embedded_legacy: 'baseUrl: {{baseUrl}} signupUrl: {{signupUrl}} contactUrl: {{contactUrl}}', }, }, error_templates: { @@ -124,7 +125,8 @@ describe('formatted error responses', function() { '"signupUrl": {{signupUrl}},' + '"contact_url": {{contact_url}},' + '"contactUrl": {{contactUrl}},' + - '"embedded": {{embedded}} ' + + '"embedded": {{embedded}},' + + '"embedded_legacy": {{embedded_legacy}} ' + '}', }, }, @@ -187,6 +189,14 @@ describe('formatted error responses', function() { done(); }); }); + + it('substitutes legacy camel case variables embedded inside of other variables', function(done) { + request.get('http://localhost:9333/embedded_legacy.json', function(error, response, body) { + var data = JSON.parse(body); + data.embedded_legacy.should.eql('baseUrl: http://localhost:9333 signupUrl: http://localhost:9333 contactUrl: http://localhost:9333/contact/'); + done(); + }); + }); }); describe('format validation', function() {