4

I'm trying to handle errors when doing drag and drop interaction for GPX files in OpenLayers 6.4.3, like for example when file being dropped has wrong format.

I found out that in the case of parse error there is now way to catch the error and inform user about that. The only visible consequence of error is this error in the console (but only for some browsers), which obviously comes from XML parser:

XML5619: Incorrect document syntax. Line: 1, Column 1

I tried to hook on interaction's error event, but it's not triggered at all.

Here is the relevant code:

var dragAndDropInteraction = new ol.interaction.DragAndDrop({
  formatConstructors: [
    ol.format.GPX
  ]
});

var map = new ol.Map({
  interactions: ol.interaction.defaults().extend([dragAndDropInteraction]),
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  target: 'map',
  view: new ol.View({
    center: [0, 0],
    zoom: 2
  })
});

dragAndDropInteraction.on('addfeatures', function(event) {
  var vectorSource = new ol.source.Vector({
    features: event.features
  });
  map.addLayer(new ol.layer.Vector({
    source: vectorSource,
    style: styleFunction
  }));
  map.getView().fit(
    vectorSource.getExtent(), {
      constrainResolution: false,
      padding: [20, 50, 20, 50]
    });
});

Is there a way to catch parse error and consequently inform user about that?

6
  • You can drag any file onto the map (beware large binary files can crash some browsers), and it tries all the supplied formats in sequence, so multiple errors could occur on every drop unless suppressed, or the file works with the first format. Each try is inside a try catch return null so I'm not sure why an error is being logged. The latest version should not fire addfeatures unless there are features.
    – Mike
    Oct 2, 2020 at 19:41
  • Just tried the latest official example at openlayers.org/en/latest/examples/drag-and-drop.html and got the same error when dragged Word file on the map. GPX worked OK.
    – TomazicM
    Oct 2, 2020 at 21:14
  • I realized now that I was playing with an ages old example from OL 4 times. It's true that with the latest OL 6.4.3 in the case of invalid format processing does not fire addfeatures event, but XML error still appears. It's no big deal, just annoying. And one other thing: How can I report invalid format since I don't know how to catch the error?
    – TomazicM
    Oct 2, 2020 at 21:49
  • I had a look at the source code. There's no real error handling, in the case of error it just dies. And obviously try catch does not catch XML syntax errors in the format.readFeatures(text, options) call.
    – TomazicM
    Oct 2, 2020 at 22:07
  • It seems to be browser specific. If I drop random files onto openlayers.org/en/latest/examples/drag-and-drop.html in Chrome there are no errors, but legacy Edge does show a DOMParser error. Edge also shows the "error" when I drop a valid .geojson because that format is not first in the list. But a file which is neither xml or json doesn't cause a JSON.parse error. It seems DOMParser is similar to network errors appearing on the log even when the code is handling them.
    – Mike
    Oct 2, 2020 at 22:41

1 Answer 1

2

The interaction is designed to handle multiple formats and errors are to be expected until it finds a format which works. Some formats will throw an error if not caught while others fail silently, so if no format works which error would it return? For a single format where you you need specific error handling it is easy to add your own drag and drop

  map.getViewport().addEventListener('dragover', function(event) {
    event.preventDefault();
  });

  map.getViewport().addEventListener('drop', function(event) {
    event.preventDefault();
    const files = event.dataTransfer.files;
    for (let i = 0, ii = files.length; i < ii; ++i) {
      const reader = new FileReader();
      reader.onload = function(e) {
        const features = new ol.format.GPX().readFeatures(reader.result);
        ...
      };
      reader.readAsText(files.item(i));
    }
  });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.