format_exception 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/.yardopts +4 -0
- data/README.md +4 -6
- data/README.rdoc +10 -0
- data/lib/format_exception/version.rb +1 -1
- data/lib/format_exception.rb +65 -18
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd3a9a8095113d62c9fe6e7a243826d4e9f242e3
|
4
|
+
data.tar.gz: bd7f0e0bc52a09b6391de7e87afc66f20a8b0648
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d53a2e53161a8f7b8ac46b82fb4b276ef0f96f04b4564eb8f812b4ba5862efdf8f2ab63dbafb2090bf3b6efcb454bd1cf5aa4e36d9e936f271f6ca1d614462a
|
7
|
+
data.tar.gz: 8a23faf1ed7c4493bcaa724dde138d1e4f623a6890be1361cd7252e3b9c2a1ecba981f1cd6abb66df0e3ab1296304f8a53db92df081a70643249bebea0fe36f3
|
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# FormatException
|
2
2
|
|
3
|
-
|
3
|
+
A module of utility methods for string formatting exceptions.
|
4
4
|
|
5
|
-
|
5
|
+
## Documentation
|
6
|
+
|
7
|
+
For documentation of the released gem, see [rubydoc.info](http://www.rubydoc.info/gems/format_exception).
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -20,10 +22,6 @@ Or install it yourself as:
|
|
20
22
|
|
21
23
|
$ gem install format_exception
|
22
24
|
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
TODO: Write usage instructions here
|
26
|
-
|
27
25
|
## Development
|
28
26
|
|
29
27
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/README.rdoc
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
= FormatException
|
2
|
+
|
3
|
+
Provides utility methods for formatting an exception as a String.
|
4
|
+
|
5
|
+
Two very common formats are supported directly, with other formats made possible with a printf-like format specifier.
|
6
|
+
The inclusion of a context message is also supported
|
7
|
+
|
8
|
+
The methods are provided by the module {FormatException}.
|
9
|
+
|
10
|
+
The implementation is covered by an rspec test suite in the {https://github.com/hetznerZA/format_exception FormatException repo}.
|
data/lib/format_exception.rb
CHANGED
@@ -29,23 +29,20 @@ require "format_exception/version"
|
|
29
29
|
module FormatException
|
30
30
|
|
31
31
|
##
|
32
|
-
# The
|
32
|
+
# The classic format (see {classic})
|
33
33
|
#
|
34
|
-
|
35
|
-
|
34
|
+
CLASSIC_FORMAT = "%f: %:m%M (%C)\n%R"
|
35
|
+
|
36
|
+
##
|
37
|
+
# The clean format (see {clean})
|
36
38
|
#
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
#
|
41
|
-
# @return [String] the formatted exception
|
39
|
+
CLEAN_FORMAT = "%:m%C: %M:\n%B"
|
40
|
+
|
41
|
+
##
|
42
|
+
# Alias for {clean}
|
42
43
|
#
|
43
44
|
def self.[](e, context_message = nil)
|
44
|
-
|
45
|
-
"#{context_message}: " + clean(e)
|
46
|
-
else
|
47
|
-
clean(e)
|
48
|
-
end
|
45
|
+
clean(e, context_message)
|
49
46
|
end
|
50
47
|
|
51
48
|
##
|
@@ -53,15 +50,20 @@ module FormatException
|
|
53
50
|
#
|
54
51
|
# Formats the exception exactly as the Ruby interpreter would if the exception
|
55
52
|
# was uncaught. The first line includes the first line of the backtrace, and the
|
56
|
-
# exception class name
|
53
|
+
# exception message and class name, with the rest of the backtrace on subsequent,
|
57
54
|
# indented lines.
|
58
55
|
#
|
56
|
+
# If the +context_message+ is given, it is included on the first line, between
|
57
|
+
# the first line of the backtrace and the exception message.
|
58
|
+
#
|
59
59
|
# @param [Exception] e
|
60
60
|
# the exception to format
|
61
|
+
# @param [String] context_message
|
62
|
+
# the additional message to include in the formatted exception
|
61
63
|
# @return [String] the formatted exception
|
62
64
|
#
|
63
|
-
def self.classic(e)
|
64
|
-
|
65
|
+
def self.classic(e, context_message = nil)
|
66
|
+
format(CLASSIC_FORMAT, e, context_message)
|
65
67
|
end
|
66
68
|
|
67
69
|
##
|
@@ -70,12 +72,57 @@ module FormatException
|
|
70
72
|
# Formats the exception as the Rails logger would, with the exception class name
|
71
73
|
# and message on the first line, with the backtrace on subsequent, indented lines.
|
72
74
|
#
|
75
|
+
# If the +context_message+ is given, it is prepended to the first line.
|
76
|
+
#
|
73
77
|
# @param [Exception] e
|
74
78
|
# the exception to format
|
79
|
+
# @param [String] context_message
|
80
|
+
# the additional message to prepend to the formatted exception
|
75
81
|
# @return [String] the formatted exception
|
76
82
|
#
|
77
|
-
def self.clean(e)
|
78
|
-
|
83
|
+
def self.clean(e, context_message = nil)
|
84
|
+
format(CLEAN_FORMAT, e, context_message)
|
79
85
|
end
|
80
86
|
|
87
|
+
##
|
88
|
+
# Format exception as per printf-like format specifier
|
89
|
+
#
|
90
|
+
# The following format specifiers are supported:
|
91
|
+
#
|
92
|
+
# * +%C+ - the exception class name
|
93
|
+
# * +%M+ - the exception message
|
94
|
+
# * +%m+ - the context message if given
|
95
|
+
# * +%:m+ - the context message, a colon and a space, if the context message is given
|
96
|
+
# * +%f+ - the first line of the backtrace, unindented
|
97
|
+
# * +%r+ - all lines of the backtrace but the first, newline-separated, unindented
|
98
|
+
# * +%R+ - all lines of the backtrace but the first, newline-separated, indented
|
99
|
+
# * +%b+ - all lines of the backtrace, newline-separated, unindented
|
100
|
+
# * +%B+ - all lines of the backtrace, newline-separated, indented
|
101
|
+
# * +%%+ - a literal +%+
|
102
|
+
#
|
103
|
+
def self.format(f, e, c = nil)
|
104
|
+
scanner = StringScanner.new(f)
|
105
|
+
formatted = ""
|
106
|
+
loop do
|
107
|
+
formatted << scanner.scan(/[^%]*/)
|
108
|
+
token = scanner.scan(/%:?./)
|
109
|
+
case token
|
110
|
+
when "%C" then formatted << e.class.to_s
|
111
|
+
when "%M" then formatted << e.message
|
112
|
+
when "%m" then formatted << c if c
|
113
|
+
when "%:m" then formatted << "#{c}: " if c
|
114
|
+
when "%f" then formatted << e.backtrace.first
|
115
|
+
when "%r" then formatted << e.backtrace.drop(1).join("\n")
|
116
|
+
when "%R" then formatted << ("\t" + e.backtrace.drop(1).join("\n\t"))
|
117
|
+
when "%b" then formatted << e.backtrace.join("\n")
|
118
|
+
when "%B" then formatted << ("\t" + e.backtrace.join("\n\t"))
|
119
|
+
when "%%" then formatted << "%"
|
120
|
+
when nil then break
|
121
|
+
else
|
122
|
+
raise ArgumentError, "unknown format specifier '#{scanner.matched}'"
|
123
|
+
end
|
124
|
+
break if scanner.eos?
|
125
|
+
end
|
126
|
+
formatted
|
127
|
+
end
|
81
128
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: format_exception
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sheldon Hearn
|
@@ -61,8 +61,11 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- ".yardopts"
|
64
66
|
- Gemfile
|
65
67
|
- README.md
|
68
|
+
- README.rdoc
|
66
69
|
- Rakefile
|
67
70
|
- bin/console
|
68
71
|
- bin/setup
|