From fae0ac5770d1dd487bbb81ae4972c6c80fbf5956 Mon Sep 17 00:00:00 2001 From: Omkar Moghe Date: Tue, 7 May 2024 12:46:09 -0700 Subject: [PATCH 1/2] changes and docs --- lib/http/connection.rb | 6 ++++-- lib/http/errors.rb | 3 +++ lib/http/request/writer.rb | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) 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..e163d8eb 100644 --- a/lib/http/errors.rb +++ b/lib/http/errors.rb @@ -6,6 +6,9 @@ class Error < StandardError; end # Generic Connection error class ConnectionError < Error; end + 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 From 2ab77786c4b04d608c99217334d597d9cebe239c Mon Sep 17 00:00:00 2001 From: Omkar Moghe Date: Tue, 7 May 2024 12:48:26 -0700 Subject: [PATCH 2/2] comment --- lib/http/errors.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/http/errors.rb b/lib/http/errors.rb index e163d8eb..8f6c2061 100644 --- a/lib/http/errors.rb +++ b/lib/http/errors.rb @@ -6,6 +6,8 @@ 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