Skip to content

Commit 609acd9

Browse files
committed
🥅 Add new InvalidResponseError exception class
The documentation for `InvalidResponseError` is: > Error raised when the server sends an invalid response. > > This is different from UnknownResponseError: the response has been > rejected. Although it may be parsable, the server is forbidden from > sending it in the current context. The client should automatically > disconnect, abruptly (without logout). > > Note that InvalidResponseError does not inherit from ResponseError: it > can be raised before the response is fully parsed. A related > ResponseParseError or ResponseError may be the #cause. The rdoc for `UnknownResponseError` is updated to: > Error raised upon an unknown response from the server. > > This is different from InvalidResponseError: the response may be a > valid extension response and the server may be allowed to send it in > this context, but Net::IMAP either does not know how to parse it or > how to handle it. This could result from enabling unknown or > unhandled extensions. The connection may still be usable, > but—depending on context—it may be prudent to disconnect. All code that previously raised `UnknownResponseError` now raises `InvalidResponseError`. However, the class is kept both for backward compatibility and because it might be reused in the future for similar scenarios: e.g: when a tag doesn't match any outstanding response tags.
1 parent 934b858 commit 609acd9

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

lib/net/imap.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,8 @@ def get_tagged_response(tag, cmd, timeout = nil)
25192519
when /\A(?:BAD)\z/ni
25202520
raise BadResponseError, resp
25212521
else
2522-
raise UnknownResponseError, resp
2522+
disconnect
2523+
raise InvalidResponseError, "invalid tagged resp: %p" % [resp.raw.chomp]
25232524
end
25242525
end
25252526

lib/net/imap/errors.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,27 @@ class BadResponseError < ResponseError
4747
class ByeResponseError < ResponseError
4848
end
4949

50+
# Error raised when the server sends an invalid response.
51+
#
52+
# This is different from UnknownResponseError: the response has been
53+
# rejected. Although it may be parsable, the server is forbidden from
54+
# sending it in the current context. The client should automatically
55+
# disconnect, abruptly (without logout).
56+
#
57+
# Note that InvalidResponseError does not inherit from ResponseError: it
58+
# can be raised before the response is fully parsed. A related
59+
# ResponseParseError or ResponseError may be the #cause.
60+
class InvalidResponseError < Error
61+
end
62+
5063
# Error raised upon an unknown response from the server.
64+
#
65+
# This is different from InvalidResponseError: the response may be a
66+
# valid extension response and the server may be allowed to send it in
67+
# this context, but Net::IMAP either does not know how to parse it or
68+
# how to handle it. This could result from enabling unknown or
69+
# unhandled extensions. The connection may still be usable,
70+
# but—depending on context—it may be prudent to disconnect.
5171
class UnknownResponseError < ResponseError
5272
end
5373

test/net/imap/test_imap.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ def test_starttls_stripping
147147
imap = nil
148148
starttls_stripping_test do |port|
149149
imap = Net::IMAP.new("localhost", :port => port)
150-
assert_raise(Net::IMAP::UnknownResponseError) do
150+
assert_raise(Net::IMAP::InvalidResponseError) do
151151
imap.starttls(:ca_file => CA_FILE)
152152
end
153+
assert imap.disconnected?
153154
imap
154155
end
155156
assert_equal false, imap.tls_verified?

0 commit comments

Comments
 (0)