exception_formatter 0.1.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.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /.idea
19
+ /coverage*
20
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Capfile ADDED
@@ -0,0 +1,16 @@
1
+ require "tb_setup_ci/recipes"
2
+
3
+ set :user, "jenkins"
4
+ set :ci_job, "exception_formatter"
5
+ set :ci_job_git_branch, "master"
6
+ set :repository, "git@git.thomasbaustert.de:repos/exception_formatter.git"
7
+
8
+ #set :db_config_dir, "test/dummy/config/"
9
+
10
+ set :ci_run_tests, false
11
+ set :ci_run_specs, true
12
+ set :ci_run_coverage, false
13
+ set :ci_coverage_task, "app:spec:rcov"
14
+
15
+ role :ci, "ci.thomasbaustert.de"
16
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in exception_formatter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Thomas Baustert
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # ExceptionFormatter
2
+
3
+ *I was sick of writing "#{exception.class.name}: #{exception.message}\\n#{exception.backtrace.join("\\n")}"*
4
+
5
+ Simple formatter for ruby exception messages.
6
+
7
+ Features:
8
+
9
+ * Custom format pattern
10
+ * Nested exception support
11
+ * ActiveRecord:::RecordInvalid exception support
12
+
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'exception_formatter'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install exception_formatter
27
+
28
+ ## Examples
29
+
30
+ ### Using default pattern
31
+
32
+ If no pattern is given the default pattern is used (see ExceptionFormatter::DEFAULT_PATTERN):
33
+
34
+ begin
35
+ ... code ... code ... code
36
+ rescue => ex
37
+ Rails.logger.error ExceptionFormatter.format(ex)
38
+ end
39
+
40
+ Will produce the string:
41
+
42
+ StandardError: the exception message
43
+ file:31
44
+ file:76
45
+
46
+ ### Using custom pattern
47
+
48
+ begin
49
+ ... code ... code ... code
50
+ rescue => ex
51
+ Rails.logger.error ExceptionFormatter.format(ex, pattern: "[%k] %m\nStacktrace:\n%b")
52
+ end
53
+
54
+ Will produce the string:
55
+
56
+ [StandardError] the exception message
57
+ Stacktrace:
58
+ file:31
59
+ file:76
60
+
61
+ Alternatively use instance method:
62
+
63
+ FORMATTER = ExceptionFormatter.new(pattern: "[%k] %m\n%b")
64
+
65
+ begin
66
+ ... code ... code ... code
67
+ rescue => ex
68
+ Rails.logger.error FORMATTER.format(ex)
69
+ end
70
+
71
+ ## Available pattern
72
+
73
+ %b = exception backtrace joined with "\n"
74
+ %c = cause, the nested exception message (by calling exception.cause.message)
75
+ %c{method} = cause, the nested exception message (by calling exception.method.message)
76
+ (useful if nested exception does not respond to #cause but to #reason or what ever)
77
+ %k = exception class name
78
+ %m = exception message
79
+ %r = record errors, the record error messages in case of ActiveRecord:::RecordInvalid exception
80
+ (by calling exception.record.errors.full_messages)
81
+ %x{method} = message return by calling exception.send(method)
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # running spec as rake default tasks
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+
7
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'exception_formatter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "exception_formatter"
8
+ spec.version = ExceptionFormatter::VERSION
9
+ spec.authors = ["Thomas Baustert"]
10
+ spec.email = ["business@thomasbaustert.de"]
11
+ spec.description = %q{Simple formatter for ruby exception messages.}
12
+ spec.summary = %q{Simple formatter for ruby exception messages.}
13
+ spec.homepage = "https://github.com/thomasbaustert/exception_formatter"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,3 @@
1
+ class ExceptionFormatter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,52 @@
1
+ require "exception_formatter/version"
2
+
3
+ class ExceptionFormatter
4
+
5
+ DEFAULT_PATTERN = "%k: %m\n%b"
6
+
7
+ def self.format(exception, options = {})
8
+ self.new(options).format(exception)
9
+ end
10
+
11
+ def initialize(options = {})
12
+ @pattern = options[:pattern] ? options[:pattern].to_s : DEFAULT_PATTERN
13
+ end
14
+
15
+
16
+ def format(exception)
17
+ s = @pattern.dup
18
+ s = s.gsub('%b', exception.backtrace.join("\n"))
19
+ s = s.gsub(/%c{(\w+)}/) { |match| exception_cause(exception, $1) }
20
+ s = s.gsub('%c', exception_cause(exception, :cause))
21
+ s = s.gsub('%k', exception.class.name)
22
+ s = s.gsub('%m', exception.message.to_s)
23
+ s = s.gsub('%r', exception_record_errors(exception))
24
+ s = s.gsub(/%x{(\w+)}/) { |match| exception_method(exception, $1) }
25
+ s
26
+ end
27
+
28
+ private
29
+
30
+ def exception_cause(exception, method)
31
+ if exception.respond_to?(method) && exception.send(method).respond_to?(:message)
32
+ exception.send(method).message
33
+ else
34
+ ""
35
+ end
36
+ end
37
+
38
+ def exception_record_errors(exception)
39
+ if exception.respond_to?(:record) &&
40
+ exception.record.respond_to?(:errors)
41
+ exception.record.errors.respond_to?(:full_messages)
42
+ exception.record.errors.full_messages.to_s
43
+ else
44
+ ""
45
+ end
46
+ end
47
+
48
+ def exception_method(exception, method)
49
+ exception.respond_to?(method) ? exception.send(method) : ""
50
+ end
51
+
52
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+ require 'exception_formatter'
3
+
4
+ describe ExceptionFormatter do
5
+
6
+ describe "::format" do
7
+
8
+ it "delegates to #format" do
9
+ exception = double('exception', message: "error", backtrace: ["file:1", "file:2"])
10
+ formatter = double('exception_formatter')
11
+
12
+ ExceptionFormatter.should_receive(:new).with(pattern: "%m\n%b").and_return(formatter)
13
+ formatter.should_receive(:format).with(exception)
14
+
15
+ ExceptionFormatter.format(exception, pattern: "%m\n%b")
16
+ end
17
+
18
+ context "no pattern given" do
19
+ it "returns default message with exception message and backtrace " do
20
+ exception = new_exception(class: double("FooError", name: "FooError"))
21
+ formatter = ExceptionFormatter.new
22
+ formatter.format(exception).should == "FooError: A Error Message\nline1\nline2"
23
+ end
24
+ end
25
+
26
+ context "pattern %m" do
27
+ it "returns message with exception message" do
28
+ exception = new_exception(message: "a freaking error")
29
+ ExceptionFormatter.format(exception, pattern: "%m").should == "a freaking error"
30
+ end
31
+ end
32
+
33
+ context "pattern %k" do
34
+ it "returns message with exception class" do
35
+ exception = new_exception(class: double("StandardError", name: "StandardError"))
36
+ ExceptionFormatter.format(exception, pattern: "%k").should == "StandardError"
37
+ end
38
+ end
39
+
40
+ context "pattern %b" do
41
+ it "returns message with exception backtrace" do
42
+ exception = new_exception(backtrace: ["file:42", "file:76"])
43
+ ExceptionFormatter.format(exception, pattern: "%b").should == "file:42\nfile:76"
44
+ end
45
+ end
46
+
47
+ context "pattern %c" do
48
+ context "exception respond to cause" do
49
+ it "returns message with exception cause" do
50
+ exception = new_exception(cause: new_exception(message: "cause error"))
51
+ ExceptionFormatter.format(exception, pattern: "%c").should == "cause error"
52
+ end
53
+ end
54
+
55
+ context "exception does respond to cause but not to cause.message" do
56
+ it "returns message without exception cause" do
57
+ exception = new_exception(cause: double("object_without_message"))
58
+ ExceptionFormatter.format(exception, pattern: "%c").should == ""
59
+ end
60
+ end
61
+
62
+ context "exception does not respond to cause" do
63
+ it "returns message without exception cause" do
64
+ exception = new_exception
65
+ ExceptionFormatter.format(exception, pattern: "%c").should == ""
66
+ end
67
+ end
68
+ end
69
+
70
+ context "pattern %c{method}" do
71
+ it "returns message with exception message return by method" do
72
+ exception = new_exception(reason: new_exception(message: "error reason"))
73
+ ExceptionFormatter.format(exception, pattern: "%c{reason}").should == "error reason"
74
+ end
75
+ end
76
+
77
+ context "%r" do
78
+ context "ActiveRecord:::RecordInvalid exception" do
79
+ it "returns message with record error messages" do
80
+ record = double('record', errors: double("ActiveModel::Errors", full_messages: ["Name must be present", "Url is invalid"]))
81
+ exception = double('exception', message: "ActiveRecord::RecordInvalid", backtrace: ["file:1", "file:2"], record: record)
82
+ ExceptionFormatter.format(exception, pattern: "%r").should == '["Name must be present", "Url is invalid"]'
83
+ ExceptionFormatter.format(exception, pattern: "%m: %r\n%b").
84
+ should == %Q{ActiveRecord::RecordInvalid: ["Name must be present", "Url is invalid"]\nfile:1\nfile:2}
85
+ end
86
+ end
87
+ end
88
+
89
+ context "pattern %x{method}" do
90
+ context "exception respond to method" do
91
+ it "returns message with exception.send(mathod)" do
92
+ exception = new_exception(awesome_message: "this is a awesome message")
93
+ ExceptionFormatter.format(exception, pattern: "%x{awesome_message}").should == "this is a awesome message"
94
+ end
95
+ end
96
+
97
+ context "exception does not respond to method" do
98
+ it "returns blank string" do
99
+ exception = new_exception
100
+ ExceptionFormatter.format(exception, pattern: "%x{awesome_message}").should == ""
101
+ end
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ def new_exception(options = {})
108
+ double('exception', { message: "A Error Message", backtrace: ["line1", "line2"] }.merge(options))
109
+ end
110
+
111
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exception_formatter
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Baustert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ type: :development
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ none: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ none: false
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ type: :development
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ none: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ none: false
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ type: :development
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ none: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ none: false
61
+ prerelease: false
62
+ description: Simple formatter for ruby exception messages.
63
+ email:
64
+ - business@thomasbaustert.de
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - Capfile
72
+ - Gemfile
73
+ - LICENSE
74
+ - README.md
75
+ - Rakefile
76
+ - exception_formatter.gemspec
77
+ - lib/exception_formatter.rb
78
+ - lib/exception_formatter/version.rb
79
+ - spec/exception_formatter_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/thomasbaustert/exception_formatter
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ hash: 3825082566233634308
95
+ version: '0'
96
+ none: false
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
103
+ hash: 3825082566233634308
104
+ version: '0'
105
+ none: false
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.25
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Simple formatter for ruby exception messages.
112
+ test_files:
113
+ - spec/exception_formatter_spec.rb
114
+ - spec/spec_helper.rb