safely_block 0.1.1 → 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 +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +9 -7
- data/lib/safely/core.rb +15 -8
- data/lib/safely/version.rb +1 -1
- data/test/safely_test.rb +38 -9
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff610f8bed6b2446fd70d85987ec4e622e5f5021
|
4
|
+
data.tar.gz: 56a9d89bee3fd0aa4804623e29639b3aeb39e8a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06cf78fcab0ee8e6a743f27b5ca9b95f45049a47c78700013da0298f7c7967616df9c6f6f082e1a013f5e06fa12e6636d2e7fc811747fa0aa7a362328754ecba
|
7
|
+
data.tar.gz: 1dcf67c2f094f854f358986240c14fe5e5afcfe6514d58a1f1cc95e8f670e5caa96462180a538f3e93427b9ef419bf1be66902c913d811adc0f889cd92dcc577
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -57,7 +57,9 @@ end
|
|
57
57
|
Silence exceptions
|
58
58
|
|
59
59
|
```ruby
|
60
|
-
safely
|
60
|
+
safely silence: ActiveRecord::RecordNotUnique do
|
61
|
+
# code
|
62
|
+
end
|
61
63
|
```
|
62
64
|
|
63
65
|
Throttle reporting with:
|
@@ -74,20 +76,20 @@ end
|
|
74
76
|
|
75
77
|
Reports exceptions to a variety of services out of the box thanks to [Errbase](https://github.com/ankane/errbase).
|
76
78
|
|
77
|
-
- [Rollbar](https://rollbar.com/)
|
78
79
|
- [Airbrake](https://airbrake.io/)
|
80
|
+
- [Appsignal](https://appsignal.com/)
|
81
|
+
- [Bugsnag](https://bugsnag.com/)
|
79
82
|
- [Exceptional](http://www.exceptional.io/)
|
80
83
|
- [Honeybadger](https://www.honeybadger.io/)
|
81
|
-
- [Sentry](https://getsentry.com/)
|
82
|
-
- [Raygun](https://raygun.io/)
|
83
|
-
- [Bugsnag](https://bugsnag.com/)
|
84
|
-
- [Appsignal](https://appsignal.com/)
|
85
84
|
- [Opbeat](https://opbeat.com/)
|
85
|
+
- [Raygun](https://raygun.io/)
|
86
|
+
- [Rollbar](https://rollbar.com/)
|
87
|
+
- [Sentry](https://getsentry.com/)
|
86
88
|
|
87
89
|
Customize reporting with:
|
88
90
|
|
89
91
|
```ruby
|
90
|
-
Safely.report_exception_method =
|
92
|
+
Safely.report_exception_method = -> (e) { Rollbar.error(e) }
|
91
93
|
```
|
92
94
|
|
93
95
|
By default, exception messages are prefixed with `[safely]`. This makes it easier to spot rescued exceptions. Turn this off with:
|
data/lib/safely/core.rb
CHANGED
@@ -24,8 +24,6 @@ module Safely
|
|
24
24
|
end
|
25
25
|
|
26
26
|
DEFAULT_EXCEPTION_METHOD = proc do |e|
|
27
|
-
e = e.dup # leave original exception unmodified
|
28
|
-
e.message.prepend("[safely] ") if e.message && Safely.tag
|
29
27
|
Errbase.report(e)
|
30
28
|
end
|
31
29
|
|
@@ -36,20 +34,29 @@ module Safely
|
|
36
34
|
self.throttle_counter = Hash.new(0)
|
37
35
|
|
38
36
|
module Methods
|
39
|
-
def safely(
|
37
|
+
def safely(tag: nil, sample: nil, except: nil, only: nil, silence: nil, throttle: false, default: nil)
|
40
38
|
yield
|
41
|
-
rescue *Array(
|
42
|
-
raise e if Array(
|
39
|
+
rescue *Array(only || StandardError) => e
|
40
|
+
raise e if Array(except).any? { |c| e.is_a?(c) }
|
43
41
|
raise e if Safely.raise_envs.include?(Safely.env)
|
44
|
-
sample = options[:sample]
|
45
42
|
if sample ? rand < 1.0 / sample : true
|
46
43
|
begin
|
47
|
-
|
44
|
+
unless Array(silence).any? { |c| e.is_a?(c) } || Safely.throttled?(e, throttle)
|
45
|
+
tag = Safely.tag if tag.nil?
|
46
|
+
if tag && e.message
|
47
|
+
e = e.dup # leave original exception unmodified
|
48
|
+
message = e.message
|
49
|
+
e.define_singleton_method(:message) do
|
50
|
+
"[#{tag == true ? "safely" : tag}] #{message}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
Safely.report_exception(e)
|
54
|
+
end
|
48
55
|
rescue => e2
|
49
56
|
$stderr.puts "FAIL-SAFE #{e2.class.name}: #{e2.message}"
|
50
57
|
end
|
51
58
|
end
|
52
|
-
|
59
|
+
default
|
53
60
|
end
|
54
61
|
alias_method :yolo, :safely
|
55
62
|
end
|
data/lib/safely/version.rb
CHANGED
data/test/safely_test.rb
CHANGED
@@ -3,6 +3,7 @@ require_relative "test_helper"
|
|
3
3
|
class TestSafely < Minitest::Test
|
4
4
|
def setup
|
5
5
|
Safely.env = "production"
|
6
|
+
Safely.tag = true
|
6
7
|
Safely.report_exception_method = Safely::DEFAULT_EXCEPTION_METHOD
|
7
8
|
end
|
8
9
|
|
@@ -28,7 +29,7 @@ class TestSafely < Minitest::Test
|
|
28
29
|
exception = Safely::TestError.new
|
29
30
|
mock = MiniTest::Mock.new
|
30
31
|
mock.expect :report_exception, nil, [exception]
|
31
|
-
Safely.report_exception_method =
|
32
|
+
Safely.report_exception_method = -> (e) { mock.report_exception(e) }
|
32
33
|
safely do
|
33
34
|
raise exception
|
34
35
|
end
|
@@ -39,16 +40,38 @@ class TestSafely < Minitest::Test
|
|
39
40
|
exception = Safely::TestError.new
|
40
41
|
mock = MiniTest::Mock.new
|
41
42
|
mock.expect :report_exception, nil, [exception]
|
42
|
-
Safely.report_exception_method =
|
43
|
+
Safely.report_exception_method = -> (e) { mock.report_exception(e) }
|
43
44
|
yolo do
|
44
45
|
raise exception
|
45
46
|
end
|
46
47
|
assert mock.verify
|
47
48
|
end
|
48
49
|
|
50
|
+
def test_tagged
|
51
|
+
ex = nil
|
52
|
+
Safely.report_exception_method = -> (e) { ex = e }
|
53
|
+
safely { raise Safely::TestError, "Boom" }
|
54
|
+
assert_equal "[safely] Boom", ex.message
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_not_tagged
|
58
|
+
Safely.tag = false
|
59
|
+
ex = nil
|
60
|
+
Safely.report_exception_method = -> (e) { ex = e }
|
61
|
+
safely { raise Safely::TestError, "Boom" }
|
62
|
+
assert_equal "Boom", ex.message
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_local_tag
|
66
|
+
ex = nil
|
67
|
+
Safely.report_exception_method = -> (e) { ex = e }
|
68
|
+
safely(tag: "hi") { raise Safely::TestError, "Boom" }
|
69
|
+
assert_equal "[hi] Boom", ex.message
|
70
|
+
end
|
71
|
+
|
49
72
|
def test_return_value
|
50
73
|
assert_equal 1, safely { 1 }
|
51
|
-
|
74
|
+
assert_nil safely { raise Safely::TestError, "Boom" }
|
52
75
|
end
|
53
76
|
|
54
77
|
def test_default
|
@@ -57,12 +80,12 @@ class TestSafely < Minitest::Test
|
|
57
80
|
end
|
58
81
|
|
59
82
|
def test_only
|
60
|
-
|
83
|
+
assert_nil safely(only: Safely::TestError) { raise Safely::TestError }
|
61
84
|
assert_raises(RuntimeError, "Boom") { safely(only: Safely::TestError) { raise "Boom" } }
|
62
85
|
end
|
63
86
|
|
64
87
|
def test_only_array
|
65
|
-
|
88
|
+
assert_nil safely(only: [Safely::TestError]) { raise Safely::TestError }
|
66
89
|
assert_raises(RuntimeError, "Boom") { safely(only: [Safely::TestError]) { raise "Boom" } }
|
67
90
|
end
|
68
91
|
|
@@ -76,8 +99,8 @@ class TestSafely < Minitest::Test
|
|
76
99
|
end
|
77
100
|
|
78
101
|
def test_failsafe
|
79
|
-
Safely.report_exception_method =
|
80
|
-
|
102
|
+
Safely.report_exception_method = -> (_) { raise "oops" }
|
103
|
+
_, err = capture_io do
|
81
104
|
safely { raise "boom" }
|
82
105
|
end
|
83
106
|
assert_equal "FAIL-SAFE RuntimeError: oops\n", err
|
@@ -85,7 +108,7 @@ class TestSafely < Minitest::Test
|
|
85
108
|
|
86
109
|
def test_throttle
|
87
110
|
count = 0
|
88
|
-
Safely.report_exception_method =
|
111
|
+
Safely.report_exception_method = -> (_) { count += 1 }
|
89
112
|
5.times do |n|
|
90
113
|
safely throttle: {limit: 2, period: 3600} do
|
91
114
|
raise Safely::TestError
|
@@ -96,7 +119,7 @@ class TestSafely < Minitest::Test
|
|
96
119
|
|
97
120
|
def test_throttle_key
|
98
121
|
count = 0
|
99
|
-
Safely.report_exception_method =
|
122
|
+
Safely.report_exception_method = -> (_) { count += 1 }
|
100
123
|
5.times do |n|
|
101
124
|
safely throttle: {limit: 2, period: 3600, key: "boom#{n % 2}"} do
|
102
125
|
raise Safely::TestError
|
@@ -104,4 +127,10 @@ class TestSafely < Minitest::Test
|
|
104
127
|
end
|
105
128
|
assert_equal 4, count
|
106
129
|
end
|
130
|
+
|
131
|
+
def test_bad_argument
|
132
|
+
assert_raises(ArgumentError) do
|
133
|
+
safely(unknown: true) { }
|
134
|
+
end
|
135
|
+
end
|
107
136
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safely_block
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: errbase
|
@@ -105,11 +105,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
105
|
version: '0'
|
106
106
|
requirements: []
|
107
107
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.6.
|
108
|
+
rubygems_version: 2.6.8
|
109
109
|
signing_key:
|
110
110
|
specification_version: 4
|
111
111
|
summary: Awesome exception handling
|
112
112
|
test_files:
|
113
113
|
- test/safely_test.rb
|
114
114
|
- test/test_helper.rb
|
115
|
-
has_rdoc:
|