Module: FormatException
- Defined in:
- lib/format_exception.rb,
lib/format_exception/version.rb
Overview
Simple exception formatter
Provides utility methods for formatting an exception as a String.
Constant Summary collapse
- CLASSIC_FORMAT =
The classic format (see classic)
"%f: %:m%M (%C)\n%R"- CLEAN_FORMAT =
The clean format (see clean)
"%:m%C: %M:\n%B"- VERSION =
"0.2.2"
Class Method Summary collapse
-
.[](e, context_message = nil) ⇒ Object
Alias for FormatException.clean.
-
.classic(e, context_message = nil) ⇒ String
The Ruby interpreter’s default format.
-
.clean(e, context_message = nil) ⇒ String
The log-friendly format.
-
.format(f, e, c = nil) ⇒ Object
Format exception as per printf-like format specifier.
Class Method Details
.[](e, context_message = nil) ⇒ Object
Alias for clean
45 46 47 |
# File 'lib/format_exception.rb', line 45 def self.[](e, = nil) clean(e, ) end |
.classic(e, context_message = nil) ⇒ String
The Ruby interpreter’s default format
Formats the exception exactly as the Ruby interpreter would if the exception was uncaught. The first line includes the first line of the backtrace, and the exception message and class name, with the rest of the backtrace on subsequent, indented lines.
If the context_message is given, it is included on the first line, between the first line of the backtrace and the exception message.
66 67 68 |
# File 'lib/format_exception.rb', line 66 def self.classic(e, = nil) format(CLASSIC_FORMAT, e, ) end |
.clean(e, context_message = nil) ⇒ String
The log-friendly format
Formats the exception as the Rails logger would, with the exception class name and message on the first line, with the backtrace on subsequent, indented lines.
If the context_message is given, it is prepended to the first line.
84 85 86 |
# File 'lib/format_exception.rb', line 84 def self.clean(e, = nil) format(CLEAN_FORMAT, e, ) end |
.format(f, e, c = nil) ⇒ Object
Format exception as per printf-like format specifier
The following format specifiers are supported:
-
%C - the exception class name
-
%M - the exception message
-
%m - the context message if given
-
%:m - the context message, a colon and a space, if the context message is given
-
%f - the first line of the backtrace, unindented
-
%r - all lines of the backtrace but the first, newline-separated, unindented
-
%R - all lines of the backtrace but the first, newline-separated, indented
-
%b - all lines of the backtrace, newline-separated, unindented
-
%B - all lines of the backtrace, newline-separated, indented
-
%% - a literal %
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/format_exception.rb', line 104 def self.format(f, e, c = nil) scanner = StringScanner.new(f) formatted = "" loop do formatted << scanner.scan(/[^%]*/) token = scanner.scan(/%:?./) case token when "%C" then formatted << e.class.to_s when "%M" then formatted << e. when "%m" then formatted << c if c when "%:m" then formatted << "#{c}: " if c when "%f" then formatted << e.backtrace.first when "%r" then formatted << e.backtrace.drop(1).join("\n") when "%R" then formatted << ("\t" + e.backtrace.drop(1).join("\n\t")) when "%b" then formatted << e.backtrace.join("\n") when "%B" then formatted << ("\t" + e.backtrace.join("\n\t")) when "%%" then formatted << "%" when nil then break else raise ArgumentError, "unknown format specifier '#{scanner.matched}'" end break if scanner.eos? end formatted end |