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.3.0"
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.
-
.data(e, context_message = nil) ⇒ Hash
Format exception as symbol-keyed dictionary.
-
.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
101 102 103 |
# File 'lib/format_exception.rb', line 101 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.
122 123 124 |
# File 'lib/format_exception.rb', line 122 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.
140 141 142 |
# File 'lib/format_exception.rb', line 140 def self.clean(e, = nil) format(CLEAN_FORMAT, e, ) end |
.data(e, context_message = nil) ⇒ Hash
Format exception as symbol-keyed dictionary
If the context_message is given, it is included as the :context_message key.
159 160 161 162 163 164 165 166 167 |
# File 'lib/format_exception.rb', line 159 def self.data(e, = nil) { name: e.class.to_s, message: e., backtrace: e.backtrace, }.tap { |h| h[:context_message] = if } 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 %
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/format_exception.rb', line 185 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 |