chore: Indicate cross-origin request in generic error message (#3669)

* Indicate cross-origin request in generic error message
* Run javascript format
* Move text to beginning of error message
* Update framework/core/locale/core.yml

Co-authored-by: David Wheatley <hi@davwheat.dev>
This commit is contained in:
Clark Winkelmann 2022-11-20 13:02:41 +01:00 committed by GitHub
parent 5bb0593bad
commit f5c346f1c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -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) {

View File

@ -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.