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, stop: '') ⇒ Backtrace

Returns a new instance of Backtrace.



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

def initialize(exp, stop: '')
  @exp = exp
  @stop = stop
end

Class Method Details

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



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

def self.exec(swallow: false, log: nil)
  yield
rescue StandardError => e
  trace = Backtrace.new(e).to_s
  if log.nil?
    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
# File 'lib/backtrace.rb', line 35

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