From 9c168420389eeb6f0a7c408dbcb4f05f345b65ab Mon Sep 17 00:00:00 2001 From: "Simon Genin (ges)" Date: Wed, 3 Jan 2024 09:51:09 +0100 Subject: [PATCH] [FIX] website_cf_turnstile: better error message for turnstile errors Right now, when a turnstile error occurs, the user is displayed a CORS error, which is incorrect. This commit fixes that by displaying a more accurate error message. The fix requires the addition of a small trick which is to use the turnstile error handler system to catch and rethrow the error. It seems useless, but it is actually required as errors thrown by files coming from external domains will be set by the browser as "Script error" and stripped of any further info. So for better error handling, these few lines are worth it. The error is most likely to happen when duplicating a database as turnstile requires a specific domain name in its cloudflare settings. This improved message will hopefully help support manage better this situation. Hopefully, we can make something in the DB duplication tool to avoid the issue entirely and have this error message displayed only for "real" but rare errors. closes odoo/odoo#148016 X-original-commit: 112508dbc57bfbc3e8145bf4c8ed260e17071a91 Signed-off-by: Aaron Bohy (aab) Signed-off-by: Simon Genin (ges@odoo) --- addons/website_cf_turnstile/__manifest__.py | 1 + .../static/src/js/error_handler.js | 23 +++++++++++++++++++ .../static/src/js/turnstile.js | 10 ++++++++ 3 files changed, 34 insertions(+) create mode 100644 addons/website_cf_turnstile/static/src/js/error_handler.js diff --git a/addons/website_cf_turnstile/__manifest__.py b/addons/website_cf_turnstile/__manifest__.py index 1c56be34342..d7306fdc322 100644 --- a/addons/website_cf_turnstile/__manifest__.py +++ b/addons/website_cf_turnstile/__manifest__.py @@ -15,6 +15,7 @@ This module implements Cloudflare Turnstile so that you can prevent bot spam on 'assets': { 'web.assets_frontend': [ 'website_cf_turnstile/static/src/js/turnstile.js', + 'website_cf_turnstile/static/src/js/error_handler.js', ], }, 'license': 'LGPL-3', diff --git a/addons/website_cf_turnstile/static/src/js/error_handler.js b/addons/website_cf_turnstile/static/src/js/error_handler.js new file mode 100644 index 00000000000..ed42ebee8fa --- /dev/null +++ b/addons/website_cf_turnstile/static/src/js/error_handler.js @@ -0,0 +1,23 @@ +/** @odoo-module */ + +import { ErrorDialog } from "@web/core/errors/error_dialogs"; +import { _t } from "@web/core/l10n/translation"; +import { registry } from "@web/core/registry"; + +function turnstileErrorHandler(env, error) { + if (error.message.includes("Turnstile Error")) { + env.services.dialog.add(ErrorDialog, { + name: _t("Cloudflare Turnstile Error"), + traceback: _t( + `There was an error with Cloudflare Turnstile, the captcha system.\n` + + `Please make sure your credentials for this service are properly set up.\n` + + `The error code is: %s.\n` + + `You can find more information about this error code here: https://developers.cloudflare.com/turnstile/reference/errors.`, + error.event.error.code + ), + }); + return true; + } +} + +registry.category("error_handlers").add("turnstile_error_handler", turnstileErrorHandler); diff --git a/addons/website_cf_turnstile/static/src/js/turnstile.js b/addons/website_cf_turnstile/static/src/js/turnstile.js index 596d8d2a6c5..fc2c3d8770f 100644 --- a/addons/website_cf_turnstile/static/src/js/turnstile.js +++ b/addons/website_cf_turnstile/static/src/js/turnstile.js @@ -18,7 +18,17 @@ publicWidget.registry.s_website_form.include({ data-appearance="${mode}" data-response-field-name="turnstile_captcha" data-sitekey="${session.turnstile_site_key}" + data-error-callback="throwTurnstileError" > + `).insertAfter('.s_website_form_send, .o_website_form_send'); }