mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 01:53:39 +08:00
DEV: Allow HTML errors whenever a popup is generated (#20989)
Follow-up-to: 6bbf832400
This commit is contained in:
parent
5c2c1bf9a7
commit
355b44472b
|
@ -246,14 +246,7 @@ export default class AdminUserIndexController extends Controller.extend(
|
|||
queryParams: { nonce },
|
||||
});
|
||||
} else {
|
||||
const htmlMessage = error.jqXHR?.responseJSON.html_message;
|
||||
if (htmlMessage) {
|
||||
this.dialog.alert({
|
||||
message: htmlSafe(error.jqXHR?.responseJSON.error),
|
||||
});
|
||||
} else {
|
||||
popupAjaxError(error);
|
||||
}
|
||||
popupAjaxError(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import I18n from "I18n";
|
||||
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) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error.stack);
|
||||
|
@ -16,7 +17,9 @@ export function extractError(error, defaultMessage) {
|
|||
error = error.jqXHR;
|
||||
}
|
||||
|
||||
let parsedError, parsedJSON;
|
||||
let html = false,
|
||||
parsedError,
|
||||
parsedJSON;
|
||||
|
||||
if (error.responseJSON) {
|
||||
parsedJSON = error.responseJSON;
|
||||
|
@ -33,6 +36,10 @@ export function extractError(error, defaultMessage) {
|
|||
}
|
||||
|
||||
if (parsedJSON) {
|
||||
if (parsedJSON.html_message) {
|
||||
html = true;
|
||||
}
|
||||
|
||||
if (parsedJSON.errors?.length > 1) {
|
||||
parsedError = I18n.t("multiple_errors", {
|
||||
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) {
|
||||
|
@ -71,5 +85,11 @@ export function throwAjaxError(undoCallback) {
|
|||
|
||||
export function popupAjaxError(error) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user