From 025a6abdaad02155303d54904c4bb4fe49a685de Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 1 Dec 2014 16:55:59 +0100 Subject: [PATCH] Closes stream responses on error. Fixes #188 --- NEWS.md | 4 ++++ app/models/formats/json.js | 20 ++++++++++++++++---- test/acceptance/app.test.js | 23 +++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index df360bd6..b7309499 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,10 @@ 1.19.1 - 2014-mm-dd ------------------- +Bug fixes: + + * Closes stream responses on error (#188) + 1.19.0 - 2014-11-21 ------------------- diff --git a/app/models/formats/json.js b/app/models/formats/json.js index dfb662a9..8de5e842 100644 --- a/app/models/formats/json.js +++ b/app/models/formats/json.js @@ -1,9 +1,11 @@ var pg = require('./pg'), util = require('util'), + PgErrorHandler = require(global.settings.app_root + '/app/postgresql/error_handler'), _ = require('underscore'); function JsonFormat() { this.buffer = ''; + this.lastKnownResult = {}; } JsonFormat.prototype = new pg('json'); @@ -11,6 +13,7 @@ JsonFormat.prototype = new pg('json'); JsonFormat.prototype._contentType = "application/json; charset=utf-8"; JsonFormat.prototype.formatResultFields = function(flds) { + flds = flds || []; var nfields = {}; for (var i=0; i 0 ) { var notices = {}, diff --git a/test/acceptance/app.test.js b/test/acceptance/app.test.js index 86bc882b..7ff75029 100644 --- a/test/acceptance/app.test.js +++ b/test/acceptance/app.test.js @@ -1441,4 +1441,27 @@ test('GET with callback must return 200 status error even if it is an error', fu }); }); + test('GET with callback must return 200 status error even if it is an error', function(done){ + assert.response( + app, + { + url: "/api/v1/sql?" + querystring.stringify({ + q: "SELECT 100/(cartodb_id - 3) cdb_ratio FROM untitle_table_4" + }), + headers: {host: 'vizzuality.cartodb.com'}, + method: 'GET' + }, + { + status: 200 + }, + function(res) { + var parsedBody = JSON.parse(res.body); + assert.equal(parsedBody.rows.length, 2); + assert.deepEqual(parsedBody.fields, {"cdb_ratio": {"type": "number"}}); + assert.deepEqual(parsedBody.error, ["division by zero"]); + done(); + } + ); + }); + });