From 6acbae50544459d2731c09d4c620f93a20d73e8f Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Fri, 7 Mar 2025 11:31:01 -0500 Subject: [PATCH] Include invalid status code when raising error --- CHANGELOG.md | 2 ++ lib/websocket/error.rb | 6 ++++++ lib/websocket/handshake/client.rb | 9 ++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c1ac99e..8feecb9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Edge +- include invalid status code when raising handshake error + ## 1.2.11 - remove unused base64 require that would cause issues in Ruby 3.4 diff --git a/lib/websocket/error.rb b/lib/websocket/error.rb index ec63e9f7..bef50645 100644 --- a/lib/websocket/error.rb +++ b/lib/websocket/error.rb @@ -108,6 +108,12 @@ def message end class InvalidStatusCode < ::WebSocket::Error::Handshake + attr_reader :invalid_status_code + + def initialize(invalid_status_code) + @invalid_status_code = invalid_status_code + end + def message :invalid_status_code end diff --git a/lib/websocket/handshake/client.rb b/lib/websocket/handshake/client.rb index d1ae0e00..46a085a0 100644 --- a/lib/websocket/handshake/client.rb +++ b/lib/websocket/handshake/client.rb @@ -123,7 +123,14 @@ def parse_first_line(line) line_parts = line.match(FIRST_LINE) raise WebSocket::Error::Handshake::InvalidHeader unless line_parts status = line_parts[1] - raise WebSocket::Error::Handshake::InvalidStatusCode unless status == '101' + + # The signature for the error doesn't take the message as the first + # argument, but rather the status code. So this should use the + # initializer to create the error. + # + # rubocop:disable Style/RaiseArgs + raise WebSocket::Error::Handshake::InvalidStatusCode.new(status) unless status == '101' + # rubocop:enable Style/RaiseArgs end end end