DEV: Rely on helpers for ajax error handling to reduce repetition (#20988)

This commit is contained in:
Daniel Waterworth 2023-04-06 11:25:24 -05:00 committed by GitHub
parent bdaaac9c9f
commit 0ff86feb96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 35 additions and 42 deletions

View File

@ -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));
},
},
});

View File

@ -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));
},
@action

View File

@ -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);
});
},

View File

@ -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);
});

View File

@ -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,8 +64,7 @@ export default Controller.extend(ModalFunctionality, GrantBadgeController, {
this.get("post.username"),
this.badgeReason
)
.then(
(newBadge) => {
.then((newBadge) => {
this.set("selectedBadgeId", null);
this.flash(
I18n.t("badges.successfully_granted", {
@ -74,11 +73,7 @@ export default Controller.extend(ModalFunctionality, GrantBadgeController, {
}),
"success"
);
},
(error) => {
this.flash(extractError(error), "error");
}
)
}, flashAjaxError(this))
.finally(() => this.set("saving", false));
},
},

View File

@ -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));
},
});

View File

@ -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));
},

View File

@ -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);
});

View File

@ -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` callback
if (undoCallback) {
undoCallback(error);
}
throw extractError(error);
throw extractError(error, defaultMessage);
};
}
export function flashAjaxError(modal, defaultMessage) {
return (error) => {
modal.flash(extractError(error, defaultMessage), "error");
};
}

View File

@ -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