try_to 1.2 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +104 -72
- data/lib/try_to.rb +3 -3
- data/lib/try_to/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ec1f34ec687f3663ab5210dac26e30f237399f274e1e449c4da8241bbafcf36
|
4
|
+
data.tar.gz: 9ac967b5be81ccaacf7736539d409c93b506157dc957016c4cd1426d4d445ee6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8293b132f2a40f59c3d636a33c84025f7f177a25227c68602bfb300b69a2c66f3728aa9d7ac628d3e415edbf838e62048635e5d8dd72f83c44a8e3ba02005eba
|
7
|
+
data.tar.gz: 6d3516a4dbeb51cf8261bc611a8f347985bdcae00ae912c39a29b71c24de8a3eef519a411ab7af41fc2a386e35252da579a9c4545bf36d423334bd3d9827cf40
|
data/README.md
CHANGED
@@ -12,33 +12,49 @@ but allows for much more sophisticated error handling.
|
|
12
12
|
|
13
13
|
Instead of using Rails' `Object#try` like this,
|
14
14
|
|
15
|
-
|
15
|
+
```ruby
|
16
|
+
obj.try(:met1).try(:met2).try(:met3).to_s
|
17
|
+
```
|
16
18
|
|
17
19
|
you can do this:
|
18
20
|
|
19
|
-
|
21
|
+
```ruby
|
22
|
+
try_to { obj.met1.met2.met3.to_s }
|
23
|
+
```
|
20
24
|
|
21
25
|
### Exceptions
|
22
26
|
|
27
|
+
#### Adding exceptions
|
28
|
+
|
23
29
|
It's possible to customize which exceptions to handle with `add_exception`:
|
24
30
|
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
```ruby
|
32
|
+
TryTo.add_exception(ZeroDivisionError)
|
33
|
+
#=> [NoMethodError, ZeroDivisionError]
|
34
|
+
try_to { 1/0 } # will not raise an exception
|
35
|
+
```
|
36
|
+
|
37
|
+
#### Removing exceptions
|
28
38
|
|
29
39
|
To remove an exception, use `remove_exception!`:
|
30
40
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
41
|
+
```ruby
|
42
|
+
TryTo.exceptions
|
43
|
+
#=> [NoMethodError, ZeroDivisionError]
|
44
|
+
TryTo.remove_exception!(ZeroDivisionError)
|
45
|
+
#=> [NoMethodError]
|
46
|
+
```
|
47
|
+
|
48
|
+
#### Resetting exceptions
|
35
49
|
|
36
50
|
You can also use `reset_exceptions!` to go back to only handle `NoMethodError`s.
|
37
51
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
52
|
+
```ruby
|
53
|
+
TryTo.exceptions
|
54
|
+
#=> [NoMethodError, RuntimeError, ZeroDivisionError]
|
55
|
+
TryTo.reset_exceptions!
|
56
|
+
#=> [NoMethodError]
|
57
|
+
```
|
42
58
|
|
43
59
|
### Handlers
|
44
60
|
|
@@ -47,87 +63,103 @@ ways in which you can customize this behavior. All handlers can either be simple
|
|
47
63
|
values or an object responding to `#call`, which should take one argument, the
|
48
64
|
exception object.
|
49
65
|
|
50
|
-
Specifying a handler
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
66
|
+
#### Specifying a handler inline:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
try_to(-> e { puts e.class }) { 1.foo }
|
70
|
+
# prints "NoMethodError"
|
71
|
+
# or provide a simple value:
|
72
|
+
try_to(42) { 1.foo }
|
73
|
+
#=> 42
|
74
|
+
```
|
75
|
+
|
76
|
+
#### Registering handlers for exception classes:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
TryTo.handlers
|
80
|
+
#=> {}
|
81
|
+
TryTo.add_handler(ZeroDivisionError, -> _ { puts "Ouch" })
|
82
|
+
try_to { 1/0 }
|
83
|
+
# prints "Ouch"
|
84
|
+
TryTo.add_handler(NoMethodError, -> _ { 23 })
|
85
|
+
try_to { 1.foo }
|
86
|
+
#=> 23
|
87
|
+
# or simply
|
88
|
+
TryTo.add_handler(NoMethodError, 42)
|
89
|
+
try_to { 1.foo } #=> 42
|
90
|
+
```
|
91
|
+
|
92
|
+
#### Removing handlers:
|
74
93
|
|
75
94
|
TryTo.handlers
|
76
95
|
#=> {ZeroDivisionError=>#<Proc:0x0000000108921d60@(irb):1 (lambda)>}
|
77
96
|
TryTo.remove_handler!(ZeroDivisionError)
|
78
97
|
#=> {}
|
79
98
|
|
80
|
-
|
81
|
-
listed in `TryTo.exceptions`.
|
82
|
-
|
83
|
-
TryTo.default_handler = 42
|
84
|
-
try_to { 1.foo } #=> 42
|
85
|
-
# or
|
86
|
-
TryTo.default_handler = lambda { |_| puts "Something went wrong!" }
|
87
|
-
try_to { 1.foo }
|
88
|
-
# Outputs: Something went wrong!
|
99
|
+
#### Default handler
|
89
100
|
|
90
|
-
|
91
|
-
|
92
|
-
# default behavior
|
93
|
-
try_to #=> nil
|
94
|
-
try_to {} #=> nil
|
95
|
-
class Foo ; end
|
96
|
-
try_to { Foo.new.foo } #=> nil
|
101
|
+
This will be called for all the exceptions listed in `TryTo.exceptions`.
|
97
102
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
103
|
+
```ruby
|
104
|
+
TryTo.default_handler = 42
|
105
|
+
try_to { 1.foo } #=> 42
|
106
|
+
# or
|
107
|
+
TryTo.default_handler = -> _e { puts "Something went wrong!" }
|
108
|
+
try_to { 1.foo }
|
109
|
+
# Outputs: Something went wrong!
|
110
|
+
```
|
104
111
|
|
105
|
-
|
106
|
-
TryTo.default_handler = -> e { puts e.class }
|
107
|
-
try_to { 1 / 0 } # prints "ZeroDivisionError"
|
108
|
-
try_to { Foo.new.foo } # prints "ZeroDivisionError"
|
112
|
+
#### Example
|
109
113
|
|
110
|
-
|
111
|
-
TryTo.add_handler(ZeroDivisionError, -> _ { puts "You shouldn't divide by 0!"})
|
112
|
-
try_to { 1 / 0 } # prints: "You shouldn't divide by 0!"
|
113
|
-
try_to { Foo.new.foo} # still prints "NoMethodError"
|
114
|
+
Here's a complete example in the form of an IRB transcript:
|
114
115
|
|
115
|
-
|
116
|
-
|
116
|
+
```ruby
|
117
|
+
# default behavior
|
118
|
+
try_to #=> nil
|
119
|
+
try_to {} #=> nil
|
120
|
+
Foo = Class.new
|
121
|
+
try_to { Foo.new.foo } #=> nil (instead of NoMethodError)
|
122
|
+
|
123
|
+
# this will raise an exception
|
124
|
+
try_to { 1 / 0 }
|
125
|
+
ZeroDivisionError: divided by 0
|
126
|
+
# let's fix that
|
127
|
+
TryTo.exceptions << ZeroDivisionError #=> [NoMethodError, ZeroDivisionError]
|
128
|
+
try_to { 1 / 0 } #=> nil
|
129
|
+
|
130
|
+
# change the default handler
|
131
|
+
TryTo.default_handler = -> e { puts e.class }
|
132
|
+
try_to { 1 / 0 } # prints "ZeroDivisionError"
|
133
|
+
try_to { Foo.new.foo } # prints "ZeroDivisionError"
|
134
|
+
|
135
|
+
# new behavior for ZeroDivisionError
|
136
|
+
TryTo.add_handler(ZeroDivisionError, -> _ { puts "You shouldn't divide by 0!"})
|
137
|
+
try_to { 1 / 0 } # prints: "You shouldn't divide by 0!"
|
138
|
+
try_to { Foo.new.foo} # still prints "NoMethodError"
|
139
|
+
|
140
|
+
# change handler at call site
|
141
|
+
try_to(-> _ {puts "Ouch!"}) { Foo.new.foo } # prints "Ouch!"
|
142
|
+
```
|
117
143
|
|
118
144
|
## Installation
|
119
145
|
|
120
146
|
Add this line to your application's Gemfile:
|
121
147
|
|
122
|
-
|
148
|
+
```ruby
|
149
|
+
gem 'try_to'
|
150
|
+
```
|
123
151
|
|
124
152
|
And then execute:
|
125
153
|
|
126
|
-
|
154
|
+
```shell
|
155
|
+
$ bundle
|
156
|
+
```
|
127
157
|
|
128
158
|
Or install it yourself:
|
129
159
|
|
130
|
-
|
160
|
+
```shell
|
161
|
+
$ gem install try_to
|
162
|
+
```
|
131
163
|
|
132
164
|
## Authors
|
133
165
|
|
data/lib/try_to.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module TryTo
|
2
4
|
@handlers = {}
|
3
5
|
@exceptions = [NoMethodError]
|
@@ -34,9 +36,7 @@ module Kernel
|
|
34
36
|
private def try_to(handler = nil)
|
35
37
|
yield if block_given?
|
36
38
|
rescue *(TryTo.exceptions | TryTo.handlers.keys) => e
|
37
|
-
handler = [
|
38
|
-
TryTo.handlers[e.class],
|
39
|
-
TryTo.default_handler].compact.first
|
39
|
+
handler = handler || TryTo.handlers[e.class] || TryTo.default_handler
|
40
40
|
handler.respond_to?(:call) ? handler.call(e) : handler
|
41
41
|
end
|
42
42
|
end
|
data/lib/try_to/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: try_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Kohl
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,8 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
- !ruby/object:Gem::Version
|
85
85
|
version: '0'
|
86
86
|
requirements: []
|
87
|
-
|
88
|
-
rubygems_version: 2.7.6
|
87
|
+
rubygems_version: 3.0.3
|
89
88
|
signing_key:
|
90
89
|
specification_version: 4
|
91
90
|
summary: An alternative approach to Rails' Object#try
|