DEV: Rely on helpers for ajax error handling to reduce repetition (#20988)
This commit is contained in:
parent
bdaaac9c9f
commit
0ff86feb96
|
@ -2,7 +2,7 @@ import Controller, { inject as controller } from "@ember/controller";
|
||||||
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
||||||
import { changeEmail } from "discourse/lib/user-activation";
|
import { changeEmail } from "discourse/lib/user-activation";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
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, {
|
export default Controller.extend(ModalFunctionality, {
|
||||||
login: controller(),
|
login: controller(),
|
||||||
|
@ -31,7 +31,7 @@ export default Controller.extend(ModalFunctionality, {
|
||||||
});
|
});
|
||||||
modal.set("currentEmail", this.newEmail);
|
modal.set("currentEmail", this.newEmail);
|
||||||
})
|
})
|
||||||
.catch((err) => this.flash(extractError(err), "error"));
|
.catch(flashAjaxError(this));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import Controller from "@ember/controller";
|
import Controller from "@ember/controller";
|
||||||
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
||||||
import { action } from "@ember/object";
|
import { action } from "@ember/object";
|
||||||
import { extractError } from "discourse/lib/ajax-error";
|
import { flashAjaxError } from "discourse/lib/ajax-error";
|
||||||
|
|
||||||
export default Controller.extend(ModalFunctionality, {
|
export default Controller.extend(ModalFunctionality, {
|
||||||
duration: null,
|
duration: null,
|
||||||
|
@ -18,9 +18,7 @@ export default Controller.extend(ModalFunctionality, {
|
||||||
.then(() => {
|
.then(() => {
|
||||||
this.send("closeModal");
|
this.send("closeModal");
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch(flashAjaxError(this));
|
||||||
this.flash(extractError(e), "error");
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
|
|
@ -6,7 +6,7 @@ import DiscourseURL from "discourse/lib/url";
|
||||||
import I18n from "I18n";
|
import I18n from "I18n";
|
||||||
import { NotificationLevels } from "discourse/lib/notification-levels";
|
import { NotificationLevels } from "discourse/lib/notification-levels";
|
||||||
import PermissionType from "discourse/models/permission-type";
|
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 { underscore } from "@ember/string";
|
||||||
import { inject as service } from "@ember/service";
|
import { inject as service } from "@ember/service";
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ export default Controller.extend({
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
this.dialog.alert(extractError(error));
|
popupAjaxError(error);
|
||||||
this.set("saving", false);
|
this.set("saving", false);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { ajax } from "discourse/lib/ajax";
|
||||||
import cookie from "discourse/lib/cookie";
|
import cookie from "discourse/lib/cookie";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
import { escapeExpression } from "discourse/lib/utilities";
|
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 getURL from "discourse-common/lib/get-url";
|
||||||
import { isEmpty } from "@ember/utils";
|
import { isEmpty } from "@ember/utils";
|
||||||
|
|
||||||
|
@ -90,9 +90,7 @@ export default Controller.extend(ModalFunctionality, {
|
||||||
this.set("helpSeen", !data.user_found);
|
this.set("helpSeen", !data.user_found);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch(flashAjaxError(this))
|
||||||
this.flash(extractError(e), "error");
|
|
||||||
})
|
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
this.set("disabled", false);
|
this.set("disabled", false);
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,7 +6,7 @@ import ModalFunctionality from "discourse/mixins/modal-functionality";
|
||||||
import UserBadge from "discourse/models/user-badge";
|
import UserBadge from "discourse/models/user-badge";
|
||||||
import { all } from "rsvp";
|
import { all } from "rsvp";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
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, {
|
export default Controller.extend(ModalFunctionality, GrantBadgeController, {
|
||||||
topicController: controller("topic"),
|
topicController: controller("topic"),
|
||||||
|
@ -64,21 +64,16 @@ export default Controller.extend(ModalFunctionality, GrantBadgeController, {
|
||||||
this.get("post.username"),
|
this.get("post.username"),
|
||||||
this.badgeReason
|
this.badgeReason
|
||||||
)
|
)
|
||||||
.then(
|
.then((newBadge) => {
|
||||||
(newBadge) => {
|
this.set("selectedBadgeId", null);
|
||||||
this.set("selectedBadgeId", null);
|
this.flash(
|
||||||
this.flash(
|
I18n.t("badges.successfully_granted", {
|
||||||
I18n.t("badges.successfully_granted", {
|
username: this.get("post.username"),
|
||||||
username: this.get("post.username"),
|
badge: newBadge.get("badge.name"),
|
||||||
badge: newBadge.get("badge.name"),
|
}),
|
||||||
}),
|
"success"
|
||||||
"success"
|
);
|
||||||
);
|
}, flashAjaxError(this))
|
||||||
},
|
|
||||||
(error) => {
|
|
||||||
this.flash(extractError(error), "error");
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.finally(() => this.set("saving", false));
|
.finally(() => this.set("saving", false));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -2,7 +2,7 @@ import Controller from "@ember/controller";
|
||||||
import { action } from "@ember/object";
|
import { action } from "@ember/object";
|
||||||
import { isEmpty } from "@ember/utils";
|
import { isEmpty } from "@ember/utils";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
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 { emailValid } from "discourse/lib/utilities";
|
||||||
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
||||||
import I18n from "I18n";
|
import I18n from "I18n";
|
||||||
|
@ -76,7 +76,7 @@ export default Controller.extend(ModalFunctionality, {
|
||||||
|
|
||||||
this.send("closeModal");
|
this.send("closeModal");
|
||||||
})
|
})
|
||||||
.catch((error) => this.flash(extractError(error), "error"))
|
.catch(flashAjaxError(this))
|
||||||
.finally(() => this.set("loading", false));
|
.finally(() => this.set("loading", false));
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,7 +10,7 @@ import { SECOND_FACTOR_METHODS } from "discourse/models/user";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
import discourseComputed from "discourse-common/utils/decorators";
|
||||||
import { escape } from "pretty-text/sanitizer";
|
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 { findAll } from "discourse/models/login-method";
|
||||||
import getURL from "discourse-common/lib/get-url";
|
import getURL from "discourse-common/lib/get-url";
|
||||||
import { getWebauthnCredential } from "discourse/lib/webauthn";
|
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));
|
.finally(() => this.set("processingEmailLink", false));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import DiscourseURL from "discourse/lib/url";
|
||||||
import I18n from "I18n";
|
import I18n from "I18n";
|
||||||
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
||||||
import discourseComputed from "discourse-common/utils/decorators";
|
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 { isEmpty } from "@ember/utils";
|
||||||
import { next } from "@ember/runloop";
|
import { next } from "@ember/runloop";
|
||||||
|
|
||||||
|
@ -168,9 +168,7 @@ export default Controller.extend(ModalFunctionality, {
|
||||||
this.topicController.send("toggleMultiSelect");
|
this.topicController.send("toggleMultiSelect");
|
||||||
DiscourseURL.routeTo(result.url);
|
DiscourseURL.routeTo(result.url);
|
||||||
})
|
})
|
||||||
.catch((xhr) => {
|
.catch(flashAjaxError(this, I18n.t("topic.move_to.error")))
|
||||||
this.flash(extractError(xhr, I18n.t("topic.move_to.error")), "error");
|
|
||||||
})
|
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
this.set("saving", false);
|
this.set("saving", false);
|
||||||
});
|
});
|
||||||
|
|
|
@ -73,13 +73,19 @@ export function extractError(error, defaultMessage) {
|
||||||
return extractErrorInfo(error, defaultMessage).message;
|
return extractErrorInfo(error, defaultMessage).message;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function throwAjaxError(undoCallback) {
|
export function throwAjaxError(undoCallback, defaultMessage) {
|
||||||
return function (error) {
|
return function (error) {
|
||||||
// If we provided an `undo` callback
|
// If we provided an `undo` callback
|
||||||
if (undoCallback) {
|
if (undoCallback) {
|
||||||
undoCallback(error);
|
undoCallback(error);
|
||||||
}
|
}
|
||||||
throw extractError(error);
|
throw extractError(error, defaultMessage);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function flashAjaxError(modal, defaultMessage) {
|
||||||
|
return (error) => {
|
||||||
|
modal.flash(extractError(error, defaultMessage), "error");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import discourseDebounce from "discourse-common/lib/debounce";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { cancel } from "@ember/runloop";
|
import { cancel } from "@ember/runloop";
|
||||||
import { action, computed } from "@ember/object";
|
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 ModalFunctionality from "discourse/mixins/modal-functionality";
|
||||||
import { inject as service } from "@ember/service";
|
import { inject as service } from "@ember/service";
|
||||||
export default class ChatChannelEditTitleController extends Controller.extend(
|
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.model.set("title", result.channel.title);
|
||||||
this.send("closeModal");
|
this.send("closeModal");
|
||||||
})
|
})
|
||||||
.catch((event) => {
|
.catch(flashAjaxError(this));
|
||||||
this.flash(extractError(event), "error");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
|
Loading…
Reference in New Issue