Commit 1f5d5590 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge the places where exceptions could be raised

parent 863d8e5a
...@@ -24,6 +24,8 @@ class EmailReceiverWorker ...@@ -24,6 +24,8 @@ class EmailReceiverWorker
reason = nil reason = nil
case e case e
when Gitlab::Email::UnknownIncomingEmail
reason = "We couldn't figure out what the email is for."
when Gitlab::Email::SentNotificationNotFoundError when Gitlab::Email::SentNotificationNotFoundError
reason = "We couldn't figure out what the email is in reply to. Please create your comment through the web interface." reason = "We couldn't figure out what the email is in reply to. Please create your comment through the web interface."
when Gitlab::Email::ProjectNotFound when Gitlab::Email::ProjectNotFound
......
...@@ -14,10 +14,11 @@ module Gitlab ...@@ -14,10 +14,11 @@ module Gitlab
end end
def can_handle? def can_handle?
!!(project_namespace && project) !!authentication_token
end end
def execute def execute
raise ProjectNotFound unless project
validate_permission!(:create_issue) validate_permission!(:create_issue)
verify_record!( verify_record!(
......
...@@ -6,7 +6,8 @@ module Gitlab ...@@ -6,7 +6,8 @@ module Gitlab
module Handler module Handler
class CreateNoteHandler < BaseHandler class CreateNoteHandler < BaseHandler
def can_handle? def can_handle?
!!(mail_key && sent_notification) # We want to raise SentNotificationNotFoundError for missing key
!!(mail_key.nil? || mail_key =~ /\A\w+\z/)
end end
def execute def execute
......
...@@ -16,6 +16,7 @@ module Gitlab ...@@ -16,6 +16,7 @@ module Gitlab
class NoteableNotFoundError < ProcessingError; end class NoteableNotFoundError < ProcessingError; end
class InvalidNoteError < ProcessingError; end class InvalidNoteError < ProcessingError; end
class InvalidIssueError < ProcessingError; end class InvalidIssueError < ProcessingError; end
class UnknownIncomingEmail < ProcessingError; end
class Receiver class Receiver
def initialize(raw) def initialize(raw)
...@@ -30,11 +31,8 @@ module Gitlab ...@@ -30,11 +31,8 @@ module Gitlab
if handler = Handler.for(mail, mail_key) if handler = Handler.for(mail, mail_key)
handler.execute handler.execute
elsif mail_key =~ %r{/|\+}
# Sent Notification mail_key would not have / or +
raise ProjectNotFound
else else
raise SentNotificationNotFoundError raise UnknownIncomingEmail
end end
end end
......
...@@ -30,6 +30,14 @@ describe Gitlab::Email::Receiver, lib: true do ...@@ -30,6 +30,14 @@ describe Gitlab::Email::Receiver, lib: true do
) )
end end
context "when we cannot find a capable handler" do
let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "!!!") }
it "raises a UnknownIncomingEmail" do
expect { receiver.execute }.to raise_error(Gitlab::Email::UnknownIncomingEmail)
end
end
context "when the recipient address doesn't include a mail key" do context "when the recipient address doesn't include a mail key" do
let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "") } let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "") }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment