Module: Complain
- Defined in:
- lib/complain.rb,
lib/complain/version.rb
Overview
One-liner exception process
# replace
rescue => exc
$stderr.puts exc.
$stderr.puts exc.backtrace
end
# with
rescue => exc
Complain.(exc)
end
Constant Summary collapse
- VERSION =
"1.0.1"
Class Method Summary collapse
-
.call(exc, logger = :stderr, with_time: false, prefix: '', postfix: '', separator: "\n", skip_exc: false) ⇒ Object
Calls exception writer.
Class Method Details
.call(exc, logger = :stderr, with_time: false, prefix: '', postfix: '', separator: "\n", skip_exc: false) ⇒ Object
Calls exception writer
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/complain.rb', line 26 def self.call(exc, logger = :stderr, with_time: false, prefix: '', postfix: '', separator: "\n", skip_exc: false) = if exc.is_a?(Exception) [exc., exc.backtrace.join(separator)] elsif exc.is_a?(String) [exc] elsif exc.respond_to?(:to_s) [exc.to_s] elsif exc.respond_to?(:inspect) [exc.inspect] else ["Message can't be processed at #{caller.join(separator)}"] end .unshift(Time.now.to_s) if with_time = .join(separator) = prefix + + postfix if logger.nil? || :stderr == logger $stderr.puts elsif :stdout == logger $stdout.puts elsif :exception == logger skip_exc = true raise exc elsif logger.respond_to?(:error) logger.error elsif logger.respond_to?(:puts) logger.puts else skip_exc = true Complain.("Can't write to logs:\n" + , :exception, skip_exc: true) end rescue => exc if skip_exc raise exc else Complain.(exc, skip_exc: true) end end |