From 5194c33f84db30225f0d07a69981ab51a3680edd Mon Sep 17 00:00:00 2001 From: annehaley Date: Thu, 11 Jun 2026 14:20:41 +0000 Subject: [PATCH] Convert instances of `then` to `async` functions with consistent error handling --- isic/core/templates/core/base.html | 23 ++-- .../templates/core/collection_detail.html | 85 ++++++++------- .../core/partials/edit_collection_js.html | 28 ++--- .../core/partials/image_browser_js.html | 26 +++-- .../templates/ingest/metadata_apply.html | 28 +++-- .../ingest/metadata_file_detail.html | 29 +++-- isic/stats/templates/stats/stats.html | 100 ++++++++++-------- .../templates/studies/study_task_detail.html | 29 +++-- 8 files changed, 180 insertions(+), 168 deletions(-) diff --git a/isic/core/templates/core/base.html b/isic/core/templates/core/base.html index eac196bea..c57b50347 100644 --- a/isic/core/templates/core/base.html +++ b/isic/core/templates/core/base.html @@ -81,21 +81,20 @@ diff --git a/isic/core/templates/core/collection_detail.html b/isic/core/templates/core/collection_detail.html index 1681632e4..102b35e34 100644 --- a/isic/core/templates/core/collection_detail.html +++ b/isic/core/templates/core/collection_detail.html @@ -196,7 +196,7 @@ function collectionDetail() { return { errorMessage: '', - shareCollectionWithUsers() { + async shareCollectionWithUsers() { let _this = this; // eek if ($('#user-selection').val().length === 0) { @@ -205,52 +205,51 @@ } if (confirm('Are you sure you want to grant additional access to this collection?')) { - axios.post('{% url "api:collection_share_to_users" collection.pk %}', { - user_ids: $('#user-selection').val().map(function(n) { return parseInt(n) }), - notify: document.getElementById('notify-users').checked - }, { + try { + const resp = await axios.post('{% url "api:collection_share_to_users" collection.pk %}', { + user_ids: $('#user-selection').val().map(function(n) { return parseInt(n) }), + notify: document.getElementById('notify-users').checked + }, { + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': '{{ csrf_token }}' + } + }); + window.location.reload(); + } catch (error) { + _this.errorMessage = error.response.data[0]; + alert('Something went wrong, try again.'); + throw error; + } + } + }, + async setPinned(pinned) { + try { + const resp = await axios.post('/api/v2/collections/{{ collection.pk }}/set-pinned/', { pinned }, { headers: { 'Content-Type': 'application/json', 'X-CSRFToken': '{{ csrf_token }}' } - }).then((resp) => { - window.location.reload(); - }).catch(function(error) { - _this.errorMessage = error.response.data[0]; }); - } - }, - setPinned(pinned) { - axios.post('/api/v2/collections/{{ collection.pk }}/set-pinned/', { pinned }, { - headers: { - 'Content-Type': 'application/json', - 'X-CSRFToken': '{{ csrf_token }}' - } - }).then(() => { window.location.reload(); - }).catch(function(error) { - if (error.response && error.response.data && error.response.data.error) { - alert(error.response.data.error); - } else { - alert('Something went wrong.'); - } - }); + } catch (error) { + alert('Something went wrong, try again.'); + throw error; + } }, - deleteCollection() { + async deleteCollection() { if (confirm('Are you sure you want to delete this collection? This action cannot be undone.')) { - axios.delete('/api/v2/collections/{{ collection.pk }}/', { - headers: { - 'X-CSRFToken': '{{ csrf_token }}' - } - }).then((resp) => { + try { + const resp = await axios.delete('/api/v2/collections/{{ collection.pk }}/', { + headers: { + 'X-CSRFToken': '{{ csrf_token }}' + } + }); window.location.href = '{% url "core/collection-list" %}'; - }).catch(function(error) { - if (error.response && error.response.data && error.response.data.error) { - alert(error.response.data.error); - } else { - alert('Something went wrong.'); - } - }); + } catch (error) { + alert('Something went wrong, try again.'); + throw error; + } } }, }; @@ -261,14 +260,18 @@ attributions: [], fetched: false, loading: false, - fetchMetaInformation() { + async fetchMetaInformation() { const _this = this; this.loading = true; - axios.get('{% url "api:collection_attribution_information" collection.pk %}').then((resp) => { + try { + const resp = await axios.get('{% url "api:collection_attribution_information" collection.pk %}'); _this.attributions = resp.data; _this.loading = false; _this.fetched = true; - }); + } catch (error) { + alert('Something went wrong, try again.'); + throw error; + } } } } diff --git a/isic/core/templates/core/partials/edit_collection_js.html b/isic/core/templates/core/partials/edit_collection_js.html index beb6f908d..d8d3bbe99 100644 --- a/isic/core/templates/core/partials/edit_collection_js.html +++ b/isic/core/templates/core/partials/edit_collection_js.html @@ -29,20 +29,24 @@ removeImage(imageId) { this._persist(); }, - deleteImages() { + async deleteImages() { let _this = this; // eek if (confirm(`Are you sure you want to remove ${this.images.size} images from this collection?`)) { - axios.post(`/api/v2/collections/${this.collectionId}/remove-from-list/`, {'isic_ids': this._imagesToArray()}, - { headers: { - 'Content-Type': 'application/json', - 'X-CSRFToken': '{{ csrf_token }}' - } - }).then((resp) => { - this.resetImages(`/collections/${this.collectionId}/`); - }).catch(function(error) { - console.error(error); - alert('Something went wrong.'); - }); + try { + const resp = await axios.post(`/api/v2/collections/${this.collectionId}/remove-from-list/`, + {'isic_ids': this._imagesToArray()}, + { + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': '{{ csrf_token }}' + } + } + ); + this.resetImages(`/collections/${this.collectionId}/`); + } catch (error) { + alert('Something went wrong, try again.'); + throw error; + } } }, resetImages(url) { diff --git a/isic/core/templates/core/partials/image_browser_js.html b/isic/core/templates/core/partials/image_browser_js.html index fdadbd4e9..1efe68c20 100644 --- a/isic/core/templates/core/partials/image_browser_js.html +++ b/isic/core/templates/core/partials/image_browser_js.html @@ -13,19 +13,25 @@ this.addSearchResultsToCollection(); }); }, - addSearchResultsToCollection() { + async addSearchResultsToCollection() { let _this = this; if (confirm('Are you sure you want to add {{ total_images|intcomma }} images to this collection?')) { - axios.post(`/api/v2/collections/${this.selectedCollection}/populate-from-search/`, '{{ search_body|escapejs }}', { - headers: { - 'Content-Type': 'application/json', - 'X-CSRFToken': '{{ csrf_token }}' - } - }).then((resp) => { + try { + const resp = await axios.post( + `/api/v2/collections/${this.selectedCollection}/populate-from-search/`, + '{{ search_body|escapejs }}', + { + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': '{{ csrf_token }}' + } + }, + ); window.location.href = `/collections/${this.selectedCollection}/`; - }).catch(function(error) { - _this.errorMessage = error.response.data[0]; - }); + } catch (error) { + alert('Something went wrong, try again.'); + throw error; + } } else { this.selectedCollection = ''; } diff --git a/isic/ingest/templates/ingest/metadata_apply.html b/isic/ingest/templates/ingest/metadata_apply.html index 2a2c26cbf..7d0350653 100644 --- a/isic/ingest/templates/ingest/metadata_apply.html +++ b/isic/ingest/templates/ingest/metadata_apply.html @@ -22,25 +22,23 @@ diff --git a/isic/ingest/templates/ingest/metadata_file_detail.html b/isic/ingest/templates/ingest/metadata_file_detail.html index 81b1eff66..d356badd2 100644 --- a/isic/ingest/templates/ingest/metadata_file_detail.html +++ b/isic/ingest/templates/ingest/metadata_file_detail.html @@ -21,29 +21,28 @@

{{ metadata_file.blob_name }}

return { applying: false, applied: false, - apply() { + async apply() { this.applying = true; - - fetch(`/api/v2/metadata-files/{{ metadata_file.id }}/update_metadata/`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'X-CSRFToken': '{{ csrf_token }}' - }, - body: JSON.stringify({}) - }).then((resp) => { + try { + const resp = await fetch(`/api/v2/metadata-files/{{ metadata_file.id }}/update_metadata/`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-CSRFToken': '{{ csrf_token }}' + }, + body: JSON.stringify({}) + }); if (!resp.ok) { throw new Error('Something went wrong.'); } - this.applied = true; this.applying = false; window.location.href = '{% url "ingest/cohort-detail" cohort.id %}'; - }).catch((err) => { + } catch (error) { this.applying = false; - console.error(err); - alert('Something went wrong.'); - }); + alert('Something went wrong, try again.'); + throw error; + } } } } diff --git a/isic/stats/templates/stats/stats.html b/isic/stats/templates/stats/stats.html index c2a4334ab..2585fb743 100644 --- a/isic/stats/templates/stats/stats.html +++ b/isic/stats/templates/stats/stats.html @@ -34,56 +34,62 @@ {{ 30_day_sessions_per_country|json_script:"country_data" }} {% endblock %} diff --git a/isic/studies/templates/studies/study_task_detail.html b/isic/studies/templates/studies/study_task_detail.html index 641e465bc..caa106062 100644 --- a/isic/studies/templates/studies/study_task_detail.html +++ b/isic/studies/templates/studies/study_task_detail.html @@ -20,22 +20,19 @@