misfit 0.0.1 → 0.0.2
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/CHANGELOG +5 -1
- data/MIT-LICENSE +1 -1
- data/README.md +26 -3
- data/Rakefile +11 -2
- data/lib/misfit.rb +18 -13
- data/lib/misfit/version.rb +2 -2
- data/spec/misfit_spec.rb +33 -17
- metadata +10 -10
data/CHANGELOG
CHANGED
data/MIT-LICENSE
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Copyright 2012 Ian White
|
2
2
|
|
3
3
|
This plugin was developed by Ian White (http://github.com/ianwhite)
|
4
|
-
while working at Distinctive Doors (http://distinctivedoors.co.uk)
|
4
|
+
while working at Distinctive Doors (http://distinctivedoors.co.uk/oss)
|
5
5
|
who have kindly agreed to release this under the MIT-LICENSE.
|
6
6
|
|
7
7
|
Permission is hereby granted, free of charge, to any person obtaining
|
data/README.md
CHANGED
@@ -1,14 +1,21 @@
|
|
1
|
-
misfit
|
1
|
+
misfit [](http://travis-ci.org/i2w/misfit)
|
2
2
|
======
|
3
3
|
|
4
4
|
Flexible approach to handling exceptions in ruby (for library writers, or consumers). Inspired by [Avdi Grim](http://avdi.org/)'s excellent book [Exceptional Ruby](http://exceptionalruby.com/).
|
5
5
|
|
6
|
-
This was developed by [Ian White](http://github.com/ianwhite) while working at [Distinctive Doors](http://distinctivedoors.co.uk) who have kindly agreed to release this under the MIT-LICENSE.
|
6
|
+
This was developed by [Ian White](http://github.com/ianwhite) while working at [Distinctive Doors](http://distinctivedoors.co.uk/oss) who have kindly agreed to release this under the MIT-LICENSE.
|
7
7
|
|
8
8
|
Misfit allows any module to act like a ruby exception class and gives the ability to decorate any standard ruby exception, optionally adding data.
|
9
9
|
|
10
10
|
Misfit also provides a mechanism to change the error handling policy from raising to something else
|
11
11
|
|
12
|
+
Installation
|
13
|
+
------------
|
14
|
+
|
15
|
+
In your Gemfile:
|
16
|
+
|
17
|
+
gem 'misfit'
|
18
|
+
|
12
19
|
Why?
|
13
20
|
----
|
14
21
|
|
@@ -94,6 +101,17 @@ You can also optionally wrap and add data to any Error:
|
|
94
101
|
|
95
102
|
`YourLib::Error.wrap &block` causes all raised exceptions to be wrapped as `YourLib::Error`
|
96
103
|
|
104
|
+
Development
|
105
|
+
-----------
|
106
|
+
|
107
|
+
To run the specs, you can start from the last known good set of gem dependencies in Gemfile.lock.development:
|
108
|
+
|
109
|
+
git clone http://github.com/i2w/misfit
|
110
|
+
cd misfit
|
111
|
+
cp Gemfile.lock.development Gemfile.lock
|
112
|
+
bundle
|
113
|
+
bundle exec rake spec
|
114
|
+
|
97
115
|
Other Examples of use:
|
98
116
|
----------------------
|
99
117
|
|
@@ -127,4 +145,9 @@ Other Examples of use:
|
|
127
145
|
end
|
128
146
|
|
129
147
|
# the resulting error will be an IOError, extended with MyIOError, and MyError, and will be rescued as such
|
130
|
-
raise MyIOError
|
148
|
+
raise MyIOError
|
149
|
+
|
150
|
+
License
|
151
|
+
-------
|
152
|
+
|
153
|
+
This project uses the MIT-LICENSE.
|
data/Rakefile
CHANGED
@@ -20,8 +20,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
20
20
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
21
|
end
|
22
22
|
|
23
|
-
Bundler::GemHelper.install_tasks
|
24
|
-
|
25
23
|
require 'rspec/core/rake_task'
|
26
24
|
|
27
25
|
RSpec::Core::RakeTask.new(:spec)
|
@@ -31,3 +29,14 @@ task :simplecov => [:simplecov_env, :spec]
|
|
31
29
|
task :simplecov_env do ENV['SIMPLECOV'] = '1' end
|
32
30
|
|
33
31
|
task :default => :spec
|
32
|
+
|
33
|
+
Bundler::GemHelper.install_tasks
|
34
|
+
|
35
|
+
task :release => :check_gemfile
|
36
|
+
|
37
|
+
task :check_gemfile do
|
38
|
+
if File.exists?("Gemfile.lock") && File.read("Gemfile.lock") != File.read("Gemfile.lock.development")
|
39
|
+
cp "Gemfile.lock", "Gemfile.lock.development"
|
40
|
+
raise "** Gemfile.lock.development has been updated, please commit these changes."
|
41
|
+
end
|
42
|
+
end
|
data/lib/misfit.rb
CHANGED
@@ -3,15 +3,15 @@ require 'active_support/core_ext'
|
|
3
3
|
|
4
4
|
module Misfit
|
5
5
|
extend ActiveSupport::Concern
|
6
|
-
|
6
|
+
|
7
7
|
included do inherit_from_misfit end
|
8
|
-
|
8
|
+
|
9
9
|
def self.exception_class
|
10
10
|
StandardError
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
attr_accessor :data
|
14
|
-
|
14
|
+
|
15
15
|
module ClassMethods
|
16
16
|
# when called with an exception, makes the exception a <self>
|
17
17
|
# when called with a block, any exceptions raised in the block will be wrapped as <self>
|
@@ -22,20 +22,20 @@ module Misfit
|
|
22
22
|
wrap_exception *args
|
23
23
|
end
|
24
24
|
end
|
25
|
-
|
26
|
-
def new message, data = nil, *backtrace
|
25
|
+
|
26
|
+
def new message = nil, data = nil, *backtrace
|
27
27
|
wrap_exception exception_class.exception(message, *backtrace), data
|
28
28
|
end
|
29
|
-
|
30
|
-
def exception message, *args
|
29
|
+
|
30
|
+
def exception message = nil, *args
|
31
31
|
new message, nil, *args
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def exception_class klass = nil
|
35
35
|
@exception_class = klass if klass
|
36
36
|
@exception_class
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
private
|
40
40
|
def wrap_exception exception, data = nil
|
41
41
|
exception.tap do |e|
|
@@ -43,13 +43,13 @@ module Misfit
|
|
43
43
|
e.data = data if data
|
44
44
|
end
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
def wrap_block
|
48
48
|
yield
|
49
49
|
rescue Exception => exception
|
50
50
|
raise wrap_exception(exception)
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
# when an exception module is included, set up this module
|
54
54
|
# as an exception module itself, and 'inherit' the exception_class
|
55
55
|
def inherit_from_misfit
|
@@ -58,4 +58,9 @@ module Misfit
|
|
58
58
|
included do inherit_from_misfit end
|
59
59
|
end
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
|
+
# this is here solely because of the way rspec instantiates exception objects in stubs
|
63
|
+
def initialize *args
|
64
|
+
super
|
65
|
+
end
|
66
|
+
end
|
data/lib/misfit/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module Misfit
|
2
|
-
VERSION = '0.0.
|
3
|
-
end
|
2
|
+
VERSION = '0.0.2'
|
3
|
+
end
|
data/spec/misfit_spec.rb
CHANGED
@@ -9,21 +9,21 @@ module MisfitSpec
|
|
9
9
|
module Error
|
10
10
|
include Misfit
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
module FooError
|
14
14
|
include Error
|
15
15
|
exception_class RuntimeError
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
module FazError
|
19
19
|
include FooError
|
20
20
|
exception_class IOError
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
module BarError
|
24
24
|
include FazError
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
describe Misfit do
|
28
28
|
describe 'Error (includes Misfit)' do
|
29
29
|
subject { exception_module }
|
@@ -33,27 +33,35 @@ module MisfitSpec
|
|
33
33
|
let(:data) { 'exception data' }
|
34
34
|
|
35
35
|
describe 'standard idioms' do
|
36
|
+
it 'raise <exception_module> should raise error with no message' do
|
37
|
+
expect { raise exception_module }.to raise_error(exception_module)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'exception_module.exception should return error with no message' do
|
41
|
+
exception_module.exception.should be_a(exception_module)
|
42
|
+
end
|
43
|
+
|
36
44
|
describe 'raise <exception_module>, <message>' do
|
37
45
|
subject { raise exception_module, message }
|
38
46
|
|
39
47
|
it { expect{ subject }.to raise_error(StandardError, message) }
|
40
48
|
it { expect{ subject }.to raise_error(exception_module, message) }
|
41
49
|
end
|
42
|
-
|
50
|
+
|
43
51
|
describe 'raise <exception_module>.new(<message>)' do
|
44
52
|
subject { raise exception_module.new(message) }
|
45
53
|
|
46
54
|
it { expect{ subject }.to raise_error(StandardError, message) }
|
47
55
|
it { expect{ subject }.to raise_error(exception_module, message) }
|
48
56
|
end
|
49
|
-
|
57
|
+
|
50
58
|
describe 'raise <exception_module>.exception(<message>)' do
|
51
59
|
subject { raise exception_module.exception(message) }
|
52
60
|
|
53
61
|
it { expect{ subject }.to raise_error(StandardError, message) }
|
54
62
|
it { expect{ subject }.to raise_error(exception_module, message) }
|
55
63
|
end
|
56
|
-
|
64
|
+
|
57
65
|
describe 'FooError (includes Error, exception_class RuntimeError)' do
|
58
66
|
let(:exception_module) { FooError }
|
59
67
|
|
@@ -65,7 +73,7 @@ module MisfitSpec
|
|
65
73
|
it { expect{ subject }.to raise_error(RuntimeError, message) }
|
66
74
|
end
|
67
75
|
end
|
68
|
-
|
76
|
+
|
69
77
|
describe 'FazError (includes FooError, exception_class IOError)' do
|
70
78
|
let(:exception_module) { FazError }
|
71
79
|
|
@@ -79,7 +87,7 @@ module MisfitSpec
|
|
79
87
|
it { expect{ subject }.to raise_error(IOError, message) }
|
80
88
|
end
|
81
89
|
end
|
82
|
-
|
90
|
+
|
83
91
|
describe 'BarError (includes FazError)' do
|
84
92
|
let(:exception_module) { BarError }
|
85
93
|
|
@@ -93,12 +101,20 @@ module MisfitSpec
|
|
93
101
|
it { expect{ subject }.to raise_error(IOError, message) }
|
94
102
|
end
|
95
103
|
end
|
104
|
+
|
105
|
+
describe "used in specs" do
|
106
|
+
it "should work with stubs" do
|
107
|
+
object = Object.new
|
108
|
+
object.stub(:foo).and_raise(Error)
|
109
|
+
expect { object.foo }.to raise_error(Error)
|
110
|
+
end
|
111
|
+
end
|
96
112
|
end
|
97
|
-
|
113
|
+
|
98
114
|
describe 'adding data' do
|
99
115
|
describe 'raise <exception_module>.new <message>, <data>' do
|
100
116
|
subject { raise exception_module.new(message, data) }
|
101
|
-
|
117
|
+
|
102
118
|
it "should have data added to the exception" do
|
103
119
|
begin
|
104
120
|
subject
|
@@ -108,28 +124,28 @@ module MisfitSpec
|
|
108
124
|
end
|
109
125
|
end
|
110
126
|
end
|
111
|
-
|
127
|
+
|
112
128
|
describe '#wrap' do
|
113
129
|
describe 'RuntimeError.new(<message>)' do
|
114
130
|
subject { exception_module.wrap RuntimeError.new(message) }
|
115
|
-
|
131
|
+
|
116
132
|
it { should be_a RuntimeError }
|
117
133
|
it { should be_a exception_module }
|
118
134
|
its(:message) { should == message }
|
119
135
|
end
|
120
|
-
|
136
|
+
|
121
137
|
describe 'RuntimeError.new(<message>), <exception data>' do
|
122
138
|
subject { exception_module.wrap RuntimeError.new(message), data }
|
123
|
-
|
139
|
+
|
124
140
|
it { should be_a RuntimeError }
|
125
141
|
it { should be_a exception_module }
|
126
142
|
its(:message) { should == message }
|
127
143
|
its(:data) { should == data }
|
128
144
|
end
|
129
|
-
|
145
|
+
|
130
146
|
describe '{ some_error_ridden_code! }' do
|
131
147
|
subject { exception_module.wrap do some_error_ridden_code! end }
|
132
|
-
|
148
|
+
|
133
149
|
it { expect{ subject }.to raise_error(NoMethodError, /some_error_ridden_code!/) }
|
134
150
|
it { expect{ subject }.to raise_error(exception_module, /some_error_ridden_code!/) }
|
135
151
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: misfit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70208106687180 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70208106687180
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70208106655660 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70208106655660
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70208106654580 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '2'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70208106654580
|
47
47
|
description: Flexible approach to handling exceptions in ruby (for library writers,
|
48
48
|
or consumers). Ispired by Avdi Grimm's excellent book 'Exceptional Ruby'.
|
49
49
|
email:
|
@@ -74,7 +74,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
74
|
version: '0'
|
75
75
|
segments:
|
76
76
|
- 0
|
77
|
-
hash:
|
77
|
+
hash: 2688544848088413638
|
78
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
79
|
none: false
|
80
80
|
requirements:
|
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
83
|
version: '0'
|
84
84
|
segments:
|
85
85
|
- 0
|
86
|
-
hash:
|
86
|
+
hash: 2688544848088413638
|
87
87
|
requirements: []
|
88
88
|
rubyforge_project:
|
89
89
|
rubygems_version: 1.8.15
|