Refactor error handling

This commit is contained in:
Gautier Pelloux-Prayer 2019-08-07 18:13:40 +02:00
parent 5969c43208
commit d00fe15ff1

View file

@ -120,6 +120,11 @@ BR.NogoAreas = L.Control.extend({
return L.DomUtil.create('div'); return L.DomUtil.create('div');
}, },
displayUploadError: function(message) {
$('#nogoError').text(message ? message : '');
$('#nogoError').css('display', message ? 'block' : 'none');
},
uploadNogos: function() { uploadNogos: function() {
var self = this; var self = this;
@ -138,10 +143,9 @@ BR.NogoAreas = L.Control.extend({
resolve(reader.result); resolve(reader.result);
}; };
reader.onerror = function() { reader.onerror = function() {
$('#nogoError').text( self.displayUploadError(
'Could not load file: ' + reader.error.message 'Could not load file: ' + reader.error.message
); );
$('#nogoError').css('display', 'block');
}; };
reader.readAsText(nogoFile); reader.readAsText(nogoFile);
@ -149,26 +153,26 @@ BR.NogoAreas = L.Control.extend({
return JSON.parse(response); return JSON.parse(response);
}); });
} else { } else {
$('#nogoError').text('Missing file or URL.'); // FIXME: use form validator instead
$('#nogoError').css('display', 'block'); self.displayUploadError('Missing file or URL.');
return false; return false;
} }
var nogoWeight = parseFloat($('#nogoWeight').val()); var nogoWeight = parseFloat($('#nogoWeight').val());
if (isNaN(nogoWeight)) { if (isNaN(nogoWeight)) {
$('#nogoError').text('Missing default nogo weight.'); // FIXME: use form validator instead
$('#nogoError').css('display', 'block'); self.displayUploadError('Missing default nogo weight.');
return false; return false;
} }
var nogoRadius = parseFloat($('#nogoRadius').val()); var nogoRadius = parseFloat($('#nogoRadius').val());
if (isNaN(nogoRadius) || nogoRadius < 0) { if (isNaN(nogoRadius) || nogoRadius < 0) {
$('#nogoError').text('Invalid default nogo radius.'); // FIXME: use form validator instead
$('#nogoError').css('display', 'block'); self.displayUploadError('Invalid default nogo radius.');
return false; return false;
} }
var nogoBuffer = parseFloat($('#nogoBuffer').val()); var nogoBuffer = parseFloat($('#nogoBuffer').val());
if (isNaN(nogoBuffer)) { if (isNaN(nogoBuffer)) {
$('#nogoError').text('Invalid nogo buffering radius.'); // FIXME: use form validator instead
$('#nogoError').css('display', 'block'); self.displayUploadError('Invalid nogo buffering radius.');
return false; return false;
} }
@ -179,7 +183,7 @@ BR.NogoAreas = L.Control.extend({
if (turf.getGeom(feature)) { if (turf.getGeom(feature)) {
var maybeBufferedFeature = feature; var maybeBufferedFeature = feature;
// Eventually buffer GeoJSON // Eventually buffer GeoJSON
if (nogoBuffer != 0) { if (nogoBuffer !== 0) {
maybeBufferedFeature = turf.buffer( maybeBufferedFeature = turf.buffer(
maybeBufferedFeature, maybeBufferedFeature,
nogoBuffer, nogoBuffer,
@ -191,8 +195,9 @@ BR.NogoAreas = L.Control.extend({
}); });
if (cleanedGeoJSONFeatures.length === 0) { if (cleanedGeoJSONFeatures.length === 0) {
$('#nogoError').text('No valid area found in provided input.'); self.displayUploadError(
$('#nogoError').css('display', 'block'); 'No valid area found in provided input.'
);
return false; return false;
} }
@ -228,8 +233,7 @@ BR.NogoAreas = L.Control.extend({
}) })
}); });
self._fireUpdate(); self._fireUpdate();
$('#nogoError').text(''); self.displayUploadError(undefined);
$('#nogoError').css('display', 'none');
$('#loadNogos').modal('hide'); $('#loadNogos').modal('hide');
}); });
return false; return false;