diff --git a/framework/core/js/src/common/Application.tsx b/framework/core/js/src/common/Application.tsx index 8d0140fde..243afa9bc 100644 --- a/framework/core/js/src/common/Application.tsx +++ b/framework/core/js/src/common/Application.tsx @@ -553,7 +553,11 @@ export default class Application { break; default: - content = app.translator.trans('core.lib.error.generic_message'); + if (this.requestWasCrossOrigin(error)) { + content = app.translator.trans('core.lib.error.generic_cross_origin_message'); + } else { + content = app.translator.trans('core.lib.error.generic_message'); + } } const isDebug: boolean = app.forum.attribute('debug'); @@ -577,6 +581,18 @@ export default class Application { return Promise.reject(error); } + /** + * Used to modify the error message shown on the page to help troubleshooting. + * While not certain, a failing cross-origin request likely indicates a missing redirect to Flarum canonical URL. + * Because XHR errors do not expose CORS information, we can only compare the requested URL origin to the page origin. + * + * @param error + * @protected + */ + protected requestWasCrossOrigin(error: RequestError): boolean { + return new URL(error.options.url, document.baseURI).origin !== window.location.origin; + } + protected requestErrorDefaultHandler(e: unknown, isDebug: boolean, formattedErrors: string[]): void { if (e instanceof RequestError) { if (isDebug && e.xhr) { diff --git a/framework/core/locale/core.yml b/framework/core/locale/core.yml index e21745c5a..0d9167f7d 100644 --- a/framework/core/locale/core.yml +++ b/framework/core/locale/core.yml @@ -555,6 +555,7 @@ core: dependent_extensions_message: "Cannot disable {extension} until the following dependent extensions are disabled: {extensions}" extension_initialiation_failed_message: "{extension} failed to initialize, check the browser console for further information." generic_message: "Oops! Something went wrong. Please reload the page and try again." + generic_cross_origin_message: "Oops! Something went wrong during a cross-origin request. Please reload the page and try again." missing_dependencies_message: "Cannot enable {extension} until the following dependencies are enabled: {extensions}" not_found_message: The requested resource was not found. payload_too_large_message: The request payload was too large.