Class: BetterErrors::ErrorFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/better_errors/error_frame.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, line, name, frame_binding = nil) ⇒ ErrorFrame

Returns a new instance of ErrorFrame.



15
16
17
18
19
20
21
22
# File 'lib/better_errors/error_frame.rb', line 15

def initialize(filename, line, name, frame_binding = nil)
  @filename       = filename
  @line           = line
  @name           = name
  @frame_binding  = frame_binding
  
  set_pretty_method_name if frame_binding
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



13
14
15
# File 'lib/better_errors/error_frame.rb', line 13

def filename
  @filename
end

#frame_bindingObject (readonly)

Returns the value of attribute frame_binding.



13
14
15
# File 'lib/better_errors/error_frame.rb', line 13

def frame_binding
  @frame_binding
end

#lineObject (readonly)

Returns the value of attribute line.



13
14
15
# File 'lib/better_errors/error_frame.rb', line 13

def line
  @line
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/better_errors/error_frame.rb', line 13

def name
  @name
end

Class Method Details

.from_exception(exception) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/better_errors/error_frame.rb', line 3

def self.from_exception(exception)
  exception.backtrace.each_with_index.map { |frame, idx|
    next unless frame =~ /\A(.*):(\d*):in `(.*)'\z/
    if BetterErrors.binding_of_caller_available?
      b = exception.__better_errors_bindings_stack[idx]
    end
    ErrorFrame.new($1, $2.to_i, $3, b)
  }.compact
end

Instance Method Details

#application?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/better_errors/error_frame.rb', line 24

def application?
  starts_with? filename, BetterErrors.application_root if BetterErrors.application_root
end

#application_pathObject



28
29
30
# File 'lib/better_errors/error_frame.rb', line 28

def application_path
  filename[(BetterErrors.application_root.length+1)..-1]
end

#contextObject



44
45
46
47
48
49
50
51
52
# File 'lib/better_errors/error_frame.rb', line 44

def context
  if application?
    :application
  elsif gem?
    :gem
  else
    :dunno
  end
end

#gem?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/better_errors/error_frame.rb', line 32

def gem?
  Gem.path.any? { |path| starts_with? filename, path }
end

#gem_pathObject



36
37
38
39
40
41
42
# File 'lib/better_errors/error_frame.rb', line 36

def gem_path
  Gem.path.each do |path|
    if starts_with? filename, path
      return filename.gsub("#{path}/gems/", "(gem) ")
    end
  end
end

#instance_variablesObject



74
75
76
77
# File 'lib/better_errors/error_frame.rb', line 74

def instance_variables
  return {} unless frame_binding
  Hash[frame_binding.eval("instance_variables").map { |x| [x, frame_binding.eval(x.to_s)] }]
end

#local_variablesObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/better_errors/error_frame.rb', line 62

def local_variables
  return {} unless frame_binding
  frame_binding.eval("local_variables").each_with_object({}) do |name, hash|
    begin
      hash[name] = frame_binding.eval(name.to_s)
    rescue NameError => e
      # local_variables sometimes returns broken variables.
      # https://bugs.ruby-lang.org/issues/7536
    end
  end
end

#pretty_pathObject



54
55
56
57
58
59
60
# File 'lib/better_errors/error_frame.rb', line 54

def pretty_path
  case context
  when :application;  application_path
  when :gem;          gem_path
  else                filename
  end
end