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

View File

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