FIX: simplify display of multiple AJAX errors (#18763)

Our dialog service doesn't accept HTML by default and we shouldn't include HTML in the error message string. And given that the Ajax error handler is called in multiple contexts, it's tricky to properly support line breaks via either HTML or `\n` so we are opting for plain text in AJAX error messages.
This commit is contained in:
Penar Musaraj 2022-10-28 08:37:08 -04:00 committed by GitHub
parent e120c94236
commit 0297c79cbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 5 deletions

View File

@ -33,8 +33,14 @@ export function extractError(error, defaultMessage) {
} }
if (parsedJSON) { if (parsedJSON) {
if (parsedJSON.errors && parsedJSON.errors.length > 0) { if (parsedJSON.errors?.length > 1) {
parsedError = parsedJSON.errors.join("<br>"); parsedError = I18n.t("multiple_errors", {
errors: parsedJSON.errors.map((e, i) => `${i + 1}) ${e}`).join(" "),
});
} else if (parsedJSON.errors?.length > 0) {
parsedError = I18n.t("generic_error_with_reason", {
error: parsedJSON.errors[0],
});
} else if (parsedJSON.error) { } else if (parsedJSON.error) {
parsedError = parsedJSON.error; parsedError = parsedJSON.error;
} else if (parsedJSON.message) { } else if (parsedJSON.message) {

View File

@ -1,3 +1,4 @@
import I18n from "I18n";
import { import {
acceptance, acceptance,
count, count,
@ -166,7 +167,9 @@ acceptance("Category Edit", function (needs) {
assert.strictEqual( assert.strictEqual(
query(".dialog-body").textContent.trim(), query(".dialog-body").textContent.trim(),
"duplicate email" I18n.t("generic_error_with_reason", {
error: "duplicate email",
})
); );
await click(".dialog-footer .btn-primary"); await click(".dialog-footer .btn-primary");

View File

@ -363,8 +363,10 @@ acceptance(
assert.strictEqual( assert.strictEqual(
query(".dialog-body").innerText.trim(), query(".dialog-body").innerText.trim(),
"There was an issue with the SMTP credentials provided, check the username and password and try again.", I18n.t("generic_error_with_reason", {
"shows a dialogue with the error message from the server" error:
"There was an issue with the SMTP credentials provided, check the username and password and try again.",
})
); );
await click(".dialog-footer .btn-primary"); await click(".dialog-footer .btn-primary");
}); });

View File

@ -257,6 +257,7 @@ en:
delete: "Delete" delete: "Delete"
generic_error: "Sorry, an error has occurred." generic_error: "Sorry, an error has occurred."
generic_error_with_reason: "An error occurred: %{error}" generic_error_with_reason: "An error occurred: %{error}"
multiple_errors: "Multiple errors occurred: %{errors}"
sign_up: "Sign Up" sign_up: "Sign Up"
log_in: "Log In" log_in: "Log In"
age: "Age" age: "Age"