nesty 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 88bbd71a1ad275e4b375f83e911a65c82c415bca
4
+ data.tar.gz: 27de613cc1345753cb0373e6f680c5a663b311ab
5
+ SHA512:
6
+ metadata.gz: be263019851c80453bc3b2a6432165835e07d78f1ad03c251b99e6b8448509d6276db1d9290fa8dd371bcc4208b94ecebdd54ca91f3c3939b72e7f07ed5a23fa
7
+ data.tar.gz: 8b290ce0bd38b5e1d32232373f204708c1f4528a389090234bbe970810179f0eec406691db98ac0e400ef38e4fc20dc4c060693f19af2dd5cb62822fea9f6b4d
@@ -0,0 +1,18 @@
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
+ tags
data/.irbrc ADDED
@@ -0,0 +1,3 @@
1
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
2
+
3
+ require './lib/nesty'
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format doc
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ - "2.0.0"
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nesty.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alan Skorkin
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.
@@ -0,0 +1,112 @@
1
+ # Nesty
2
+
3
+ Now, when you rescue an error and then re-raise your own, you don't have to lose track of what actually occured, you can keep/nest the old error in your own and the stacktrace will reflect the cause of the original error.
4
+
5
+ ## Why Use It?
6
+
7
+ When you use libraries that raise their own errors, it's not a good idea to allow these errors to bubble up through your app/library. Clients of your app/library should only have to deal with errors that belong to your app and not ones that come from libraries that your app is using. To achieve this without nested exception support, you would need to rescue the error that come from external libraries and then raise your own errors in their place. Of course, when you do this you lose the information from the original error. With nested exception support you no longer have to lose this information which is very handy.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'nesty'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install nesty
22
+
23
+ ## Usage
24
+
25
+ Super simple, create your own error class just like normal, but include a module in it:
26
+
27
+ ```ruby
28
+ class HappyError < StandardError
29
+ include Nesty::NestedError
30
+ end
31
+ ```
32
+
33
+ Alternatively rather than inheriting from `StandardError` do this:
34
+
35
+ ```ruby
36
+ class HappyError < Nesty::NestedStandardError
37
+ end
38
+ ```
39
+
40
+ Instances of `HappyError`, will now support nesting another error inside.
41
+
42
+ Here is how we can use this.
43
+
44
+ ```ruby
45
+ begin
46
+ raise StandardError.new
47
+ rescue => e
48
+ raise HappyError.new("hello", e)
49
+ end
50
+ ```
51
+
52
+ That was the explicit way, you raise another error and pass in the error you rescued, but you can also do this:
53
+
54
+ ```ruby
55
+ begin
56
+ raise StandardError.new
57
+ rescue => e
58
+ raise HappyError.new
59
+ end
60
+ ```
61
+
62
+ This is the implicit way, the `HappyError` instance will still nest the `StandardError` that was rescued.
63
+
64
+ You can of course go deeper and keep rescuing and raising your own errors. As long as you raise with instances that support nesting (e.g. ones that include `Nesty::NestedError` or inherit from `Nesty::NestedStandardError`), the stack trace will include all the nested exception messages.
65
+
66
+ ### What The Stacktrace Will Look Like?
67
+
68
+ It's probably a good idea to keep the stacktrace as close to a normal one as possible and in this case it actually will look very similar to a normal stacktrace. The only difference is that the error messages for all the nested errors will be included in the stacktrace (as opposed to just the message for the outer error).
69
+
70
+ Let's illustrate with an example.
71
+
72
+ We have 3 errors, A, B and C all nested in each other (A is nested in B and B is nested in C). They have the following messages and backtrace arrays:
73
+
74
+ ```
75
+ A - message: 'a', backtrace: ['2', '1']
76
+ B - message: 'b', backtrace: ['4', '3', '2', '1']
77
+ C - message: 'c', backtrace: ['6', '5', '4', '3', '2', '1']
78
+ ```
79
+
80
+ If C was not nested and we allowed it to bubble up so that it gets dumped to standard output, we would see something like the following:
81
+
82
+ ```
83
+ c
84
+ 6
85
+ 5
86
+ 4
87
+ 3
88
+ 2
89
+ 1
90
+ ```
91
+
92
+ But, with out nested errors we would see the following:
93
+
94
+ ```
95
+ c
96
+ 6
97
+ 5
98
+ 4: b
99
+ 3
100
+ 2: a
101
+ 1
102
+ ```
103
+
104
+ Since a stacktrace for a nested error is always a subset of the stacktrace of the enclosing error, all we need do is add the messages for each of our nested errors in the appropriate place in the stacktrace. Simple, but handy.
105
+
106
+ ## Contributing
107
+
108
+ 1. Fork it
109
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
110
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
111
+ 4. Push to the branch (`git push origin my-new-feature`)
112
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.join(File.expand_path(__FILE__), "..", "..", "lib", "nesty"))
4
+
5
+ class MyError < StandardError
6
+ include Nesty::NestedError
7
+ end
8
+
9
+ #just run this to see what get spit out to the console
10
+
11
+ begin
12
+ 1/0
13
+ rescue => e
14
+ begin
15
+ raise MyError.new("Number errors will be caught", e)
16
+ rescue => e
17
+ begin
18
+ raise MyError.new("Don't need to let MyError bubble up")
19
+ rescue => e
20
+ raise MyError.new("Last one for sure!")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.join(File.expand_path(__FILE__), "..", "..", "lib", "nesty"))
4
+
5
+ class MyError < StandardError
6
+ include Nesty::NestedError
7
+ end
8
+
9
+ #just run this to see what get spit out to the console
10
+
11
+ raise MyError.new("Gotta catch 'em all!")
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.join(File.expand_path(__FILE__), "..", "..", "lib", "nesty"))
4
+
5
+ class MyError < StandardError
6
+ include Nesty::NestedError
7
+ end
8
+
9
+ #just run this to see what get spit out to the console
10
+
11
+ begin
12
+ 1/0
13
+ rescue => e
14
+ raise MyError.new("Gotta catch 'em all!")
15
+ end
@@ -0,0 +1,4 @@
1
+ require "nesty/version"
2
+ require "nesty/nested_error"
3
+ require "nesty/nested_standard_error"
4
+
@@ -0,0 +1,26 @@
1
+ module Nesty
2
+ module NestedError
3
+ attr_reader :nested, :raw_backtrace
4
+
5
+ def initialize(msg = nil, nested = $!)
6
+ super(msg)
7
+ @nested = nested
8
+ end
9
+
10
+ def set_backtrace(backtrace)
11
+ @raw_backtrace = backtrace
12
+ if nested
13
+ backtrace = backtrace - nested_raw_backtrace
14
+ backtrace += ["#{nested.backtrace.first}: #{nested.message}"]
15
+ backtrace += nested.backtrace[1..-1] || []
16
+ end
17
+ super(backtrace)
18
+ end
19
+
20
+ private
21
+
22
+ def nested_raw_backtrace
23
+ nested.respond_to?(:raw_backtrace) ? nested.raw_backtrace : nested.backtrace
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Nesty
2
+ class NestedStandardError
3
+ include NestedError
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Nesty
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nesty/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nesty"
8
+ spec.version = Nesty::VERSION
9
+ spec.authors = ["Alan Skorkin"]
10
+ spec.email = ["alan@skorks.com"]
11
+ spec.summary = %q{Nested exception support for Ruby}
12
+ spec.description = %q{Nested exception support for Ruby}
13
+ spec.homepage = "https://github.com/skorks/nesty"
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
+ spec.add_development_dependency 'travis-lint'
25
+ end
@@ -0,0 +1,122 @@
1
+ describe Nesty::NestedError do
2
+ class TestError < StandardError
3
+ include Nesty::NestedError
4
+ end
5
+
6
+ # test when no nested error and message is nil
7
+ # test when no nested error and message is not nil
8
+ # test when one level of nesting and nested error is explicitly set
9
+ # test when one level of nesting and nested error is implicit
10
+ # test when two levels of nesting and nested error are explicitly set
11
+ # test when two levels of nesting and all errors are implicit
12
+
13
+ let(:outer_message) {'hello'}
14
+ let(:outer_backtrace) {['x', 'a', 'b']}
15
+ let(:error) do
16
+ begin
17
+ raise TestError.new(outer_message, nested)
18
+ rescue => e
19
+ e.set_backtrace(outer_backtrace)
20
+ e
21
+ end
22
+ end
23
+
24
+ describe "nested error" do
25
+ subject { error }
26
+
27
+ context "when there is no nested error" do
28
+ let(:nested) {nil}
29
+ let(:outer_backtrace) {['a', 'b']}
30
+
31
+ context "when message is nil" do
32
+ let(:outer_message) {nil}
33
+ it {subject.message.should == TestError.name}
34
+ it {subject.backtrace.size.should == 2}
35
+ it {subject.backtrace[0].should == 'a'}
36
+ end
37
+
38
+ context "when message is not nil" do
39
+ let(:outer_message) {'hello'}
40
+ it {subject.message.should == 'hello'}
41
+ it {subject.backtrace.size.should == 2}
42
+ it {subject.backtrace[1].should == 'b'}
43
+ end
44
+ end
45
+
46
+ context "when there is one level of nested error" do
47
+ let(:nested_message) {'foo'}
48
+ let(:nested_backtrace) {['a', 'b']}
49
+ let(:nested) do
50
+ begin
51
+ raise StandardError.new(nested_message)
52
+ rescue => e
53
+ e.set_backtrace(nested_backtrace)
54
+ e
55
+ end
56
+ end
57
+
58
+ context "and nested error is explicitly set" do
59
+ it {subject.message.should == 'hello'}
60
+ it {subject.backtrace.size.should == 3}
61
+ it {subject.backtrace[0].should == 'x'}
62
+ it {subject.backtrace[1].should == "a: #{nested_message}"}
63
+ it {subject.backtrace[2].should == 'b'}
64
+ end
65
+
66
+ context "and nested error is implicitly set" do
67
+ let(:error) do
68
+ begin
69
+ begin
70
+ raise nested
71
+ rescue => e
72
+ error = TestError.new(outer_message)
73
+ error.set_backtrace(outer_backtrace)
74
+ raise error
75
+ end
76
+ rescue => ae
77
+ ae
78
+ end
79
+ end
80
+
81
+ it {subject.message.should == 'hello'}
82
+ it {subject.backtrace.size.should == 3}
83
+ it {subject.backtrace[0].should == 'x'}
84
+ it {subject.backtrace[1].should == "a: #{nested_message}"}
85
+ it {subject.backtrace[2].should == 'b'}
86
+ end
87
+ end
88
+
89
+ context "when there are multiple levels of nested error" do
90
+ let(:outer_message) {'hello'}
91
+ let(:outer_backtrace) {['w', 'x', 'y', 'z', 'a', 'b']}
92
+ let(:nested_message) {'foo'}
93
+ let(:nested_backtrace) {['y', 'z', 'a', 'b']}
94
+ let(:nested_nested_message) {'bar'}
95
+ let(:nested_nested_backtrace) {['a', 'b']}
96
+ let(:nested) do
97
+ begin
98
+ raise TestError.new(nested_message, nested_nested)
99
+ rescue => e
100
+ e.set_backtrace(nested_backtrace)
101
+ e
102
+ end
103
+ end
104
+ let(:nested_nested) do
105
+ begin
106
+ raise StandardError.new(nested_nested_message)
107
+ rescue => e
108
+ e.set_backtrace(nested_nested_backtrace)
109
+ e
110
+ end
111
+ end
112
+ it {subject.message.should == 'hello'}
113
+ it {subject.backtrace.size.should == 6}
114
+ it {subject.backtrace[0].should == 'w'}
115
+ it {subject.backtrace[1].should == 'x'}
116
+ it {subject.backtrace[2].should == "y: #{nested_message}"}
117
+ it {subject.backtrace[3].should == 'z'}
118
+ it {subject.backtrace[4].should == "a: #{nested_nested_message}"}
119
+ it {subject.backtrace[5].should == 'b'}
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,21 @@
1
+ require 'nesty'
2
+
3
+ Dir[File.join(File.dirname(__FILE__), 'support', '**', "*.rb").to_s].each {|file| require file }
4
+
5
+ # This file was generated by the `rspec --init` command. Conventionally, all
6
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
7
+ # Require this file using `require "spec_helper"` to ensure that it is only
8
+ # loaded once.
9
+ #
10
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nesty
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alan Skorkin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: travis-lint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Nested exception support for Ruby
70
+ email:
71
+ - alan@skorks.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .irbrc
78
+ - .rspec
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - examples/complex.rb
85
+ - examples/no_nested.rb
86
+ - examples/simple.rb
87
+ - lib/nesty.rb
88
+ - lib/nesty/nested_error.rb
89
+ - lib/nesty/nested_standard_error.rb
90
+ - lib/nesty/version.rb
91
+ - nesty.gemspec
92
+ - spec/lib/nesty/nested_error_spec.rb
93
+ - spec/spec_helper.rb
94
+ homepage: https://github.com/skorks/nesty
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.0
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Nested exception support for Ruby
118
+ test_files:
119
+ - spec/lib/nesty/nested_error_spec.rb
120
+ - spec/spec_helper.rb