Retur a proper error message when ogr2ogr command fails

This commit is contained in:
Daniel García Aubert 2017-04-18 11:40:37 +02:00
parent d60e2107af
commit 6dba06130b
2 changed files with 20 additions and 27 deletions

View File

@ -173,34 +173,25 @@ OgrFormat.prototype.toOGR = function(options, out_format, out_filename, callback
next(err); next(err);
}); });
var stdout = ''; var stderrData = [];
child.stdout.on('data', function(data) { child.stderr.setEncoding('utf8');
stdout += data;
//console.log('stdout: ' + data);
});
var stderr;
var logErrPat = new RegExp(/^ERROR/);
child.stderr.on('data', function (data) { child.stderr.on('data', function (data) {
data = data.toString(); // know of a faster way ? stderrData.push(data);
// Store only the first ERROR line
if ( ! stderr && data.match(logErrPat) ) {
stderr = data;
}
console.log('ogr2ogr stderr: ' + data);
}); });
child.on('exit', function(code) { child.on('exit', function(code) {
if ( code ) { if (code !== 0) {
console.log("ogr2ogr exited with code " + code); var errMessage = 'ogr2ogr command return code ' + code;
var emsg = stderr ? stderr.split('\n')[0] : ( "unknown ogr2ogr error (code " + code + ")" ); if (stderrData.length > 0) {
// TODO: add more info about this error ? errMessage += ', Error: ' + stderrData.join('\n');
//if ( RegExp(/attempt to write non-.*geometry.*to.*type shapefile/i).exec(emsg) )
next(new Error(emsg));
} else {
next(null);
} }
return next(new Error(errMessage));
}
return next();
}); });
}, },
function finish(err) { function finish(err) {
callback(err, out_filename); callback(err, out_filename);

View File

@ -197,8 +197,9 @@ it('mixed type geometry', function(done){
assert.deepEqual(res.headers['content-disposition'], 'inline'); assert.deepEqual(res.headers['content-disposition'], 'inline');
assert.equal(res.statusCode, 400, res.statusCode + ': ' +res.body); assert.equal(res.statusCode, 400, res.statusCode + ': ' +res.body);
var parsedBody = JSON.parse(res.body); var parsedBody = JSON.parse(res.body);
var expectedBody = {"error":["ERROR 1: Attempt to write non-point (LINESTRING) geometry to point shapefile."]}; var error = parsedBody.error[0];
assert.deepEqual(parsedBody, expectedBody); var expectedError = /Attempt to write non-point \(LINESTRING\) geometry to point shapefile/g;
assert.ok(expectedError.test(error), error);
done(); done();
}); });
}); });
@ -222,8 +223,9 @@ it('errors are not confused with warnings', function(done){
assert.deepEqual(res.headers['content-disposition'], 'inline'); assert.deepEqual(res.headers['content-disposition'], 'inline');
assert.equal(res.statusCode, 400, res.statusCode + ': ' +res.body); assert.equal(res.statusCode, 400, res.statusCode + ': ' +res.body);
var parsedBody = JSON.parse(res.body); var parsedBody = JSON.parse(res.body);
var expectedBody = {"error":["ERROR 1: Attempt to write non-point (LINESTRING) geometry to point shapefile."]}; var error = parsedBody.error[0];
assert.deepEqual(parsedBody, expectedBody); var expectedError = /Attempt to write non-point \(LINESTRING\) geometry to point shapefile/g;
assert.ok(expectedError.test(error), error);
done(); done();
}); });
}); });