Skip to content

Commit

Permalink
🥅 Add new InvalidResponseError exception class
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nevans committed Oct 21, 2023
1 parent 934b858 commit 609acd9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/net/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,8 @@ def get_tagged_response(tag, cmd, timeout = nil)
when /\A(?:BAD)\z/ni
raise BadResponseError, resp
else
raise UnknownResponseError, resp
disconnect
raise InvalidResponseError, "invalid tagged resp: %p" % [resp.raw.chomp]
end
end

Expand Down
20 changes: 20 additions & 0 deletions lib/net/imap/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,27 @@ class BadResponseError < ResponseError
class ByeResponseError < ResponseError
end

# 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.
class InvalidResponseError < Error
end

# 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.
class UnknownResponseError < ResponseError
end

Expand Down
3 changes: 2 additions & 1 deletion test/net/imap/test_imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ def test_starttls_stripping
imap = nil
starttls_stripping_test do |port|
imap = Net::IMAP.new("localhost", :port => port)
assert_raise(Net::IMAP::UnknownResponseError) do
assert_raise(Net::IMAP::InvalidResponseError) do
imap.starttls(:ca_file => CA_FILE)
end
assert imap.disconnected?
imap
end
assert_equal false, imap.tls_verified?
Expand Down

0 comments on commit 609acd9

Please sign in to comment.