From ce70e7252b997a88bd923d17e7a1b65fa8365b73 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 4 Aug 2014 15:56:43 +0200 Subject: [PATCH] Callback requests send 200 status error even if the query failed --- app/controllers/app.js | 18 ++++++++++++------ test/acceptance/app.test.js | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/controllers/app.js b/app/controllers/app.js index c7611e47..ec7e4dd2 100755 --- a/app/controllers/app.js +++ b/app/controllers/app.js @@ -560,18 +560,24 @@ function handleException(err, res){ res.header('X-SQLAPI-Profiler', res.req.profiler.toJSONString()); } - // if the exception defines a http status code, use that, else a 400 - if (!_.isUndefined(err.http_status)){ - res.send(msg, err.http_status); - } else { - res.send(msg, 400); - } + res.send(msg, getStatusError(err, res.req)); if ( res.req && res.req.profiler ) { res.req.profiler.sendStats(); } } +function getStatusError(err, req) { + var statusError = _.isUndefined(err.http_status) ? 400 : err.http_status; + + // JSONP has to return 200 status error + if (req && req.query && req.query.callback) { + statusError = 200; + } + + return statusError; +} + return app; } diff --git a/test/acceptance/app.test.js b/test/acceptance/app.test.js index c751d7ad..f40a17de 100644 --- a/test/acceptance/app.test.js +++ b/test/acceptance/app.test.js @@ -1421,4 +1421,19 @@ test('GET with callback param returns wrapped result set with callback as jsonp' }); }); +test('GET with callback must return 200 status error even if it is an error', function(done){ + assert.response(app, { + url: "/api/v1/sql?q=DROP%20TABLE%20untitle_table_4&callback=foo_jsonp", + headers: {host: 'vizzuality.cartodb.com'}, + method: 'GET' + },{}, function(res) { + assert.equal(res.statusCode, 200, res.statusCode + ': ' + res.body); + function foo_jsonp(body) { + assert.deepEqual(body, {"error":["must be owner of relation untitle_table_4"]}); + } + eval(res.body); + done(); + }); +}); + });