From 4c86758f74e9f42ae925e76802169a29ad8a3cff Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Mon, 14 Dec 2015 21:32:23 +0530 Subject: [PATCH] FIX: show proper message on invite error --- .../discourse/controllers/invite.js.es6 | 20 ++++++++++++-- app/controllers/invites_controller.rb | 26 ++++++++++++------- app/controllers/topics_controller.rb | 18 ++++++++----- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/invite.js.es6 b/app/assets/javascripts/discourse/controllers/invite.js.es6 index bb94adae25c..1747ee1c444 100644 --- a/app/assets/javascripts/discourse/controllers/invite.js.es6 +++ b/app/assets/javascripts/discourse/controllers/invite.js.es6 @@ -149,6 +149,7 @@ export default Ember.Controller.extend(ModalFunctionality, { createInvite() { const Invite = require('discourse/models/invite').default; + const self = this; if (this.get('disabled')) { return; } @@ -168,11 +169,19 @@ export default Ember.Controller.extend(ModalFunctionality, { } else if (this.get('isMessage') && result && result.user) { this.get('model.details.allowed_users').pushObject(result.user); } - }).catch(() => model.setProperties({ saving: false, error: true })); + }).catch(function(e) { + if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) { + self.set("errorMessage", e.jqXHR.responseJSON.errors[0]); + } else { + self.set("errorMessage", self.get('isMessage') ? I18n.t('topic.invite_private.error') : I18n.t('topic.invite_reply.error')); + } + model.setProperties({ saving: false, error: true }) + }); }, generateInvitelink() { const Invite = require('discourse/models/invite').default; + const self = this; if (this.get('disabled')) { return; } @@ -193,7 +202,14 @@ export default Ember.Controller.extend(ModalFunctionality, { userInvitedController.set('model', invite_model); userInvitedController.set('totalInvites', invite_model.invites.length); }); - }).catch(() => model.setProperties({ saving: false, error: true })); + }).catch(function(e) { + if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) { + self.set("errorMessage", e.jqXHR.responseJSON.errors[0]); + } else { + self.set("errorMessage", self.get('isMessage') ? I18n.t('topic.invite_private.error') : I18n.t('topic.invite_reply.error')); + } + model.setProperties({ saving: false, error: true }) + }); } } diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb index 1012248748e..1a7f87b7aac 100644 --- a/app/controllers/invites_controller.rb +++ b/app/controllers/invites_controller.rb @@ -41,10 +41,14 @@ class InvitesController < ApplicationController guardian.ensure_can_send_multiple_invites!(current_user) end - if Invite.invite_by_email(params[:email], current_user, _topic=nil, group_ids) - render json: success_json - else - render json: failed_json, status: 422 + begin + if Invite.invite_by_email(params[:email], current_user, _topic=nil, group_ids) + render json: success_json + else + render json: failed_json, status: 422 + end + rescue => e + render json: {errors: [e.message]}, status: 422 end end @@ -59,11 +63,15 @@ class InvitesController < ApplicationController guardian.ensure_can_send_multiple_invites!(current_user) end - # generate invite link - if invite_link = Invite.generate_invite_link(params[:email], current_user, topic, group_ids) - render_json_dump(invite_link) - else - render json: failed_json, status: 422 + begin + # generate invite link + if invite_link = Invite.generate_invite_link(params[:email], current_user, topic, group_ids) + render_json_dump(invite_link) + else + render json: failed_json, status: 422 + end + rescue => e + render json: {errors: [e.message]}, status: 422 end end diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 7848cbb34bc..1a61e740d0b 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -326,15 +326,19 @@ class TopicsController < ApplicationController group_ids = Group.lookup_group_ids(params) guardian.ensure_can_invite_to!(topic,group_ids) - if topic.invite(current_user, username_or_email, group_ids) - user = User.find_by_username_or_email(username_or_email) - if user - render_json_dump BasicUserSerializer.new(user, scope: guardian, root: 'user') + begin + if topic.invite(current_user, username_or_email, group_ids) + user = User.find_by_username_or_email(username_or_email) + if user + render_json_dump BasicUserSerializer.new(user, scope: guardian, root: 'user') + else + render json: success_json + end else - render json: success_json + render json: failed_json, status: 422 end - else - render json: failed_json, status: 422 + rescue => e + render json: {errors: [e.message]}, status: 422 end end