handling copyto errors

This commit is contained in:
Simon Martín 2018-05-24 19:48:24 +02:00
parent 3d8f45afd8
commit e2bba967f9
2 changed files with 22 additions and 20 deletions

View File

@ -10,6 +10,7 @@ const rateLimitsMiddleware = require('../middlewares/rate-limit');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware; const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
const { getFormatFromCopyQuery } = require('../utils/query_info'); const { getFormatFromCopyQuery } = require('../utils/query_info');
const BunyanLogger = require('../services/bunyanLogger'); const BunyanLogger = require('../services/bunyanLogger');
const errorHandlerFactory = require('../services/error_handler_factory');
const zlib = require('zlib'); const zlib = require('zlib');
const PSQL = require('cartodb-psql'); const PSQL = require('cartodb-psql');
@ -92,7 +93,12 @@ function handleCopyTo (logger) {
const copyToStream = copyTo(sql); const copyToStream = copyTo(sql);
const pgstream = client.query(copyToStream); const pgstream = client.query(copyToStream);
pgstream pgstream
.on('error', next) .on('error', err => {
pgstream.unpipe(res);
const errorHandler = errorHandlerFactory(err);
res.write(JSON.stringify(errorHandler.getResponse()));
res.end();
})
.on('data', data => metrics.size += data.length) .on('data', data => metrics.size += data.length)
.on('end', () => { .on('end', () => {
metrics.time = (Date.now() - startTime) / 1000; metrics.time = (Date.now() - startTime) / 1000;

View File

@ -185,7 +185,8 @@ describe('copy-endpoints timeout', function() {
headers: {host: 'vizzuality.cartodb.com'}, headers: {host: 'vizzuality.cartodb.com'},
method: 'GET' method: 'GET'
}, },
function(err, res) { function(err) {
assert.ifError(err);
assert.response(server, { assert.response(server, {
url: "/api/v1/sql/copyfrom?" + querystring.stringify({ url: "/api/v1/sql/copyfrom?" + querystring.stringify({
q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)" q: "COPY copy_endpoints_test (id, name) FROM STDIN WITH (FORMAT CSV, DELIMITER ',', HEADER true)"
@ -214,20 +215,19 @@ describe('copy-endpoints timeout', function() {
headers: {host: 'vizzuality.cartodb.com'}, headers: {host: 'vizzuality.cartodb.com'},
method: 'GET' method: 'GET'
}, },
function(err, res) { done);
done();
});
}); });
}); });
}); });
it('should fail with copyto and timeout', function(done){ it('should fail with copyto and timeout', function(done){
assert.response(server, { assert.response(server, {
url: '/api/v1/sql?q=set statement_timeout = 1', url: '/api/v1/sql?q=set statement_timeout = 20',
headers: {host: 'vizzuality.cartodb.com'}, headers: {host: 'vizzuality.cartodb.com'},
method: 'GET' method: 'GET'
}, },
function(err, res) { function(err) {
assert.ifError(err);
assert.response(server, { assert.response(server, {
url: "/api/v1/sql/copyto?" + querystring.stringify({ url: "/api/v1/sql/copyto?" + querystring.stringify({
q: 'COPY populated_places_simple_reduced TO STDOUT', q: 'COPY populated_places_simple_reduced TO STDOUT',
@ -237,25 +237,21 @@ describe('copy-endpoints timeout', function() {
method: 'GET' method: 'GET'
},{}, function(err, res) { },{}, function(err, res) {
assert.ifError(err); assert.ifError(err);
// assert.deepEqual(JSON.parse(res.body), { const error = {
// error: [ error:["You are over platform's limits. Please contact us to know more details"],
// 'You are over platform\'s limits. Please contact us to know more details' context:"limit",
// ], detail:"datasource"
// context: 'limit', };
// detail: 'datasource' const expectedError = res.body.substring(res.body.length - JSON.stringify(error).length);
// }); assert.deepEqual(JSON.parse(expectedError), error);
console.log(res.body);
assert.response(server, { assert.response(server, {
url: "/api/v1/sql?q=set statement_timeout = 2000", url: "/api/v1/sql?q=set statement_timeout = 2000",
headers: {host: 'vizzuality.cartodb.com'}, headers: {host: 'vizzuality.cartodb.com'},
method: 'GET' method: 'GET'
}, },
function(err, res) { done);
done();
});
}); });
}); });
}); });
}) });