This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/app.js
David Taylor 1f1aa6a0d8
FEATURE: Catch decorateCooked errors from themes/plugins (#15450)
If a theme/plugin raises an error while decorating post content, the decorator will be skipped, and the error reported on the console. Additionally, administrators will be shown a red warning at the top of the screen.

This commit refactors and re-uses some of the logic from the theme-initializer-error-reporting logic. In future, new error reports can be added by doing something like:

```
document.dispatchEvent(
  new CustomEvent("discourse-error", {
    detail: { messageKey: "some.translation.key", error },
  })
);
```
2022-01-04 21:59:52 +00:00

113 lines
2.8 KiB
JavaScript

import Application from "@ember/application";
import { buildResolver } from "discourse-common/resolver";
import { isTesting } from "discourse-common/config/environment";
const _pluginCallbacks = [];
let _unhandledThemeErrors = [];
const Discourse = Application.extend({
rootElement: "#main",
customEvents: {
paste: "paste",
},
Resolver: buildResolver("discourse"),
_prepareInitializer(moduleName) {
const themeId = moduleThemeId(moduleName);
let module = null;
try {
module = requirejs(moduleName, null, null, true);
if (!module) {
throw new Error(moduleName + " must export an initializer.");
}
} catch (error) {
if (!themeId || isTesting()) {
throw error;
}
fireThemeErrorEvent({ themeId, error });
return;
}
const init = module.default;
const oldInitialize = init.initialize;
init.initialize = (app) => {
try {
return oldInitialize.call(init, app.__container__, app);
} catch (error) {
if (!themeId || isTesting()) {
throw error;
}
fireThemeErrorEvent({ themeId, error });
}
};
return init;
},
// Start up the Discourse application by running all the initializers we've defined.
start() {
document.querySelector("noscript")?.remove();
Object.keys(requirejs._eak_seen).forEach((key) => {
if (/\/pre\-initializers\//.test(key)) {
const initializer = this._prepareInitializer(key);
if (initializer) {
this.initializer(initializer);
}
} else if (/\/(api\-)?initializers\//.test(key)) {
const initializer = this._prepareInitializer(key);
if (initializer) {
this.instanceInitializer(initializer);
}
}
});
// Plugins that are registered via `<script>` tags.
const withPluginApi = requirejs("discourse/lib/plugin-api").withPluginApi;
let initCount = 0;
_pluginCallbacks.forEach((cb) => {
this.instanceInitializer({
name: `_discourse_plugin_${++initCount}`,
after: "inject-objects",
initialize: () => withPluginApi(cb.version, cb.code),
});
});
},
_registerPluginCode(version, code) {
_pluginCallbacks.push({ version, code });
},
});
function moduleThemeId(moduleName) {
const match = moduleName.match(/^discourse\/theme\-(\d+)\//);
if (match) {
return parseInt(match[1], 10);
}
}
function fireThemeErrorEvent({ themeId, error }) {
const event = new CustomEvent("discourse-error", {
cancelable: true,
detail: { themeId, error },
});
const unhandled = document.dispatchEvent(event);
if (unhandled) {
_unhandledThemeErrors.push(event);
}
}
export function getAndClearUnhandledThemeErrors() {
const copy = _unhandledThemeErrors;
_unhandledThemeErrors = [];
return copy;
}
export default Discourse;