safely_block 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +4 -0
- data/LICENSE.txt +1 -1
- data/README.md +40 -4
- data/lib/safely/core.rb +10 -6
- data/lib/safely/version.rb +1 -1
- data/lib/safely_block.rb +1 -1
- metadata +16 -27
- data/.gitignore +0 -22
- data/.travis.yml +0 -11
- data/Gemfile +0 -4
- data/Rakefile +0 -8
- data/safely_block.gemspec +0 -26
- data/test/safely_test.rb +0 -147
- data/test/test_helper.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f891f7ea77daf5922edec085cd02c05d8b8fe973e57acfe7715c1c5f0f5931a7
|
4
|
+
data.tar.gz: 0e4475ae9ccb1276f483017b13d0741042ba919ffb8f2ad8f801e3c6b93f1cbd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9c296d154cffcdf40d9b4eed9778ea98141ee44f66f54d0f2500ba4a81c985549b0f58785b3b63c097619eaed28f0132814f6e14ed79c0c53e5ece38f8e21fc
|
7
|
+
data.tar.gz: 5ef24bcf4b32bc6b4a0bc3e65d2d112f30a6a312e68e6f0c9ebc0652839829ae8dba75f2c96d374be1ee7b64c6edbd1a2535e0f124175a7c826f0d99d5e6ef8f
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -10,8 +10,18 @@ Exceptions are rescued and automatically reported to your favorite reporting ser
|
|
10
10
|
|
11
11
|
In development and test environments, exceptions are raised so you can fix them.
|
12
12
|
|
13
|
+
[Read more](https://ankane.org/safely-pattern)
|
14
|
+
|
13
15
|
[![Build Status](https://travis-ci.org/ankane/safely.svg?branch=master)](https://travis-ci.org/ankane/safely)
|
14
16
|
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application’s Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'safely_block'
|
23
|
+
```
|
24
|
+
|
15
25
|
## Use It Everywhere
|
16
26
|
|
17
27
|
“Oh no, analytics brought down search”
|
@@ -32,6 +42,14 @@ Also aliased as `yolo`.
|
|
32
42
|
|
33
43
|
## Features
|
34
44
|
|
45
|
+
Pass extra context to be reported with exceptions
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
safely context: {user_id: 123} do
|
49
|
+
# code
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
35
53
|
Specify a default value to return on exceptions
|
36
54
|
|
37
55
|
```ruby
|
@@ -81,7 +99,8 @@ Reports exceptions to a variety of services out of the box thanks to [Errbase](h
|
|
81
99
|
- [Airbrake](https://airbrake.io/)
|
82
100
|
- [Appsignal](https://appsignal.com/)
|
83
101
|
- [Bugsnag](https://bugsnag.com/)
|
84
|
-
- [
|
102
|
+
- [Exception Notification](https://github.com/smartinez87/exception_notification)
|
103
|
+
- [Google Stackdriver](https://cloud.google.com/stackdriver/)
|
85
104
|
- [Honeybadger](https://www.honeybadger.io/)
|
86
105
|
- [Opbeat](https://opbeat.com/)
|
87
106
|
- [Raygun](https://raygun.io/)
|
@@ -106,14 +125,22 @@ To report exceptions manually:
|
|
106
125
|
Safely.report_exception(e)
|
107
126
|
```
|
108
127
|
|
109
|
-
##
|
128
|
+
## Data Protection
|
110
129
|
|
111
|
-
|
130
|
+
To protect the privacy of your users, do not send [personal data](https://en.wikipedia.org/wiki/Personally_identifiable_information) to exception services. Filter sensitive form fields, use ids (not email addresses) to identify users, and mask IP addresses.
|
131
|
+
|
132
|
+
With Rollbar, you can do:
|
112
133
|
|
113
134
|
```ruby
|
114
|
-
|
135
|
+
Rollbar.configure do |config|
|
136
|
+
config.person_id_method = "id" # default
|
137
|
+
config.scrub_fields |= [:birthday]
|
138
|
+
config.anonymize_user_ip = true
|
139
|
+
end
|
115
140
|
```
|
116
141
|
|
142
|
+
While working on exceptions, be on the lookout for personal data and correct as needed.
|
143
|
+
|
117
144
|
## History
|
118
145
|
|
119
146
|
View the [changelog](https://github.com/ankane/safely/blob/master/CHANGELOG.md)
|
@@ -126,3 +153,12 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
|
|
126
153
|
- Fix bugs and [submit pull requests](https://github.com/ankane/safely/pulls)
|
127
154
|
- Write, clarify, or fix documentation
|
128
155
|
- Suggest or add new features
|
156
|
+
|
157
|
+
To get started with development and testing:
|
158
|
+
|
159
|
+
```sh
|
160
|
+
git clone https://github.com/ankane/safely.git
|
161
|
+
cd safely
|
162
|
+
bundle install
|
163
|
+
rake test
|
164
|
+
```
|
data/lib/safely/core.rb
CHANGED
@@ -7,7 +7,7 @@ module Safely
|
|
7
7
|
attr_accessor :raise_envs, :tag, :report_exception_method, :throttle_counter
|
8
8
|
attr_writer :env
|
9
9
|
|
10
|
-
def report_exception(e, tag: nil)
|
10
|
+
def report_exception(e, tag: nil, context: {})
|
11
11
|
tag = Safely.tag if tag.nil?
|
12
12
|
if tag && e.message
|
13
13
|
e = e.dup # leave original exception unmodified
|
@@ -16,7 +16,11 @@ module Safely
|
|
16
16
|
"[#{tag == true ? "safely" : tag}] #{message}"
|
17
17
|
end
|
18
18
|
end
|
19
|
-
report_exception_method.
|
19
|
+
if report_exception_method.arity == 1
|
20
|
+
report_exception_method.call(e)
|
21
|
+
else
|
22
|
+
report_exception_method.call(e, context)
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
26
|
def env
|
@@ -31,8 +35,8 @@ module Safely
|
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
|
-
DEFAULT_EXCEPTION_METHOD = proc do |e|
|
35
|
-
Errbase.report(e)
|
38
|
+
DEFAULT_EXCEPTION_METHOD = proc do |e, context|
|
39
|
+
Errbase.report(e, context)
|
36
40
|
end
|
37
41
|
|
38
42
|
self.tag = true
|
@@ -42,7 +46,7 @@ module Safely
|
|
42
46
|
self.throttle_counter = Hash.new(0)
|
43
47
|
|
44
48
|
module Methods
|
45
|
-
def safely(tag: nil, sample: nil, except: nil, only: nil, silence: nil, throttle: false, default: nil)
|
49
|
+
def safely(tag: nil, sample: nil, except: nil, only: nil, silence: nil, throttle: false, default: nil, context: {})
|
46
50
|
yield
|
47
51
|
rescue *Array(only || StandardError) => e
|
48
52
|
raise e if Array(except).any? { |c| e.is_a?(c) }
|
@@ -50,7 +54,7 @@ module Safely
|
|
50
54
|
if sample ? rand < 1.0 / sample : true
|
51
55
|
begin
|
52
56
|
unless Array(silence).any? { |c| e.is_a?(c) } || Safely.throttled?(e, throttle)
|
53
|
-
Safely.report_exception(e, tag: tag)
|
57
|
+
Safely.report_exception(e, tag: tag, context: context)
|
54
58
|
end
|
55
59
|
rescue => e2
|
56
60
|
$stderr.puts "FAIL-SAFE #{e2.class.name}: #{e2.message}"
|
data/lib/safely/version.rb
CHANGED
data/lib/safely_block.rb
CHANGED
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.2.
|
4
|
+
version: 0.2.2
|
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: 2019-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: errbase
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.1.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.1.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,34 +58,26 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
69
|
-
description:
|
70
|
-
email:
|
71
|
-
- andrew@chartkick.com
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email: andrew@chartkick.com
|
72
71
|
executables: []
|
73
72
|
extensions: []
|
74
73
|
extra_rdoc_files: []
|
75
74
|
files:
|
76
|
-
- ".gitignore"
|
77
|
-
- ".travis.yml"
|
78
75
|
- CHANGELOG.md
|
79
|
-
- Gemfile
|
80
76
|
- LICENSE.txt
|
81
77
|
- README.md
|
82
|
-
- Rakefile
|
83
78
|
- lib/safely/core.rb
|
84
79
|
- lib/safely/version.rb
|
85
80
|
- lib/safely_block.rb
|
86
|
-
- safely_block.gemspec
|
87
|
-
- test/safely_test.rb
|
88
|
-
- test/test_helper.rb
|
89
81
|
homepage: https://github.com/ankane/safely
|
90
82
|
licenses:
|
91
83
|
- MIT
|
@@ -98,18 +90,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
90
|
requirements:
|
99
91
|
- - ">="
|
100
92
|
- !ruby/object:Gem::Version
|
101
|
-
version: '
|
93
|
+
version: '2.3'
|
102
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
95
|
requirements:
|
104
96
|
- - ">="
|
105
97
|
- !ruby/object:Gem::Version
|
106
98
|
version: '0'
|
107
99
|
requirements: []
|
108
|
-
|
109
|
-
rubygems_version: 2.6.13
|
100
|
+
rubygems_version: 3.0.4
|
110
101
|
signing_key:
|
111
102
|
specification_version: 4
|
112
|
-
summary:
|
113
|
-
test_files:
|
114
|
-
- test/safely_test.rb
|
115
|
-
- test/test_helper.rb
|
103
|
+
summary: Rescue and report exceptions in non-critical code
|
104
|
+
test_files: []
|
data/.gitignore
DELETED
@@ -1,22 +0,0 @@
|
|
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
|
-
*.bundle
|
19
|
-
*.so
|
20
|
-
*.o
|
21
|
-
*.a
|
22
|
-
mkmf.log
|
data/.travis.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/safely_block.gemspec
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "safely/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "safely_block"
|
8
|
-
spec.version = Safely::VERSION
|
9
|
-
spec.authors = ["Andrew Kane"]
|
10
|
-
spec.email = ["andrew@chartkick.com"]
|
11
|
-
spec.summary = "Awesome exception handling"
|
12
|
-
spec.description = "Awesome exception handling"
|
13
|
-
spec.homepage = "https://github.com/ankane/safely"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
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_dependency "errbase"
|
22
|
-
|
23
|
-
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
-
spec.add_development_dependency "rake"
|
25
|
-
spec.add_development_dependency "minitest", ">= 5"
|
26
|
-
end
|
data/test/safely_test.rb
DELETED
@@ -1,147 +0,0 @@
|
|
1
|
-
require_relative "test_helper"
|
2
|
-
|
3
|
-
class TestSafely < Minitest::Test
|
4
|
-
def setup
|
5
|
-
Safely.env = "production"
|
6
|
-
Safely.tag = true
|
7
|
-
Safely.report_exception_method = Safely::DEFAULT_EXCEPTION_METHOD
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_development_environment
|
11
|
-
Safely.env = "development"
|
12
|
-
assert_raises(Safely::TestError) do
|
13
|
-
safely do
|
14
|
-
raise Safely::TestError
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_test_environment
|
20
|
-
Safely.env = "test"
|
21
|
-
assert_raises(Safely::TestError) do
|
22
|
-
safely do
|
23
|
-
raise Safely::TestError
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_production_environment
|
29
|
-
exception = Safely::TestError.new
|
30
|
-
mock = MiniTest::Mock.new
|
31
|
-
mock.expect :report_exception, nil, [exception]
|
32
|
-
Safely.report_exception_method = -> (e) { mock.report_exception(e) }
|
33
|
-
safely do
|
34
|
-
raise exception
|
35
|
-
end
|
36
|
-
assert mock.verify
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_yolo
|
40
|
-
exception = Safely::TestError.new
|
41
|
-
mock = MiniTest::Mock.new
|
42
|
-
mock.expect :report_exception, nil, [exception]
|
43
|
-
Safely.report_exception_method = -> (e) { mock.report_exception(e) }
|
44
|
-
yolo do
|
45
|
-
raise exception
|
46
|
-
end
|
47
|
-
assert mock.verify
|
48
|
-
end
|
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
|
-
|
72
|
-
def test_report_exception_tag
|
73
|
-
ex = nil
|
74
|
-
Safely.report_exception_method = -> (e) { ex = e }
|
75
|
-
begin
|
76
|
-
raise Safely::TestError, "Boom"
|
77
|
-
rescue => e
|
78
|
-
Safely.report_exception(e)
|
79
|
-
end
|
80
|
-
assert_equal "[safely] Boom", ex.message
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_return_value
|
84
|
-
assert_equal 1, safely { 1 }
|
85
|
-
assert_nil safely { raise Safely::TestError, "Boom" }
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_default
|
89
|
-
assert_equal 1, safely(default: 2) { 1 }
|
90
|
-
assert_equal 2, safely(default: 2) { raise Safely::TestError, "Boom" }
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_only
|
94
|
-
assert_nil safely(only: Safely::TestError) { raise Safely::TestError }
|
95
|
-
assert_raises(RuntimeError, "Boom") { safely(only: Safely::TestError) { raise "Boom" } }
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_only_array
|
99
|
-
assert_nil safely(only: [Safely::TestError]) { raise Safely::TestError }
|
100
|
-
assert_raises(RuntimeError, "Boom") { safely(only: [Safely::TestError]) { raise "Boom" } }
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_except
|
104
|
-
assert_raises(Safely::TestError, "Boom") { safely(except: StandardError) { raise Safely::TestError, "Boom" } }
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_silence
|
108
|
-
safely(silence: StandardError) { raise Safely::TestError, "Boom" }
|
109
|
-
assert true
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_failsafe
|
113
|
-
Safely.report_exception_method = -> (_) { raise "oops" }
|
114
|
-
_, err = capture_io do
|
115
|
-
safely { raise "boom" }
|
116
|
-
end
|
117
|
-
assert_equal "FAIL-SAFE RuntimeError: oops\n", err
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_throttle
|
121
|
-
count = 0
|
122
|
-
Safely.report_exception_method = -> (_) { count += 1 }
|
123
|
-
5.times do |n|
|
124
|
-
safely throttle: {limit: 2, period: 3600} do
|
125
|
-
raise Safely::TestError
|
126
|
-
end
|
127
|
-
end
|
128
|
-
assert_equal 2, count
|
129
|
-
end
|
130
|
-
|
131
|
-
def test_throttle_key
|
132
|
-
count = 0
|
133
|
-
Safely.report_exception_method = -> (_) { count += 1 }
|
134
|
-
5.times do |n|
|
135
|
-
safely throttle: {limit: 2, period: 3600, key: "boom#{n % 2}"} do
|
136
|
-
raise Safely::TestError
|
137
|
-
end
|
138
|
-
end
|
139
|
-
assert_equal 4, count
|
140
|
-
end
|
141
|
-
|
142
|
-
def test_bad_argument
|
143
|
-
assert_raises(ArgumentError) do
|
144
|
-
safely(unknown: true) { }
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|