Class: ExceptionFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/exception_formatter.rb,
lib/exception_formatter/version.rb

Constant Summary collapse

DEFAULT_PATTERN =
"%k: %m\n%b"
VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ExceptionFormatter

Returns a new instance of ExceptionFormatter.



11
12
13
# File 'lib/exception_formatter.rb', line 11

def initialize(options = {})
  @pattern = options[:pattern] ? options[:pattern].to_s : DEFAULT_PATTERN
end

Class Method Details

.format(exception, options = {}) ⇒ Object



7
8
9
# File 'lib/exception_formatter.rb', line 7

def self.format(exception, options = {})
  self.new(options).format(exception)
end

Instance Method Details

#format(exception) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/exception_formatter.rb', line 16

def format(exception)
  s = @pattern.dup
  s = s.gsub('%b', exception.backtrace.join("\n"))
  s = s.gsub(/%c{(\w+)}/) { |match| exception_cause(exception, $1) }
  s = s.gsub('%c', exception_cause(exception, :cause))
  s = s.gsub('%k', exception.class.name)
  s = s.gsub('%m', exception.message.to_s)
  s = s.gsub('%r', exception_record_errors(exception))
  s = s.gsub(/%x{(\w+)}/) { |match| exception_method(exception, $1) }
  s
end