DEV: Allow HTML errors whenever a popup is generated (#20989)

Follow-up-to: 6bbf832400
This commit is contained in:
Daniel Waterworth 2023-04-06 10:00:54 -05:00 committed by GitHub
parent 5c2c1bf9a7
commit 355b44472b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 12 deletions

View File

@ -245,16 +245,9 @@ export default class AdminUserIndexController extends Controller.extend(
this.router.transitionTo("second-factor-auth", { this.router.transitionTo("second-factor-auth", {
queryParams: { nonce }, queryParams: { nonce },
}); });
} else {
const htmlMessage = error.jqXHR?.responseJSON.html_message;
if (htmlMessage) {
this.dialog.alert({
message: htmlSafe(error.jqXHR?.responseJSON.error),
});
} else { } else {
popupAjaxError(error); popupAjaxError(error);
} }
}
}); });
} }

View File

@ -1,7 +1,8 @@
import I18n from "I18n"; import I18n from "I18n";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import { htmlSafe } from "@ember/template";
export function extractError(error, defaultMessage) { function extractErrorInfo(error, defaultMessage) {
if (error instanceof Error) { if (error instanceof Error) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(error.stack); console.error(error.stack);
@ -16,7 +17,9 @@ export function extractError(error, defaultMessage) {
error = error.jqXHR; error = error.jqXHR;
} }
let parsedError, parsedJSON; let html = false,
parsedError,
parsedJSON;
if (error.responseJSON) { if (error.responseJSON) {
parsedJSON = error.responseJSON; parsedJSON = error.responseJSON;
@ -33,6 +36,10 @@ export function extractError(error, defaultMessage) {
} }
if (parsedJSON) { if (parsedJSON) {
if (parsedJSON.html_message) {
html = true;
}
if (parsedJSON.errors?.length > 1) { if (parsedJSON.errors?.length > 1) {
parsedError = I18n.t("multiple_errors", { parsedError = I18n.t("multiple_errors", {
errors: parsedJSON.errors.map((e, i) => `${i + 1}) ${e}`).join(" "), errors: parsedJSON.errors.map((e, i) => `${i + 1}) ${e}`).join(" "),
@ -56,7 +63,14 @@ export function extractError(error, defaultMessage) {
} }
} }
return parsedError || defaultMessage || I18n.t("generic_error"); return {
html,
message: parsedError || defaultMessage || I18n.t("generic_error"),
};
}
export function extractError(error, defaultMessage) {
return extractErrorInfo(error, defaultMessage).message;
} }
export function throwAjaxError(undoCallback) { export function throwAjaxError(undoCallback) {
@ -71,5 +85,11 @@ export function throwAjaxError(undoCallback) {
export function popupAjaxError(error) { export function popupAjaxError(error) {
const dialog = getOwner(this).lookup("service:dialog"); const dialog = getOwner(this).lookup("service:dialog");
dialog.alert(extractError(error)); const errorInfo = extractErrorInfo(error);
if (errorInfo.html) {
dialog.alert({ message: htmlSafe(errorInfo.message) });
} else {
dialog.alert(errorInfo.message);
}
} }