From 5f124d8ecf10f50c5b28e8a2e2faa35909698b82 Mon Sep 17 00:00:00 2001 From: PKulkoRaccoonGang Date: Thu, 4 Apr 2024 18:31:26 +0300 Subject: [PATCH] feat: added ajax error handler --- cms/templates/container_editor.html | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cms/templates/container_editor.html b/cms/templates/container_editor.html index b8f2403ad16c..7864b63cce8b 100644 --- a/cms/templates/container_editor.html +++ b/cms/templates/container_editor.html @@ -175,6 +175,10 @@ // Serves to initialize the rendering of a xblock edit modal window. setTimeout(() => $('.button-edit').trigger('click'), 300); + const iframeMessageHandler = (method, msg) => { + return window.parent.postMessage({ method, msg }, '*'); + } + /** * Callback function for the MutationObserver to handle mutations * and send information when the modal window close logic is triggered. @@ -195,7 +199,7 @@ // If removed nodes match the current modal, post a message to close the edit modal. if (editModalRemovedNodes.length > 0) { - window.parent.postMessage({ method: 'close_edit_modal', msg: 'Message from edit iframe modal' }, '*'); + iframeMessageHandler('close_edit_modal', 'Sends a message when the modal window is closed'); } }) @@ -205,6 +209,20 @@ attributes: true, attributeFilter: ['class'] }); + + const originalAjax = $.ajax; + // Notifies the parent element that a server error has occurred. + $.ajax = function(options) { + const errorHandler = options.error; + options.error = function(xhr, textStatus, errorThrown) { + iframeMessageHandler('edit_modal-error', 'Sends a message in case of server error'); + + if (errorHandler) { + errorHandler(xhr, textStatus, errorThrown); + } + }; + return originalAjax.call(this, options); + }; });