From da7d09527be972fe517f501123d1919c897ec352 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Fri, 26 Jan 2024 13:16:12 +0100 Subject: [PATCH] chore: catch error when using Request, and make remote URL working again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I decided to remove the check `is_ajax` from `validate_url` to simplify and edge case, and because I think it was more or less useless. Basically, when getting remote data, we have two cases: - direct call to the remote URL - proxy through our `ajax_proxy` system (to work around CORS limitations) In the first case, we cannot set the `X-Requested-With` header, otherwise preflight step will fail, and in the second case, until now, we needed to set this header for this `is_ajax` check to pass. So keeping this check would mean adapting the behaviour of the Request/ServerRequest class in a non elegant way. So let's make it simple… --- umap/static/umap/js/umap.layer.js | 21 +++++++++++++-------- umap/views.py | 1 - 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/umap/static/umap/js/umap.layer.js b/umap/static/umap/js/umap.layer.js index 3c6877d9..9dd8d8c4 100644 --- a/umap/static/umap/js/umap.layer.js +++ b/umap/static/umap/js/umap.layer.js @@ -747,15 +747,18 @@ L.U.DataLayer = L.Evented.extend({ if (!this.options.remoteData.dynamic && this.hasDataLoaded() && !force) return if (!this.isVisible()) return let url = this.map.localizeUrl(this.options.remoteData.url) - if (this.options.remoteData.proxy) + if (this.options.remoteData.proxy) { url = this.map.proxyUrl(url, this.options.remoteData.ttl) + } const response = await this.map.request.get(url) - this.clear() - this.rawToGeoJSON( - await response.text(), - this.options.remoteData.format, - (geojson) => this.fromGeoJSON(geojson) - ) + if (response && response.ok) { + this.clear() + this.rawToGeoJSON( + await response.text(), + this.options.remoteData.format, + (geojson) => this.fromGeoJSON(geojson) + ) + } }, onceLoaded: function (callback, context) { @@ -1060,7 +1063,9 @@ L.U.DataLayer = L.Evented.extend({ importFromUrl: async function (uri, type) { uri = this.map.localizeUrl(uri) const response = await this.map.request.get(uri) - this.importRaw(await response.text(), type) + if (response && response.ok) { + this.importRaw(await response.text(), type) + } }, getColor: function () { diff --git a/umap/views.py b/umap/views.py index c6a4cede..f4484269 100644 --- a/umap/views.py +++ b/umap/views.py @@ -335,7 +335,6 @@ showcase = MapsShowCase.as_view() def validate_url(request): assert request.method == "GET" - assert is_ajax(request) url = request.GET.get("url") assert url try: