diff --git a/lib/http/connection.rb b/lib/http/connection.rb index 24d34a98..adeac882 100644 --- a/lib/http/connection.rb +++ b/lib/http/connection.rb @@ -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 @@ -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? @@ -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 diff --git a/lib/http/errors.rb b/lib/http/errors.rb index f0f44ab8..8f6c2061 100644 --- a/lib/http/errors.rb +++ b/lib/http/errors.rb @@ -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 diff --git a/lib/http/request/writer.rb b/lib/http/request/writer.rb index ce06c410..a1a6df63 100644 --- a/lib/http/request/writer.rb +++ b/lib/http/request/writer.rb @@ -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) @@ -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