A platform for community discussion. Free, open, simple.
DEV: Rely on helpers for ajax error handling to reduce repetition (#20988)
Daniel Waterworth0ff86feb966d
diff --git a/app/assets/javascripts/discourse/app/controllers/activation-edit.js b/app/assets/javascripts/discourse/app/controllers/activation-edit.jsindex ee57b1b1fd9..a5e5e4562e1 100644--- a/app/assets/javascripts/discourse/app/controllers/activation-edit.js+++ b/app/assets/javascripts/discourse/app/controllers/activation-edit.js@@ -2,7 +2,7 @@ import Controller, { inject as controller } from "@ember/controller";import ModalFunctionality from "discourse/mixins/modal-functionality";import { changeEmail } from "discourse/lib/user-activation";import discourseComputed from "discourse-common/utils/decorators";-import { extractError } from "discourse/lib/ajax-error";+import { flashAjaxError } from "discourse/lib/ajax-error";export default Controller.extend(ModalFunctionality, {login: controller(),@@ -31,7 +31,7 @@ export default Controller.extend(ModalFunctionality, {});modal.set("currentEmail", this.newEmail);})- .catch((err) => this.flash(extractError(err), "error"));+ .catch(flashAjaxError(this));},},});diff --git a/app/assets/javascripts/discourse/app/controllers/do-not-disturb.js b/app/assets/javascripts/discourse/app/controllers/do-not-disturb.jsindex cafa0483ef3..d5c9243d830 100644--- a/app/assets/javascripts/discourse/app/controllers/do-not-disturb.js+++ b/app/assets/javascripts/discourse/app/controllers/do-not-disturb.js@@ -1,7 +1,7 @@import Controller from "@ember/controller";import ModalFunctionality from "discourse/mixins/modal-functionality";import { action } from "@ember/object";-import { extractError } from "discourse/lib/ajax-error";+import { flashAjaxError } from "discourse/lib/ajax-error";export default Controller.extend(ModalFunctionality, {duration: null,@@ -18,9 +18,7 @@ export default Controller.extend(ModalFunctionality, {.then(() => {this.send("closeModal");})- .catch((e) => {- this.flash(extractError(e), "error");- });+ .catch(flashAjaxError(this));},@actiondiff --git a/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js b/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.jsindex 154fd5e94e6..e1cd03e70c0 100644--- a/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js+++ b/app/assets/javascripts/discourse/app/controllers/edit-category-tabs.js@@ -6,7 +6,7 @@ import DiscourseURL from "discourse/lib/url";import I18n from "I18n";import { NotificationLevels } from "discourse/lib/notification-levels";import PermissionType from "discourse/models/permission-type";-import { extractError } from "discourse/lib/ajax-error";+import { popupAjaxError } from "discourse/lib/ajax-error";import { underscore } from "@ember/string";import { inject as service } from "@ember/service";@@ -113,7 +113,7 @@ export default Controller.extend({}}).catch((error) => {- this.dialog.alert(extractError(error));+ popupAjaxError(error);this.set("saving", false);});},diff --git a/app/assets/javascripts/discourse/app/controllers/forgot-password.js b/app/assets/javascripts/discourse/app/controllers/forgot-password.jsindex 7733fed4ec0..6ca580d7f55 100644--- a/app/assets/javascripts/discourse/app/controllers/forgot-password.js+++ b/app/assets/javascripts/discourse/app/controllers/forgot-password.js@@ -5,7 +5,7 @@ import { ajax } from "discourse/lib/ajax";import cookie from "discourse/lib/cookie";import discourseComputed from "discourse-common/utils/decorators";import { escapeExpression } from "discourse/lib/utilities";-import { extractError } from "discourse/lib/ajax-error";+import { flashAjaxError } from "discourse/lib/ajax-error";import getURL from "discourse-common/lib/get-url";import { isEmpty } from "@ember/utils";@@ -90,9 +90,7 @@ export default Controller.extend(ModalFunctionality, {this.set("helpSeen", !data.user_found);}})- .catch((e) => {- this.flash(extractError(e), "error");- })+ .catch(flashAjaxError(this)).finally(() => {this.set("disabled", false);});diff --git a/app/assets/javascripts/discourse/app/controllers/grant-badge.js b/app/assets/javascripts/discourse/app/controllers/grant-badge.jsindex 0b09e326616..8c6d8e3afcb 100644--- a/app/assets/javascripts/discourse/app/controllers/grant-badge.js+++ b/app/assets/javascripts/discourse/app/controllers/grant-badge.js@@ -6,7 +6,7 @@ import ModalFunctionality from "discourse/mixins/modal-functionality";import UserBadge from "discourse/models/user-badge";import { all } from "rsvp";import discourseComputed from "discourse-common/utils/decorators";-import { extractError } from "discourse/lib/ajax-error";+import { flashAjaxError } from "discourse/lib/ajax-error";export default Controller.extend(ModalFunctionality, GrantBadgeController, {topicController: controller("topic"),@@ -64,21 +64,16 @@ export default Controller.extend(ModalFunctionality, GrantBadgeController, {this.get("post.username"),this.badgeReason)- .then(- (newBadge) => {- this.set("selectedBadgeId", null);- this.flash(- I18n.t("badges.successfully_granted", {- username: this.get("post.username"),- badge: newBadge.get("badge.name"),- }),- "success"- );- },- (error) => {- this.flash(extractError(error), "error");- }- )+ .then((newBadge) => {+ this.set("selectedBadgeId", null);+ this.flash(+ I18n.t("badges.successfully_granted", {+ username: this.get("post.username"),+ badge: newBadge.get("badge.name"),+ }),+ "success"+ );+ }, flashAjaxError(this)).finally(() => this.set("saving", false));},},diff --git a/app/assets/javascripts/discourse/app/controllers/group-add-members.js b/app/assets/javascripts/discourse/app/controllers/group-add-members.jsindex b6bd5e51a4f..345072b2d57 100644--- a/app/assets/javascripts/discourse/app/controllers/group-add-members.js+++ b/app/assets/javascripts/discourse/app/controllers/group-add-members.js@@ -2,7 +2,7 @@ import Controller from "@ember/controller";import { action } from "@ember/object";import { isEmpty } from "@ember/utils";import discourseComputed from "discourse-common/utils/decorators";-import { extractError } from "discourse/lib/ajax-error";+import { flashAjaxError } from "discourse/lib/ajax-error";import { emailValid } from "discourse/lib/utilities";import ModalFunctionality from "discourse/mixins/modal-functionality";import I18n from "I18n";@@ -76,7 +76,7 @@ export default Controller.extend(ModalFunctionality, {this.send("closeModal");})- .catch((error) => this.flash(extractError(error), "error"))+ .catch(flashAjaxError(this)).finally(() => this.set("loading", false));},});diff --git a/app/assets/javascripts/discourse/app/controllers/login.js b/app/assets/javascripts/discourse/app/controllers/login.jsindex c4038cc086b..58d01b794ea 100644--- a/app/assets/javascripts/discourse/app/controllers/login.js+++ b/app/assets/javascripts/discourse/app/controllers/login.js@@ -10,7 +10,7 @@ import { SECOND_FACTOR_METHODS } from "discourse/models/user";import { ajax } from "discourse/lib/ajax";import discourseComputed from "discourse-common/utils/decorators";import { escape } from "pretty-text/sanitizer";-import { extractError } from "discourse/lib/ajax-error";+import { flashAjaxError } from "discourse/lib/ajax-error";import { findAll } from "discourse/models/login-method";import getURL from "discourse-common/lib/get-url";import { getWebauthnCredential } from "discourse/lib/webauthn";@@ -176,7 +176,7 @@ export default Controller.extend(ModalFunctionality, {);}})- .catch((e) => this.flash(extractError(e), "error"))+ .catch(flashAjaxError(this)).finally(() => this.set("processingEmailLink", false));},diff --git a/app/assets/javascripts/discourse/app/controllers/move-to-topic.js b/app/assets/javascripts/discourse/app/controllers/move-to-topic.jsindex c5f80bcab28..507a797b4c9 100644--- a/app/assets/javascripts/discourse/app/controllers/move-to-topic.js+++ b/app/assets/javascripts/discourse/app/controllers/move-to-topic.js@@ -5,7 +5,7 @@ import DiscourseURL from "discourse/lib/url";import I18n from "I18n";import ModalFunctionality from "discourse/mixins/modal-functionality";import discourseComputed from "discourse-common/utils/decorators";-import { extractError } from "discourse/lib/ajax-error";+import { flashAjaxError } from "discourse/lib/ajax-error";import { isEmpty } from "@ember/utils";import { next } from "@ember/runloop";@@ -168,9 +168,7 @@ export default Controller.extend(ModalFunctionality, {this.topicController.send("toggleMultiSelect");DiscourseURL.routeTo(result.url);})- .catch((xhr) => {- this.flash(extractError(xhr, I18n.t("topic.move_to.error")), "error");- })+ .catch(flashAjaxError(this, I18n.t("topic.move_to.error"))).finally(() => {this.set("saving", false);});diff --git a/app/assets/javascripts/discourse/app/lib/ajax-error.js b/app/assets/javascripts/discourse/app/lib/ajax-error.jsindex 9e15966e6dd..464afe9c78d 100644--- a/app/assets/javascripts/discourse/app/lib/ajax-error.js+++ b/app/assets/javascripts/discourse/app/lib/ajax-error.js@@ -73,13 +73,19 @@ export function extractError(error, defaultMessage) {return extractErrorInfo(error, defaultMessage).message;}-export function throwAjaxError(undoCallback) {+export function throwAjaxError(undoCallback, defaultMessage) {return function (error) {// If we provided an `undo` callbackif (undoCallback) {undoCallback(error);}- throw extractError(error);+ throw extractError(error, defaultMessage);+ };+}++export function flashAjaxError(modal, defaultMessage) {+ return (error) => {+ modal.flash(extractError(error, defaultMessage), "error");};}diff --git a/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-name-slug.js b/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-name-slug.jsindex be4d23e7279..35be0c97a41 100644--- a/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-name-slug.js+++ b/plugins/chat/assets/javascripts/discourse/controllers/chat-channel-edit-name-slug.js@@ -3,7 +3,7 @@ import discourseDebounce from "discourse-common/lib/debounce";import { ajax } from "discourse/lib/ajax";import { cancel } from "@ember/runloop";import { action, computed } from "@ember/object";-import { extractError } from "discourse/lib/ajax-error";+import { flashAjaxError } from "discourse/lib/ajax-error";import ModalFunctionality from "discourse/mixins/modal-functionality";import { inject as service } from "@ember/service";export default class ChatChannelEditTitleController extends Controller.extend(@@ -49,9 +49,7 @@ export default class ChatChannelEditTitleController extends Controller.extend(this.model.set("title", result.channel.title);this.send("closeModal");})- .catch((event) => {- this.flash(extractError(event), "error");- });+ .catch(flashAjaxError(this));}@action