diff --git a/app/controllers/api/v1/accounts/inboxes_controller.rb b/app/controllers/api/v1/accounts/inboxes_controller.rb index ded4db598..95662e29b 100644 --- a/app/controllers/api/v1/accounts/inboxes_controller.rb +++ b/app/controllers/api/v1/accounts/inboxes_controller.rb @@ -43,14 +43,18 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController def update @inbox.update!(permitted_params.except(:channel)) - @inbox.update_working_hours(params.permit(working_hours: Inbox::OFFISABLE_ATTRS)[:working_hours]) if params[:working_hours] + update_inbox_working_hours channel_attributes = get_channel_attributes(@inbox.channel_type) # Inbox update doesn't necessarily need channel attributes return if permitted_params(channel_attributes)[:channel].blank? if @inbox.inbox_type == 'Email' - validate_email_channel(channel_attributes) + begin + validate_email_channel(channel_attributes) + rescue StandardError => e + render json: { message: e }, status: :unprocessable_entity and return + end @inbox.channel.reauthorized! end @@ -58,6 +62,10 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController update_channel_feature_flags end + def update_inbox_working_hours + @inbox.update_working_hours(params.permit(working_hours: Inbox::OFFISABLE_ATTRS)[:working_hours]) if params[:working_hours] + end + def agent_bot @agent_bot = @inbox.agent_bot end @@ -89,12 +97,6 @@ class Api::V1::Accounts::InboxesController < Api::V1::Accounts::BaseController @agent_bot = AgentBot.find(params[:agent_bot]) if params[:agent_bot] end - def inbox_name(channel) - return channel.try(:bot_name) if channel.is_a?(Channel::Telegram) - - permitted_params[:name] - end - def create_channel return unless %w[web_widget api email line telegram whatsapp sms].include?(permitted_params[:channel][:type]) diff --git a/app/helpers/api/v1/inboxes_helper.rb b/app/helpers/api/v1/inboxes_helper.rb index 82644e62f..aa28e4ccc 100644 --- a/app/helpers/api/v1/inboxes_helper.rb +++ b/app/helpers/api/v1/inboxes_helper.rb @@ -1,4 +1,10 @@ module Api::V1::InboxesHelper + def inbox_name(channel) + return channel.try(:bot_name) if channel.is_a?(Channel::Telegram) + + permitted_params[:name] + end + def validate_email_channel(attributes) channel_data = permitted_params(attributes)[:channel] @@ -19,8 +25,7 @@ module Api::V1::InboxesHelper enable_ssl: channel_data[:imap_enable_ssl] } end - Mail.connection do # rubocop:disable:block - end + check_imap_connection(channel_data) end def validate_smtp(channel_data) @@ -32,6 +37,25 @@ module Api::V1::InboxesHelper check_smtp_connection(channel_data, smtp) end + def check_imap_connection(channel_data) + Mail.connection {} # rubocop:disable:block + rescue SocketError => e + raise StandardError, I18n.t('errors.inboxes.imap.socket_error') + rescue Net::IMAP::NoResponseError => e + raise StandardError, I18n.t('errors.inboxes.imap.no_response_error') + rescue Errno::EHOSTUNREACH => e + raise StandardError, I18n.t('errors.inboxes.imap.host_unreachable_error') + rescue Net::OpenTimeout => e + raise StandardError, + I18n.t('errors.inboxes.imap.connection_timed_out_error', address: channel_data[:imap_address], port: channel_data[:imap_port]) + rescue Net::IMAP::Error => e + raise StandardError, I18n.t('errors.inboxes.imap.connection_closed_error') + rescue StandardError => e + raise StandardError, e.message + ensure + Rails.logger.error e if e.present? + end + def check_smtp_connection(channel_data, smtp) smtp.start(channel_data[:smtp_domain], channel_data[:smtp_login], channel_data[:smtp_password], channel_data[:smtp_authentication]&.to_sym || :login) diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/ImapSettings.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/ImapSettings.vue index af2d5dd5c..072fc20f3 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/ImapSettings.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/ImapSettings.vue @@ -156,7 +156,7 @@ export default { await this.$store.dispatch('inboxes/updateInboxIMAP', payload); this.showAlert(this.$t('INBOX_MGMT.IMAP.EDIT.SUCCESS_MESSAGE')); } catch (error) { - this.showAlert(this.$t('INBOX_MGMT.IMAP.EDIT.ERROR_MESSAGE')); + this.showAlert(error.message); } }, }, diff --git a/app/javascript/dashboard/store/modules/inboxes.js b/app/javascript/dashboard/store/modules/inboxes.js index b16ed4799..668107ef7 100644 --- a/app/javascript/dashboard/store/modules/inboxes.js +++ b/app/javascript/dashboard/store/modules/inboxes.js @@ -216,7 +216,11 @@ export const actions = { commit(types.default.SET_INBOXES_UI_FLAG, { isUpdatingIMAP: false, }); - throw new Error(error); + if (error.response?.data?.message) { + throw new Error(error.response?.data?.message); + } else { + throw new Error(error); + } } }, updateInboxSMTP: async ( diff --git a/app/mailboxes/mailbox_helper.rb b/app/mailboxes/mailbox_helper.rb index 927ab5eb5..6aaf4fde3 100644 --- a/app/mailboxes/mailbox_helper.rb +++ b/app/mailboxes/mailbox_helper.rb @@ -21,6 +21,8 @@ module MailboxHelper end def add_attachments_to_message + return if @message.blank? + processed_mail.attachments.each do |mail_attachment| attachment = @message.attachments.new( account_id: @conversation.account_id, diff --git a/config/locales/en.yml b/config/locales/en.yml index b986ef1c1..d7bf7c4e7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -46,6 +46,13 @@ en: contacts: import: failed: File is blank + inboxes: + imap: + socket_error: Please check the network connection, IMAP address and try again. + no_response_error: Please check the IMAP credentials and try again. + host_unreachable_error: Host unreachable, Please check the IMAP address, IMAP port and try again. + connection_timed_out_error: Connection timed out for %{address}:%{port} + connection_closed_error: Connection closed. reports: period: Reporting period %{since} to %{until}