Skip to content

Commit ad87ee8

Browse files
committed
Better DuplicateHeaderError detailed message.
1 parent 0d59b03 commit ad87ee8

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

lib/protocol/http/error.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,30 @@ class DuplicateHeaderError < Error
1919
include BadRequest
2020

2121
# @parameter key [String] The header key that was duplicated.
22-
def initialize(key)
22+
def initialize(key, existing_value, new_value)
2323
super("Duplicate singleton header key: #{key.inspect}")
24+
25+
@key = key
26+
@existing_value = existing_value
27+
@new_value = new_value
2428
end
2529

2630
# @attribute [String] key The header key that was duplicated.
2731
attr :key
32+
33+
# @attribute [String] existing_value The existing value for the duplicated header.
34+
attr :existing_value
35+
36+
# @attribute [String] new_value The new value for the duplicated header.
37+
attr :new_value
38+
39+
def detailed_message(highlight: false)
40+
<<~MESSAGE
41+
#{self.message}
42+
Existing value: #{@existing_value.inspect}
43+
New value: #{@new_value.inspect}
44+
MESSAGE
45+
end
2846
end
2947

3048
# Raised when an invalid trailer header is encountered in headers.

lib/protocol/http/headers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ def delete(key)
453453
end
454454
else
455455
if hash.key?(key)
456-
raise DuplicateHeaderError, key
456+
raise DuplicateHeaderError.new(key, hash[key], value)
457457
end
458458

459459
hash[key] = value

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- `Protocol::HTTP::DuplicateHeaderError` now includes the existing and new values for better debugging.
6+
37
## v0.58.0
48

59
- Move trailer validation to `Headers#add` method to ensure all additions are checked at the time of addition as this is a hard requirement.

test/protocol/http/error.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2018-2026, by Samuel Williams.
5+
6+
require "protocol/http/error"
7+
8+
describe Protocol::HTTP::DuplicateHeaderError do
9+
let(:key) {"content-length"}
10+
let(:existing_value) {"100"}
11+
let(:new_value) {"200"}
12+
let(:error) {subject.new(key, existing_value, new_value)}
13+
14+
with "#initialize" do
15+
it "should set the key and values" do
16+
expect(error.key).to be == key
17+
expect(error.existing_value).to be == existing_value
18+
expect(error.new_value).to be == new_value
19+
end
20+
21+
it "should have a descriptive message" do
22+
expect(error.message).to be =~ /Duplicate singleton header key: "content-length"/
23+
end
24+
end
25+
26+
with "#detailed_message" do
27+
it "should include the header key and both values" do
28+
message = error.detailed_message
29+
30+
expect(message).to be =~ /Duplicate singleton header key: "content-length"/
31+
expect(message).to be =~ /Existing value: "100"/
32+
expect(message).to be =~ /New value: "200"/
33+
end
34+
35+
it "should work with highlight parameter" do
36+
message = error.detailed_message(highlight: true)
37+
38+
expect(message).to be =~ /Duplicate singleton header key: "content-length"/
39+
expect(message).to be =~ /Existing value: "100"/
40+
expect(message).to be =~ /New value: "200"/
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)