Skip to content

Commit

Permalink
Add more granularity to ConnectionError through more specific error…
Browse files Browse the repository at this point in the history
… classes (#783)
  • Loading branch information
omkarmoghe authored Jul 24, 2024
1 parent ba0733e commit b74b16c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
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

0 comments on commit b74b16c

Please sign in to comment.