Read and display HTTP response body on WebSocket handshake failure

400/4xx responses include a body (Content-Length) with the actual error
message, which was previously swallowed. Now captured and shown in the
RuntimeError so the root cause is visible without --debug.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-22 13:10:18 +01:00
parent 9fe6ef339a
commit 7235f687b1
+14 -8
View File
@@ -23,11 +23,7 @@ module PortainerCli
socket = open_socket
handshake = perform_handshake(socket)
unless handshake.valid?
raise "WebSocket handshake failed: #{handshake.error}\nServer response:\n#{handshake.instance_variable_get(:@data)}"
end
perform_handshake(socket)
run_io_loop(socket)
ensure
socket&.close rescue nil
@@ -59,15 +55,25 @@ module PortainerCli
debug "Handshake request:\n#{handshake.to_s.chomp}"
socket.write(handshake.to_s)
response = +''
header_bytes = +''
loop do
byte = socket.read(1)
break if byte.nil?
response << byte
header_bytes << byte
handshake << byte
break if handshake.finished?
end
debug "Handshake response:\n#{response.chomp}"
debug "Handshake response headers:\n#{header_bytes.chomp}"
unless handshake.valid?
# Try to read the response body for error details
body = ''
if (len = header_bytes.match(/Content-Length:\s*(\d+)/i)&.[](1)&.to_i) && len > 0
body = socket.read(len).to_s
end
raise "WebSocket handshake failed: #{handshake.error}\n" \
"Server response:\n#{header_bytes.chomp}\n#{body}"
end
handshake
end