From 6e9b0c4d2f4c34f280cb3f90d26a40b8bf663b09 Mon Sep 17 00:00:00 2001 From: Chris Lawton <chris.lawton@torchbox.com> Date: Wed, 15 May 2019 09:52:14 +0100 Subject: [PATCH] handle other fetch errors --- .../src/javascript/apply/edit-comment.js | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/opentech/static_src/src/javascript/apply/edit-comment.js b/opentech/static_src/src/javascript/apply/edit-comment.js index ed5e8e63f..eef32d781 100644 --- a/opentech/static_src/src/javascript/apply/edit-comment.js +++ b/opentech/static_src/src/javascript/apply/edit-comment.js @@ -73,10 +73,12 @@ message: editedComment }) }).then(response => { - if (response.status === 404) { - $(this).closest(editBlock).append('<p class="wrapper--error js-comment-error">Update unsuccessful. This comment has been edited elsewhere. To get the latest updates please refresh the page, but note any unsaved changes will be lost by doing so.</p>'); - $(this).attr('disabled', true); - return; + if (!response.ok) { + const error = Object.assign({}, response, { + status: response.status, + statusText: response.statusText + }); + return Promise.reject(error); } return response.json(); }).then(data => { @@ -85,9 +87,21 @@ showComment(this); showEditButton(this); hidePageDownEditor(this); + }).catch((error) => { + if (error.status === 404) { + handleError(this, 'Update unsuccessful. This comment has been edited elsewhere. To get the latest updates please refresh the page, but note any unsaved changes will be lost by doing so.'); + } + else { + handleError(this, 'An error has occured. Please try again later.'); + } }); }); + const handleError = (el, message) => { + $(el).closest(editBlock).append(`<p class="wrapper--error js-comment-error">${message}</p>`); + $(el).attr('disabled', true); + }; + const initEditor = () => { const converterOne = window.Markdown.getSanitizingConverter(); const commentEditor = new window.Markdown.Editor(converterOne, '-edit-comment'); -- GitLab