Class: ErrorToCommunicate::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/error_to_communicate/config.rb

Constant Summary collapse

DEFAULT_HEURISTICS =
[
  Heuristic::WrongNumberOfArguments,
  Heuristic::NoMethodError,
  Heuristic::LoadError,
  Heuristic::SyntaxError,
  Heuristic::Exception,
]
DEFAULT_BLACKLIST =

Should maybe also be an array, b/c there’s no great way to add a proc to the blacklist, right now, it would have to check it’s thing and then call the next one

lambda do |einfo|
  einfo.classname == 'SystemExit'
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



43
44
45
46
47
48
49
50
51
# File 'lib/error_to_communicate/config.rb', line 43

def initialize(options={})
  self.heuristics  = options.fetch(:heuristics)      { DEFAULT_HEURISTICS }
  self.blacklist   = options.fetch(:blacklist)       { DEFAULT_BLACKLIST }
  self.theme       = options.fetch(:theme)           { Theme.new } # this is still really fkn rough
  self.format_with = options.fetch(:format_with)     { FormatTerminal }
  loaded_features  = options.fetch(:loaded_features) { $LOADED_FEATURES }
  root             = options.fetch(:root)            { File.expand_path Dir.pwd }
  self.project     = Project.new root: root, loaded_features: loaded_features
end

Instance Attribute Details

#blacklistObject

Returns the value of attribute blacklist.



41
42
43
# File 'lib/error_to_communicate/config.rb', line 41

def blacklist
  @blacklist
end

#catchall_heuristicObject

Returns the value of attribute catchall_heuristic.



41
42
43
# File 'lib/error_to_communicate/config.rb', line 41

def catchall_heuristic
  @catchall_heuristic
end

#format_withObject

Returns the value of attribute format_with.



41
42
43
# File 'lib/error_to_communicate/config.rb', line 41

def format_with
  @format_with
end

#heuristicsObject

Returns the value of attribute heuristics.



41
42
43
# File 'lib/error_to_communicate/config.rb', line 41

def heuristics
  @heuristics
end

#projectObject

Returns the value of attribute project.



41
42
43
# File 'lib/error_to_communicate/config.rb', line 41

def project
  @project
end

#themeObject

Returns the value of attribute theme.



41
42
43
# File 'lib/error_to_communicate/config.rb', line 41

def theme
  @theme
end

Class Method Details

.defaultObject



37
38
39
# File 'lib/error_to_communicate/config.rb', line 37

def self.default
  @default ||= new
end

Instance Method Details

#accept?(exception, binding) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/error_to_communicate/config.rb', line 53

def accept?(exception, binding)
  return false unless ExceptionInfo.parseable? exception, binding
  einfo = ExceptionInfo.parse(exception, binding)
  !blacklist.call(einfo) && !!heuristics.find { |h| h.for? einfo }
end

#format(heuristic, cwd) ⇒ Object



66
67
68
# File 'lib/error_to_communicate/config.rb', line 66

def format(heuristic, cwd)
  format_with.call theme: theme, heuristic: heuristic, cwd: Pathname.new(cwd)
end

#heuristic_for(exception, binding) ⇒ Object



59
60
61
62
63
64
# File 'lib/error_to_communicate/config.rb', line 59

def heuristic_for(exception, binding)
  accept?(exception, binding) || raise(ArgumentError, "Asked for a heuristic on an object we don't accept: #{exception.inspect}")
  einfo = ExceptionInfo.parse(exception, binding)
  heuristics.find { |heuristic| heuristic.for? einfo }
            .new(einfo: einfo, project: project)
end