Skip to content
Browse files

Tidy up error handling in Connection

  • Loading branch information...
1 parent 9bbaf06 commit 8df4620fac5ff48f508e3ccd9b22a0ec0c8e016d @ConradIrwin committed
Showing with 10 additions and 15 deletions.
  1. +10 −15 lib/em-imap/connection.rb
View
25 lib/em-imap/connection.rb
@@ -48,7 +48,6 @@ def send_command(cmd, *args)
Command.new(next_tag!, cmd, args).tap do |command|
add_to_listener_pool(command)
listen_for_tagged_response(command)
- listen_for_bye_response(command)
send_command_object(command)
end
end
@@ -67,27 +66,25 @@ def add_response_handler(&block)
Listener.new(&block).tap do |listener|
listener.stopback{ listener.succeed }
add_to_listener_pool(listener)
- listen_for_bye_response(listener)
end
end
def post_init
@listeners = []
super
- listen_for_greeting
listen_for_failure
+ listen_for_greeting
end
# Listen for the first response from the server and succeed or fail
# the connection deferrable.
def listen_for_greeting
add_to_listener_pool(hello_listener)
- listen_for_bye_response(hello_listener)
hello_listener.listen do |response|
# TODO: Is this the right condition? I think it can be one of several
# possible answers depending on how trusted the connection is, but probably
# not *anything* except BYE.
- if response.is_a?(Net::IMAP::UntaggedResponse)
+ if response.is_a?(Net::IMAP::UntaggedResponse) && response.name != "BYE"
hello_listener.succeed response
else
hello_listener.fail Net::IMAP::ResponseParseError.new(response.raw_data)
@@ -116,6 +113,14 @@ def listen_for_failure
@listeners.clone.each{ |listener| listener.fail error } while @listeners.size > 0
close_connection unless @unbound
end
+
+ # If we receive a BYE response from the server, then we're not going
+ # to hear any more, so we fail all our listeners.
+ add_response_handler do |response|
+ if response.is_a?(Net::IMAP::UntaggedResponse) && response.name == "BYE"
+ fail Net::IMAP::ByeResponseError.new(response.raw_data)
+ end
+ end
end
def add_to_listener_pool(listener)
@@ -148,16 +153,6 @@ def listen_for_tagged_response(command)
end
end
- # If we receive a BYE response from the server, then we're not going
- # to hear any more, so we fail all our listeners.
- def listen_for_bye_response(listener)
- listener.listen do |response|
- if response.is_a?(Net::IMAP::UntaggedResponse) && response.name == "BYE"
- listener.fail Net::IMAP::ByeResponseError.new(response.raw_data)
- end
- end
- end
-
# Provides a next_tag! method to generate unique tags
# for an IMAP session.
module TagSequence

0 comments on commit 8df4620

Please sign in to comment.
Something went wrong with that request. Please try again.