Use error message from the server when reshare failed

This commit is contained in:
Steffen van Bergerem 2016-11-18 23:01:57 +01:00 committed by Benjamin Neff
parent 8078c60cee
commit 6c5c865b15
7 changed files with 31 additions and 10 deletions

View file

@ -123,8 +123,8 @@ app.models.Post.Interactions = Backbone.Model.extend({
interactions.set({"reshares_count": interactions.get("reshares_count") + 1});
interactions.reshares.trigger("change");
})
.fail(function(){
app.flashMessages.error(Diaspora.I18n.t("reshares.duplicate"));
.fail(function(response) {
app.flashMessages.handleAjaxError(response);
});
app.instrument("track", "Reshare");

View file

@ -98,8 +98,12 @@
success: function() {
Diaspora.Mobile.PostActions.toggleActive(link);
},
error: function() {
alert(Diaspora.I18n.t("failed_to_reshare"));
error: function(response) {
if (response.status === 0) {
alert(Diaspora.I18n.t("errors.connection"));
} else {
alert(response.responseText);
}
},
complete: function() {
Diaspora.Mobile.PostActions.hideLoader(link);

View file

@ -14,7 +14,7 @@ class ResharesController < ApplicationController
current_user.dispatch_post(@reshare)
render :json => ExtremePostPresenter.new(@reshare, current_user), :status => 201
else
render :nothing => true, :status => 422
render text: I18n.t("javascripts.failed_to_reshare"), status: 422
end
end

View file

@ -199,7 +199,6 @@ en:
hide: "Hide comments"
no_comments: "There are no comments yet."
reshares:
duplicate: "That good, eh? Youve already reshared that post!"
successful: "The post was successfully reshared!"
post: "Reshare <%= name %>s post?"
aspect_navigation:

View file

@ -46,7 +46,7 @@ describe ResharesController, :type => :controller do
it 'doesn\'t allow the user to reshare the post again' do
post_request!
expect(response.code).to eq('422')
expect(response.body.strip).to be_empty
expect(response.body).to eq(I18n.t("javascripts.failed_to_reshare"))
end
end

View file

@ -6,6 +6,8 @@ describe("app.models.Post.Interactions", function(){
this.interactions = this.post.interactions;
this.author = factory.author({guid: "loggedInAsARockstar"});
loginAs({guid: "loggedInAsARockstar"});
spec.content().append($("<div id='flash-container'>"));
app.flashMessages = new app.views.FlashMessages({el: spec.content().find("#flash-container")});
this.userLike = new app.models.Like({author : this.author});
});
@ -110,6 +112,16 @@ describe("app.models.Post.Interactions", function(){
jasmine.Ajax.requests.mostRecent().respondWith(ajaxSuccess);
expect(this.post.get("participation")).toBeTruthy();
});
it("displays a flash message on errors", function() {
spyOn(app.flashMessages, "handleAjaxError").and.callThrough();
this.interactions.reshare();
jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "error message"});
expect(app.flashMessages.handleAjaxError).toHaveBeenCalled();
expect(app.flashMessages.handleAjaxError.calls.argsFor(0)[0].responseText).toBe("error message");
expect(spec.content().find(".flash-message")).toBeErrorFlashMessage("error message");
});
});
describe("userLike", function(){

View file

@ -223,10 +223,16 @@ describe("Diaspora.Mobile.PostActions", function(){
expect(Diaspora.Mobile.PostActions.toggleActive).toHaveBeenCalledWith(this.reshareLink);
});
it("pops an alert on error", function(){
it("pops an alert on server errors", function() {
this.reshareLink.click();
jasmine.Ajax.requests.mostRecent().respondWith({status: 400});
expect(window.alert).toHaveBeenCalledWith(Diaspora.I18n.t("failed_to_reshare"));
jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "reshare failed"});
expect(window.alert).toHaveBeenCalledWith("reshare failed");
});
it("pops an alert on network errors", function() {
this.reshareLink.click();
jasmine.Ajax.requests.mostRecent().abort();
expect(window.alert).toHaveBeenCalledWith(Diaspora.I18n.t("errors.connection"));
});
});
});