From 18f05db13866f17e765f1d16f9469026ee610011 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Tue, 23 Nov 2021 01:37:50 +0100 Subject: [PATCH] Always show CSRF error message for Ajax --- app/assets/javascripts/editor/ajax.js | 5 +++-- app/assets/javascripts/exercises.js.erb | 9 ++++++--- app/views/request_for_comments/show.html.slim | 6 ++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/editor/ajax.js b/app/assets/javascripts/editor/ajax.js index 971f75d6..127944f6 100644 --- a/app/assets/javascripts/editor/ajax.js +++ b/app/assets/javascripts/editor/ajax.js @@ -7,11 +7,12 @@ CodeOceanEditorAJAX = { }, ajaxError: function(response) { - var message = ((response || {}).responseJSON || {}).message || ''; + const responseJSON = ((response || {}).responseJSON || {}); + const message = responseJSON.message || responseJSON.error || ''; $.flash.danger({ text: message.length > 0 ? message : $('#flash').data('message-failure'), showPermanent: response.status === 422, }); } -}; \ No newline at end of file +}; diff --git a/app/assets/javascripts/exercises.js.erb b/app/assets/javascripts/exercises.js.erb index 5ffea543..e648d2fe 100644 --- a/app/assets/javascripts/exercises.js.erb +++ b/app/assets/javascripts/exercises.js.erb @@ -110,11 +110,14 @@ $(document).on('turbolinks:load', function () { } } - var ajaxError = function (error) { + var ajaxError = function (response) { + const responseJSON = ((response || {}).responseJSON || {}); + const message = responseJSON.message || responseJSON.error || ''; + $.flash.danger({ - text: $('#flash').data('message-failure') + text: message.length > 0 ? message : $('#flash').data('message-failure'), + showPermanent: response.status === 422, }); - Sentry.captureException(JSON.stringify(error)); }; var buildCheckboxes = function () { diff --git a/app/views/request_for_comments/show.html.slim b/app/views/request_for_comments/show.html.slim index 890d371c..b06cee5b 100644 --- a/app/views/request_for_comments/show.html.slim +++ b/app/views/request_for_comments/show.html.slim @@ -469,9 +469,11 @@ javascript: } function ajaxError(response) { - var message = ((response || {}).responseJSON || {}).message || ''; + const responseJSON = ((response || {}).responseJSON || {}); + const message = responseJSON.message || responseJSON.error || ''; $.flash.danger({ - text: message.length > 0 ? message : $('#flash').data('message-failure') + text: message.length > 0 ? message : $('#flash').data('message-failure'), + showPermanent: response.status === 422, }); }