From aac4a4ed94a48838a76a8aae279bf60b3faf7d98 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Thu, 2 Mar 2017 20:37:28 -0300 Subject: [PATCH] Handle invalid parameters and missing bounced emails --- .../modals/admin-incoming-email.js.es6 | 8 +++++++- app/controllers/admin/email_controller.rb | 20 +++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/modals/admin-incoming-email.js.es6 b/app/assets/javascripts/admin/controllers/modals/admin-incoming-email.js.es6 index ca2d44e2510..3d57d2a6c37 100644 --- a/app/assets/javascripts/admin/controllers/modals/admin-incoming-email.js.es6 +++ b/app/assets/javascripts/admin/controllers/modals/admin-incoming-email.js.es6 @@ -2,6 +2,7 @@ import ModalFunctionality from 'discourse/mixins/modal-functionality'; import IncomingEmail from 'admin/models/incoming-email'; import computed from 'ember-addons/ember-computed-decorators'; import { longDate } from 'discourse/lib/formatter'; +import { popupAjaxError } from 'discourse/lib/ajax-error'; export default Ember.Controller.extend(ModalFunctionality, { @@ -15,7 +16,12 @@ export default Ember.Controller.extend(ModalFunctionality, { }, loadFromBounced(id) { - return IncomingEmail.findByBounced(id).then(result => this.set("model", result)); + return IncomingEmail.findByBounced(id) + .then(result => this.set("model", result)) + .catch(error => { + this.send("closeModal"); + popupAjaxError(error); + }); } }); diff --git a/app/controllers/admin/email_controller.rb b/app/controllers/admin/email_controller.rb index 8e6c3b53b0e..077937e0218 100644 --- a/app/controllers/admin/email_controller.rb +++ b/app/controllers/admin/email_controller.rb @@ -91,13 +91,21 @@ class Admin::EmailController < Admin::AdminController def incoming_from_bounced params.require(:id) - bounced = EmailLog.find(params[:id].to_i) - email_local_part, email_domain = SiteSetting.notification_email.split('@') - bounced_to_address = "#{email_local_part}+verp-#{bounced.bounce_key}@#{email_domain}" + begin + bounced = EmailLog.find_by(id: params[:id].to_i) + raise Discourse::InvalidParameters if bounced.nil? - incoming_email = IncomingEmail.find_by(to_addresses: bounced_to_address) - serializer = IncomingEmailDetailsSerializer.new(incoming_email, root: false) - render_json_dump(serializer) + email_local_part, email_domain = SiteSetting.notification_email.split('@') + bounced_to_address = "#{email_local_part}+verp-#{bounced.bounce_key}@#{email_domain}" + + incoming_email = IncomingEmail.find_by(to_addresses: bounced_to_address) + raise Discourse::NotFound if incoming_email.nil? + + serializer = IncomingEmailDetailsSerializer.new(incoming_email, root: false) + render_json_dump(serializer) + rescue => e + render json: {errors: [e.message]}, status: 404 + end end private