8000 Add `ConnectionError` with response. · socketry/async-websocket@9746e62 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9746e62

Browse files
committed
Add ConnectionError with response.
1 parent de416c5 commit 9746e62

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

async-websocket.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
2626
spec.required_ruby_version = ">= 3.1"
2727

2828
spec.add_dependency "async-http", "~> 0.54"
29+
spec.add_dependency "protocol-http", ">= 0.28.1"
2930
spec.add_dependency "protocol-rack", "~> 0.5"
3031
spec.add_dependency "protocol-websocket", "~> 0.15"
3132
end

lib/async/websocket/client.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ def connect(authority, path, scheme: @delegate.scheme, headers: nil, handler: Co
110110
response = request.call(connection)
111111

112112
unless response.stream?
113-
response.close
114-
115-
raise ProtocolError, "Failed to negotiate connection: #{response.status}"
113+
raise ConnectionError.new("Failed to negotiate connection!", response.unwrap)
116114
end
117115

118116
protocol = response.headers[SEC_WEBSOCKET_PROTOCOL]&.first

lib/async/websocket/connect_request.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def close
2828
@response.close
2929
end
3030

31+
def unwrap
32+
@response.buffered!
33+
end
34+
3135
attr_accessor :response
3236
attr_accessor :stream
3337

lib/async/websocket/error.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,16 @@ class Error < ::Protocol::WebSocket::Error
1515

1616
class UnsupportedVersionError < Error
1717
end
18+
19+
class ConnectionError < Error
20+
def initialize(message, response)
21+
super(message)
22+
23+
@response = response
24+
end
25+
26+
# The failed HTTP response.
27+
attr :response
28+
end
1829
end
1930
end

lib/async/websocket/upgrade_request.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def close
3333
@response.close
3434
end
3535

36+
def unwrap
37+
@response.buffered!
38+
end
39+
3640
attr_accessor :response
3741

3842
def stream?

test/async/websocket/client.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
end
1919

2020
connection.close
21+
rescue Protocol::WebSocket::ClosedError
22+
# Ignore this error.
2123
end or Protocol::HTTP::Response[404, {}, []]
2224
end
2325
end
@@ -125,7 +127,24 @@
125127
it "raises an error when the server doesn't support websockets" do
126128
expect do
127129
Async::WebSocket::Client.connect(client_endpoint) {}
128-
end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Failed to negotiate connection/)
130+
end.to raise_exception(Async::WebSocket::ConnectionError, message: be =~ /Failed to negotiate connection/)
131+
end
132+
end
133+
134+
with 'deliberate failure response' do
135+
let(:app) do
136+
Protocol::HTTP::Middleware.for do |request|
137+
Protocol::HTTP::Response[401, {}, ["You are not allowed!"]]
138+
end
139+
end
140+
141+
it "raises a connection error when the server responds with an error" do
142+
begin
143+
Async::WebSocket::Client.connect(client_endpoint) {}
144+
rescue Async::WebSocket::ConnectionError => error
145+
expect(error.response.status).to be == 401
146+
expect(error.response.read).to be == "You are not allowed!"
147+
end
129148
end
130149
end
131150
end
@@ -134,7 +153,7 @@
134153
it 'raises an error' do
135154
expect do
136155
Async::WebSocket::Client.connect(client_endpoint) {}
137-
end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Failed to negotiate connection/)
156+
end.to raise_exception(Async::WebSocket::ConnectionError, message: be =~ /Failed to negotiate connection/)
138157
end
139158
end
140159

0 commit comments

Comments
 (0)
0