From 0616827419ac32fbacd4bc686f9f34d575133380 Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 17 Sep 2024 11:57:21 +1000 Subject: [PATCH 1/2] Catch errors and alert the user I'm not sure why, but Turbo was swallowing the unauthorized error, so I thought we should alert the user to help with debugging. Same with network error, it gave no feedback before. I think this is testable in theory, but I haven't invested the time on it. --- app/webpacker/js/turbo.js | 17 +++++++++++++++-- config/locales/en.yml | 4 ++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/webpacker/js/turbo.js b/app/webpacker/js/turbo.js index d8816b4d2a..cd6ed4b872 100644 --- a/app/webpacker/js/turbo.js +++ b/app/webpacker/js/turbo.js @@ -8,10 +8,23 @@ document.addEventListener("turbo:frame-missing", (event) => { event.preventDefault(); // show error message instead - status = event.detail.response.status; + showError(event.detail.response?.status); +}); + +document.addEventListener("turbo:submit-end", (event) => { + if (!event.detail.success){ + // show error message on failure + showError(event.detail.fetchResponse?.statusCode); + event.preventDefault(); + } +}); + +function showError(status) { if(status == 401) { alert(I18n.t("errors.unauthorized.message")); + } else if(status === undefined) { + alert(I18n.t("errors.network_error.message")); } else { alert(I18n.t("errors.general_error.message")); } -}); +} diff --git a/config/locales/en.yml b/config/locales/en.yml index e7e6cdaa27..0fd20f5667 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -188,6 +188,10 @@ en: We record all errors and may be working on a fix. If the problem persists or is urgent, please contact us." + unauthorized: + message: "You are not authorised to perform that action." + network_error: + message: "Network error: please try again later." stripe: error_code: incorrect_number: "The card number is incorrect." From a3a79686db5beabbc0ba4dcabd630413c7846979 Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 17 Dec 2024 11:16:56 +1100 Subject: [PATCH 2/2] Don't catch other 4xx errors --- app/webpacker/js/turbo.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/webpacker/js/turbo.js b/app/webpacker/js/turbo.js index cd6ed4b872..4b330cdada 100644 --- a/app/webpacker/js/turbo.js +++ b/app/webpacker/js/turbo.js @@ -20,11 +20,12 @@ document.addEventListener("turbo:submit-end", (event) => { }); function showError(status) { + // Note that other 4xx errors will be handled differently. if(status == 401) { alert(I18n.t("errors.unauthorized.message")); } else if(status === undefined) { alert(I18n.t("errors.network_error.message")); - } else { + } else if (status >= 500) { alert(I18n.t("errors.general_error.message")); } }