format_exception 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d80328dd66e21a3a58ca8d3ba455a2fc51220e75
4
- data.tar.gz: 2dffab1f7c1421c02a622797e2e4ddbbb4bd0008
3
+ metadata.gz: cd3a9a8095113d62c9fe6e7a243826d4e9f242e3
4
+ data.tar.gz: bd7f0e0bc52a09b6391de7e87afc66f20a8b0648
5
5
  SHA512:
6
- metadata.gz: 64ca99310e1ebdbb3ffd5023f427e7dc4c4db8eb50d2f9cda1c6c5934c0f5405f8286955f39ded23eec1420d7e94c3e2054714e49387370418e6b9bdb07bb726
7
- data.tar.gz: 998c51089e13986a3d5f625310aebdefe0736d377885c0b413fdb11ba6b125419167b904e4628f11c31d952e1680af451db770638dc07a1af82537118e290c48
6
+ metadata.gz: 0d53a2e53161a8f7b8ac46b82fb4b276ef0f96f04b4564eb8f812b4ba5862efdf8f2ab63dbafb2090bf3b6efcb454bd1cf5aa4e36d9e936f271f6ca1d614462a
7
+ data.tar.gz: 8a23faf1ed7c4493bcaa724dde138d1e4f623a6890be1361cd7252e3b9c2a1ecba981f1cd6abb66df0e3ab1296304f8a53db92df081a70643249bebea0fe36f3
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ --no-private
2
+ --exclude README.md
3
+ --readme README.rdoc
4
+ -
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # FormatException
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/format_exception`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A module of utility methods for string formatting exceptions.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
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}.
@@ -1,3 +1,3 @@
1
1
  module FormatException
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -29,23 +29,20 @@ require "format_exception/version"
29
29
  module FormatException
30
30
 
31
31
  ##
32
- # The contextual clean format
32
+ # The classic format (see {classic})
33
33
  #
34
- # Format the exception as per {clean}, but with an optional +context_message+
35
- # prepended to the first line.
34
+ CLASSIC_FORMAT = "%f: %:m%M (%C)\n%R"
35
+
36
+ ##
37
+ # The clean format (see {clean})
36
38
  #
37
- # @param [Exception] e
38
- # the exception to format
39
- # @param [String] context_message
40
- # the additional message to prepend to the formatted exception
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
- if context_message
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 and message, with the rest of the backtrace on subsequent,
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
- "#{e.backtrace.first}: #{e.message} (#{e.class})\n\t" + e.backtrace.drop(1).join("\n\t")
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
- "#{e.class}: #{e.message}:\n\t" + e.backtrace.join("\n\t")
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.1.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