DEV: Move theme-error-handler initializer to service (#23534)

This will make it easier for other parts of the app to display similar error banners
This commit is contained in:
David Taylor 2023-09-12 14:07:03 +01:00 committed by GitHub
parent abcdd8d367
commit 3f86ee63b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 20 deletions

View File

@ -0,0 +1,5 @@
export default {
initialize(owner) {
owner.lookup("service:client-error-handler");
},
};

View File

@ -1,4 +1,3 @@
import { isTesting } from "discourse-common/config/environment";
import { getAndClearUnhandledThemeErrors } from "discourse/app"; import { getAndClearUnhandledThemeErrors } from "discourse/app";
import getURL from "discourse-common/lib/get-url"; import getURL from "discourse-common/lib/get-url";
import I18n from "I18n"; import I18n from "I18n";
@ -9,26 +8,34 @@ import identifySource, {
getThemeInfo, getThemeInfo,
} from "discourse/lib/source-identifier"; } from "discourse/lib/source-identifier";
import Ember from "ember"; import Ember from "ember";
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
import Service, { inject as service } from "@ember/service";
import { getOwner } from "@ember/application";
const showingErrors = new Set(); const showingErrors = new Set();
export default { @disableImplicitInjections
initialize(owner) { export default class ClientErrorHandlerService extends Service {
if (isTesting()) { @service currentUser;
return;
}
this.currentUser = owner.lookup("service:current-user"); constructor() {
super(...arguments);
getAndClearUnhandledThemeErrors().forEach((e) => this.reportThemeError(e)); getAndClearUnhandledThemeErrors().forEach((e) => this.reportThemeError(e));
document.addEventListener("discourse-error", this.handleDiscourseError); document.addEventListener("discourse-error", this.handleDiscourseError);
}, }
teardown() { get rootElement() {
return document.querySelector(getOwner(this).rootElement);
}
willDestroy() {
document.removeEventListener("discourse-error", this.handleDiscourseError); document.removeEventListener("discourse-error", this.handleDiscourseError);
delete this.currentUser; this.rootElement
}, .querySelectorAll(".broken-theme-alert-banner")
.forEach((e) => e.remove());
}
@bind @bind
handleDiscourseError(e) { handleDiscourseError(e) {
@ -39,7 +46,7 @@ export default {
} }
e.preventDefault(); // Mark as handled e.preventDefault(); // Mark as handled
}, }
reportThemeError(e) { reportThemeError(e) {
const { themeId, error } = e.detail; const { themeId, error } = e.detail;
@ -53,7 +60,7 @@ export default {
const message = I18n.t("themes.broken_theme_alert"); const message = I18n.t("themes.broken_theme_alert");
this.displayErrorNotice(message, source); this.displayErrorNotice(message, source);
}, }
reportGenericError(e) { reportGenericError(e) {
const { messageKey, error } = e.detail; const { messageKey, error } = e.detail;
@ -67,14 +74,14 @@ export default {
showingErrors.add(messageKey); showingErrors.add(messageKey);
this.displayErrorNotice(message, source); this.displayErrorNotice(message, source);
} }
}, }
displayErrorNotice(message, source) { displayErrorNotice(message, source) {
if (!this.currentUser?.admin) { if (!this.currentUser?.admin) {
return; return;
} }
let html = `⚠️ ${message}`; let html = `⚠️ ${escape(message)}`;
if (source && source.type === "theme") { if (source && source.type === "theme") {
html += `<br/>${I18n.t("themes.error_caused_by", { html += `<br/>${I18n.t("themes.error_caused_by", {
@ -88,11 +95,11 @@ export default {
)}</span>`; )}</span>`;
const alertDiv = document.createElement("div"); const alertDiv = document.createElement("div");
alertDiv.classList.add("broken-theme-alert"); alertDiv.classList.add("broken-theme-alert-banner");
alertDiv.innerHTML = html; alertDiv.innerHTML = html;
document.body.prepend(alertDiv); this.rootElement.prepend(alertDiv);
}, }
}; }
function reportToLogster(name, error) { function reportToLogster(name, error) {
const data = { const data = {

View File

@ -777,7 +777,7 @@ table {
} }
} }
.broken-theme-alert { .broken-theme-alert-banner {
font-size: var(--base-font-size); font-size: var(--base-font-size);
font-weight: bold; font-weight: bold;
padding: 5px 0; padding: 5px 0;