Class: Backtrace

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

Overview

Backtrace as a string.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2018 Yegor Bugayenko

License

MIT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp, mine: '') ⇒ Backtrace

Returns a new instance of Backtrace.


30
31
32
33
# File 'lib/backtrace.rb', line 30

def initialize(exp, mine: '')
  @exp = exp
  @mine = (mine.is_a?(Regexp) ? mine : Regexp.new(Regexp.quote(mine)))
end

Class Method Details

.exec(swallow: false, log: nil, mine: '') ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/backtrace.rb', line 47

def self.exec(swallow: false, log: nil, mine: '')
  yield
rescue StandardError => e
  trace = Backtrace.new(e, mine: mine).to_s
  if log.nil? || !log.respond_to?(:error)
    puts trace
  else
    log.error(trace)
  end
  raise e unless swallow
end

Instance Method Details

#to_sObject


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/backtrace.rb', line 35

def to_s
  [
    @exp.class.name,
    ': ',
    @exp.message,
    "\n\t",
    @exp.backtrace.reverse
      .drop_while { |t| @mine.match(t).nil? }
      .reverse.join("\n\t")
  ].join
end