DEV: Make attachWidgetAction fail gracefully when widget is missing (#21750)

This brings the behaviour in line with our other widget-related APIs like `decorateWidget` and `reopenWidget`. This commit also adds a theme/plugin prefix to the console messages.
This commit is contained in:
David Taylor 2023-05-25 15:21:58 +01:00 committed by GitHub
parent 036e27e69a
commit d52198356c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -573,7 +573,17 @@ class PluginApi {
attachWidgetAction(widget, actionName, fn) {
const widgetClass =
queryRegistry(widget) ||
this.container.factoryFor(`widget:${widget}`).class;
this.container.factoryFor(`widget:${widget}`)?.class;
if (!widgetClass) {
// eslint-disable-next-line no-console
console.error(
consolePrefix(),
`attachWidgetAction: Could not find widget ${widget} in registry`
);
return;
}
widgetClass.prototype[actionName] = fn;
}

View File

@ -23,6 +23,7 @@ import { deepMerge } from "discourse-common/lib/object";
import { get } from "@ember/object";
import { h } from "virtual-dom";
import { isProduction } from "discourse-common/config/environment";
import { consolePrefix } from "discourse/lib/source-identifier";
const _registry = {};
@ -106,7 +107,10 @@ export function reopenWidget(name, opts) {
let existing = _registry[name];
if (!existing) {
// eslint-disable-next-line no-console
console.error(`Could not find widget ${name} in registry`);
console.error(
consolePrefix(),
`reopenWidget: Could not find widget ${name} in registry`
);
return;
}