Skip to content

Commit b74b16c

Browse files
authored
Add more granularity to ConnectionError through more specific error classes (#783)
1 parent ba0733e commit b74b16c

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

lib/http/connection.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ def readpartial(size = BUFFER_SIZE)
105105

106106
# Reads data from socket up until headers are loaded
107107
# @return [void]
108+
# @raise [ResponseHeaderError] when unable to read response headers
108109
def read_headers!
109110
until @parser.headers?
110111
result = read_more(BUFFER_SIZE)
111-
raise ConnectionError, "couldn't read response headers" if result == :eof
112+
raise ResponseHeaderError, "couldn't read response headers" if result == :eof
112113
end
113114

114115
set_keep_alive
@@ -217,6 +218,7 @@ def set_keep_alive
217218

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

@@ -228,7 +230,7 @@ def read_more(size)
228230
@parser << value
229231
end
230232
rescue IOError, SocketError, SystemCallError => e
231-
raise ConnectionError, "error reading from socket: #{e}", e.backtrace
233+
raise SocketReadError, "error reading from socket: #{e}", e.backtrace
232234
end
233235
end
234236
end

lib/http/errors.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ class Error < StandardError; end
77
# Generic Connection error
88
class ConnectionError < Error; end
99

10+
# Types of Connection errors
11+
class ResponseHeaderError < ConnectionError; end
12+
class SocketReadError < ConnectionError; end
13+
class SocketWriteError < ConnectionError; end
14+
1015
# Generic Request error
1116
class RequestError < Error; end
1217

lib/http/request/writer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def chunked?
108108

109109
private
110110

111+
# @raise [SocketWriteError] when unable to write to socket
111112
def write(data)
112113
until data.empty?
113114
length = @socket.write(data)
@@ -118,7 +119,7 @@ def write(data)
118119
rescue Errno::EPIPE
119120
raise
120121
rescue IOError, SocketError, SystemCallError => e
121-
raise ConnectionError, "error writing to socket: #{e}", e.backtrace
122+
raise SocketWriteError, "error writing to socket: #{e}", e.backtrace
122123
end
123124
end
124125
end

0 commit comments

Comments
 (0)