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 { getFormatFromCopyQuery } = require('../utils/query_info');
const BunyanLogger = require('../services/bunyanLogger');
const errorHandlerFactory = require('../services/error_handler_factory');
const zlib = require('zlib');
const PSQL = require('cartodb-psql');
@ -92,7 +93,12 @@ function handleCopyTo (logger) {
const copyToStream = copyTo(sql);
const pgstream = client.query(copyToStream);
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('end', () => {
metrics.time = (Date.now() - startTime) / 1000;

View File

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