Exception: Literal::TypeError

Inherits:
TypeError
  • Object
show all
Includes:
Error
Defined in:
lib/literal/errors/type_error.rb

Defined Under Namespace

Classes: Context

Constant Summary collapse

INDENT =
"  "

Instance Method Summary collapse

Constructor Details

#initialize(context:) ⇒ TypeError

Returns a new instance of TypeError.



46
47
48
49
50
# File 'lib/literal/errors/type_error.rb', line 46

def initialize(context:)
  @context = context

  super()
end

Instance Method Details

#deconstruct_keys(keys) ⇒ Object



76
77
78
# File 'lib/literal/errors/type_error.rb', line 76

def deconstruct_keys(keys)
  to_h.slice(*keys)
end

#messageObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/literal/errors/type_error.rb', line 52

def message
  message = +"Type mismatch\n\n"

  @context.descend do |c, level|
    idt = INDENT * level
    if c.receiver || c.method
      message << idt
      message << c.receiver.class.inspect if c.receiver
      message << c.method if c.method
      message << " (from #{backtrace[1]})" if level.zero?
      message << "\n"
    end
    if c.label
      idt << INDENT
      message << idt << c.label << "\n"
    end
    if c.expected && c.children.empty?
      message << idt << "  Expected: #{c.expected.inspect}\n"
      message << idt << "  Actual (#{c.actual.class}): #{c.actual.inspect}\n"
    end
  end
  message
end

#to_hObject



80
81
82
# File 'lib/literal/errors/type_error.rb', line 80

def to_h
  @context.to_h
end