Skip to content

Commit

Permalink
Fix JSON::GeneratorError#detailed_message with Ruby < 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
rahim committed Mar 5, 2025
1 parent f3e1136 commit 2e015ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,13 @@ def initialize(message, invalid_object = nil)
end

def detailed_message(...)
# Exception#detailed_message doesn't exist until Ruby 3.2
super_message = defined?(super) ? super : message

if @invalid_object.nil?
super
super_message
else
"#{super}\nInvalid object: #{@invalid_object.inspect}"
"#{super_message}\nInvalid object: #{@invalid_object.inspect}"
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ def test_json_generate
end
end

def test_json_generate_error_detailed_message
error = assert_raise JSON::GeneratorError do
generate(["\xea"])
end

assert_not_nil(error.detailed_message)
end

def test_json_generate_unsupported_types
assert_raise JSON::GeneratorError do
generate(Object.new, strict: true)
Expand Down

0 comments on commit 2e015ff

Please sign in to comment.