Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more granularity to ConnectionError through more specific error classes #783

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/http/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ def readpartial(size = BUFFER_SIZE)

# Reads data from socket up until headers are loaded
# @return [void]
# @raise [ResponseHeaderError] when unable to read response headers
def read_headers!
until @parser.headers?
result = read_more(BUFFER_SIZE)
raise ConnectionError, "couldn't read response headers" if result == :eof
raise ResponseHeaderError, "couldn't read response headers" if result == :eof
end

set_keep_alive
Expand Down Expand Up @@ -217,6 +218,7 @@ def set_keep_alive

# Feeds some more data into parser
# @return [void]
# @raise [SocketReadError] when unable to read from socket
def read_more(size)
return if @parser.finished?

Expand All @@ -228,7 +230,7 @@ def read_more(size)
@parser << value
end
rescue IOError, SocketError, SystemCallError => e
raise ConnectionError, "error reading from socket: #{e}", e.backtrace
raise SocketReadError, "error reading from socket: #{e}", e.backtrace
end
end
end
5 changes: 5 additions & 0 deletions lib/http/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class Error < StandardError; end
# Generic Connection error
class ConnectionError < Error; end

# Types of Connection errors
class ResponseHeaderError < ConnectionError; end
class SocketReadError < ConnectionError; end
class SocketWriteError < ConnectionError; end

# Generic Request error
class RequestError < Error; end

Expand Down
3 changes: 2 additions & 1 deletion lib/http/request/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def chunked?

private

# @raise [SocketWriteError] when unable to write to socket
def write(data)
until data.empty?
length = @socket.write(data)
Expand All @@ -118,7 +119,7 @@ def write(data)
rescue Errno::EPIPE
raise
rescue IOError, SocketError, SystemCallError => e
raise ConnectionError, "error writing to socket: #{e}", e.backtrace
raise SocketWriteError, "error writing to socket: #{e}", e.backtrace
end
end
end
Expand Down
Loading