DEV: Clean up pre-initializers (#18680)

This commit is contained in:
Jarek Radosz 2022-10-26 03:19:54 +02:00 committed by GitHub
parent 305b7c8fae
commit f822a933fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 79 deletions

View File

@ -2,10 +2,11 @@ import { loadSprites } from "discourse/lib/svg-sprite-loader";
export default { export default {
name: "svg-sprite-fontawesome", name: "svg-sprite-fontawesome",
after: "discourse-bootstrap", after: "export-application-global",
initialize(container) { initialize(container) {
let session = container.lookup("service:session"); const session = container.lookup("service:session");
if (session.svgSpritePath) { if (session.svgSpritePath) {
loadSprites(session.svgSpritePath, "fontawesome"); loadSprites(session.svgSpritePath, "fontawesome");
} }

View File

@ -14,7 +14,7 @@ const showingErrors = new Set();
export default { export default {
name: "theme-errors-handler", name: "theme-errors-handler",
after: "inject-discourse-objects", after: "export-application-global",
initialize(container) { initialize(container) {
if (isTesting()) { if (isTesting()) {
@ -23,9 +23,7 @@ export default {
this.currentUser = container.lookup("service:current-user"); this.currentUser = container.lookup("service:current-user");
getAndClearUnhandledThemeErrors().forEach((e) => { getAndClearUnhandledThemeErrors().forEach((e) => this.reportThemeError(e));
reportThemeError(this.currentUser, e);
});
document.addEventListener("discourse-error", this.handleDiscourseError); document.addEventListener("discourse-error", this.handleDiscourseError);
}, },
@ -38,12 +36,65 @@ export default {
@bind @bind
handleDiscourseError(e) { handleDiscourseError(e) {
if (e.detail?.themeId) { if (e.detail?.themeId) {
reportThemeError(this.currentUser, e); this.reportThemeError(e);
} else { } else {
reportGenericError(this.currentUser, e); this.reportGenericError(e);
} }
e.preventDefault(); // Mark as handled e.preventDefault(); // Mark as handled
}, },
reportThemeError(e) {
const { themeId, error } = e.detail;
const source = {
type: "theme",
...getThemeInfo(themeId),
};
reportToConsole(error, source);
reportToLogster(source.name, error);
const message = I18n.t("themes.broken_theme_alert");
this.displayErrorNotice(this.currentUser, message, source);
},
reportGenericError(e) {
const { messageKey, error } = e.detail;
const message = I18n.t(messageKey);
const source = identifySource(error);
reportToConsole(error, source);
if (messageKey && !showingErrors.has(messageKey)) {
showingErrors.add(messageKey);
this.displayErrorNotice(message, source);
}
},
displayErrorNotice(message, source) {
if (!this.currentUser?.admin) {
return;
}
let html = `⚠️ ${message}`;
if (source && source.type === "theme") {
html += `<br/>${I18n.t("themes.error_caused_by", {
name: escape(source.name),
path: source.path,
})}`;
}
html += `<br/><span class='theme-error-suffix'>${I18n.t(
"themes.only_admins"
)}</span>`;
const alertDiv = document.createElement("div");
alertDiv.classList.add("broken-theme-alert");
alertDiv.innerHTML = html;
document.body.prepend(alertDiv);
},
}; };
function reportToLogster(name, error) { function reportToLogster(name, error) {
@ -59,36 +110,6 @@ function reportToLogster(name, error) {
}); });
} }
function reportThemeError(currentUser, e) {
const { themeId, error } = e.detail;
const source = {
type: "theme",
...getThemeInfo(themeId),
};
reportToConsole(error, source);
reportToLogster(source.name, error);
const message = I18n.t("themes.broken_theme_alert");
displayErrorNotice(currentUser, message, source);
}
function reportGenericError(currentUser, e) {
const { messageKey, error } = e.detail;
let message = I18n.t(messageKey);
const source = identifySource(error);
reportToConsole(error, source);
if (messageKey && !showingErrors.has(messageKey)) {
showingErrors.add(messageKey);
displayErrorNotice(currentUser, message, source);
}
}
function reportToConsole(error, source) { function reportToConsole(error, source) {
const prefix = consolePrefix(error, source); const prefix = consolePrefix(error, source);
if (prefix) { if (prefix) {
@ -99,27 +120,3 @@ function reportToConsole(error, source) {
console.error(error); console.error(error);
} }
} }
function displayErrorNotice(currentUser, message, source) {
if (!currentUser?.admin) {
return;
}
let html = `⚠️ ${message}`;
if (source && source.type === "theme") {
html += `<br/>${I18n.t("themes.error_caused_by", {
name: escape(source.name),
path: source.path,
})}`;
}
html += `<br/><span class='theme-error-suffix'>${I18n.t(
"themes.only_admins"
)}</span>`;
const alertDiv = document.createElement("div");
alertDiv.classList.add("broken-theme-alert");
alertDiv.innerHTML = html;
document.body.prepend(alertDiv);
}

View File

@ -141,14 +141,3 @@ export function mapRoutes() {
this.route("unknown", { path: "*path" }); this.route("unknown", { path: "*path" });
}); });
} }
export function teardownRouter(routerClass) {
routerClass.dslCallbacks.splice(0, routerClass.dslCallbacks.length);
}
export function registerRouter(registry) {
registry.unregister("router:main");
let router = mapRoutes();
registry.register("router:main", router);
return router;
}

View File

@ -1,16 +1,15 @@
import { registerRouter, teardownRouter } from "discourse/mapping-router"; import { mapRoutes } from "discourse/mapping-router";
export default { export default {
name: "map-routes", name: "map-routes",
after: "inject-discourse-objects", after: "inject-discourse-objects",
initialize(container, app) { initialize(_, app) {
let routerClass = registerRouter(app); this.routerClass = mapRoutes();
container.registry.register("router:main", routerClass); app.register("router:main", this.routerClass);
this.routerClass = routerClass;
}, },
teardown() { teardown() {
teardownRouter(this.routerClass); this.routerClass.dslCallbacks.length = 0;
}, },
}; };