Open
Description
Literal::TypeError
delegates #to_h
to @context
but Literal::TypeError::Context
does not define #to_h
. An implementation could be:
class Literal::TypeError < TypeError
class Context
def to_h
{
receiver: @receiver,
method: @method,
label: @label,
expected: @expected,
actual: @actual,
children: @children
}
end
alias to_hash to_h
end
end
Steps to reproduce:
begin
Name.new first: "very long string here"
rescue => e
puts "> error.class.name : #{e.class.name}"
puts "> error.to_h.inspect : #{e.to_h.inspect}"
end
As of 1.8.0
, that code produces:
# > error.class.name : Literal::TypeError
# /Users/unknown/.asdf/installs/ruby/3.3.1/lib/ruby/gems/3.3.0/gems/literal-1.8.0/lib/literal/errors/type_error.rb:94:in `to_h': undefined method `to_h' for an instance of Literal::TypeError::Context (NoMethodError)
#
# @context.to_h
# ^^^^^
# Did you mean? to_s
# from (irb):9:in `rescue in <top (required)>'
# from (irb):5:in `<main>'
# from <internal:kernel>:187:in `loop'
# from bin/console:11:in `<main>'
# (irb):6:in `new': Type mismatch (Literal::TypeError)
#
# Name#initialize (from (irb):6:in `<main>')
# first:
# .length
# Expected: 1..10
# Actual (Integer): 21
#
#
# raise Literal::TypeError.new(context:)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# from (irb):6:in `<main>'
# from <internal:kernel>:187:in `loop'
# from bin/console:11:in `<main>'
With the proposed implementation:
# > error.class.name : Literal::TypeError
# > error.to_h.isnpect : {:receiver=>#<Name:0x000000011f097458>, :method=>"#initialize", :label=>"first:", :expected=>_Constraint(String, length: 1..10), :actual=>"very long string here", :children=>[{:receiver=>nil, :method=>nil, :label=>".length", :expected=>1..10, :actual=>21, :children=>[]}]}
Metadata
Metadata
Assignees
Labels
No labels