rubocop-swallow-exception 0.1.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 +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +106 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/default.yml +3 -0
- data/lib/rubocop-swallow-exception.rb +6 -0
- data/lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb +62 -0
- data/lib/rubocop/swallow_exception/inject.rb +17 -0
- data/lib/rubocop/swallow_exception/version.rb +5 -0
- data/rubocop-swallow-exception.gemspec +43 -0
- data/rubocop-swallow-exception.iml +24 -0
- data/rubocop-swallow-exception.png +0 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0c755c6d7e8ff63a15867afc9aa4685200b84764
|
4
|
+
data.tar.gz: b445490c52fd37abae40d106b164cd4ebc5ad9a8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3bb562f5ec4dbab25dde2f435d600adbc34d631de9d47f6a8720c3b50c26eb1b9b3ede3e7b306cfcf5e9e8dd472402350d3ce893c18dc7de09dcdcbd5ecde6a3
|
7
|
+
data.tar.gz: 13f748452d5097c69dee442f010e422ecc3b88af4bdfddc3c919c47b47ef4ebb7d215b376e92684a0a95e11358a1c88c83e2d59df02e97abde557aba68eda26c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 ONDA, Takashi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# rubocop-swallow-exception
|
2
|
+
|
3
|
+
This is mmj's custom Cop that forbids swallowing exception.
|
4
|
+
See [OWASP article](https://www.owasp.org/index.php/Exception_handling_techniques#Swallowing_Exceptions)
|
5
|
+
to understand why this Cop is required.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rubocop-swallow-exception'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install rubocop-swallow-exception
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Just add require option when you run rubocop.
|
27
|
+
|
28
|
+
$ rubocop --require rubocop-swallow-exception
|
29
|
+
|
30
|
+

|
31
|
+
|
32
|
+
|
33
|
+
## Specification
|
34
|
+
|
35
|
+
The Cop searches rescue body that does not contain raise statement in top level
|
36
|
+
nor `Raven.capture_exception` ([Sentry](https://sentry.io) client) calling
|
37
|
+
|
38
|
+
See spec file below in detail.
|
39
|
+
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
describe RuboCop::SwallowException do
|
43
|
+
|
44
|
+
subject(:cop) { RuboCop::Cop::Lint::SwallowException.new }
|
45
|
+
|
46
|
+
it 'has a version number' do
|
47
|
+
expect(RuboCop::SwallowException::VERSION).not_to be(nil)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'NG when rescue body is empty' do
|
51
|
+
inspect_source(cop, <<-EOS)
|
52
|
+
def bad_method
|
53
|
+
p :hello
|
54
|
+
rescue => e
|
55
|
+
# do nothing
|
56
|
+
end
|
57
|
+
EOS
|
58
|
+
expect(cop.offenses.size).to eq(1)
|
59
|
+
expect(cop.messages.first).to eq('rescue body is empty!')
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'OK when raise exception in top level' do
|
63
|
+
inspect_source(cop, <<-EOS)
|
64
|
+
def bad_method
|
65
|
+
p :hello
|
66
|
+
rescue => e
|
67
|
+
log.error 'error occured'
|
68
|
+
log.error e.backtrace.join("\n")
|
69
|
+
raise e
|
70
|
+
end
|
71
|
+
EOS
|
72
|
+
expect(cop.offenses.size).to eq(0)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "OK when send error to Sentry, by using `Raven.capture_exception'" do
|
76
|
+
inspect_source(cop, <<-EOS)
|
77
|
+
def bad_method
|
78
|
+
p :hello
|
79
|
+
rescue => e
|
80
|
+
Raven.capture_exception(e)
|
81
|
+
end
|
82
|
+
EOS
|
83
|
+
expect(cop.offenses.size).to eq(0)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'NG when rescue body does not contain raise nor Raven' do
|
87
|
+
inspect_source(cop, <<-EOS)
|
88
|
+
def bad_method
|
89
|
+
p :hello
|
90
|
+
rescue => e
|
91
|
+
log.error 'error occured'
|
92
|
+
log.error e.backtrace.join("\n")
|
93
|
+
end
|
94
|
+
EOS
|
95
|
+
expect(cop.offenses.size).to eq(1)
|
96
|
+
expect(cop.messages.first).to match(/you have to/)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
```
|
101
|
+
|
102
|
+
|
103
|
+
## License
|
104
|
+
|
105
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
106
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rubocop/swallow/exception"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/config/default.yml
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module RuboCop
|
2
|
+
module Cop
|
3
|
+
module Lint
|
4
|
+
|
5
|
+
class SwallowException < Cop
|
6
|
+
|
7
|
+
def on_resbody(node)
|
8
|
+
# rescue の中身が空ならエラー
|
9
|
+
unless node.children[2]
|
10
|
+
add_offense(node, :expression, 'rescue body is empty!', :fatal)
|
11
|
+
return
|
12
|
+
end
|
13
|
+
body = node.children[2]
|
14
|
+
# トップレベルで条件なしに raise していれば OK
|
15
|
+
return if has_raise?(body)
|
16
|
+
# トップレベルで Raven.capture_exception 呼び出していれば OK
|
17
|
+
return if has_raven_capture_exception?(body)
|
18
|
+
# raise も Raven.capture_exception もなければエラー
|
19
|
+
add_offense(node, :expression, (<<-MSG).strip, :fatal)
|
20
|
+
you have to raise exception or capture exception by Raven in rescue body.
|
21
|
+
MSG
|
22
|
+
end
|
23
|
+
|
24
|
+
def has_raven_capture_exception?(node)
|
25
|
+
case node.type
|
26
|
+
when :send
|
27
|
+
raven_capture_exception?(node)
|
28
|
+
when :begin
|
29
|
+
node.children.any? { |c| raven_capture_exception?(c) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_raise?(node)
|
34
|
+
case node.type
|
35
|
+
when :send
|
36
|
+
raise?(node)
|
37
|
+
when :begin
|
38
|
+
node.children.any? { |c| raise?(c) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def raven_capture_exception?(node)
|
43
|
+
node.type == :send &&
|
44
|
+
raven?(node.children[0]) &&
|
45
|
+
node.children[1] == :capture_exception
|
46
|
+
end
|
47
|
+
|
48
|
+
def raven?(node)
|
49
|
+
node.type == :const && node.children[1] == :Raven
|
50
|
+
end
|
51
|
+
|
52
|
+
def raise?(node)
|
53
|
+
node.type == :send &&
|
54
|
+
node.children[0] == nil &&
|
55
|
+
node.children[1] == :raise
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubocop/config_loader'
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module SwallowException
|
5
|
+
module Inject
|
6
|
+
|
7
|
+
DEFAULT_FILE = File.expand_path('../../../../config/default.yml', __FILE__)
|
8
|
+
|
9
|
+
def self.defaults!
|
10
|
+
default = ConfigLoader.load_file(DEFAULT_FILE)
|
11
|
+
config = ConfigLoader.merge_with_default(default, DEFAULT_FILE)
|
12
|
+
ConfigLoader.instance_variable_set(:@default_configuration, config)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rubocop/swallow_exception/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
|
8
|
+
spec.name = 'rubocop-swallow-exception'
|
9
|
+
spec.version = RuboCop::SwallowException::VERSION
|
10
|
+
spec.authors = ['ONDA, Takashi']
|
11
|
+
spec.email = ['onda@mmj.ne.jp']
|
12
|
+
|
13
|
+
spec.summary = %q{custom Cop forbids swallowing exception}
|
14
|
+
spec.description = <<-EOD
|
15
|
+
This custom Cop forbids swallowing exception.
|
16
|
+
See OWASP article.
|
17
|
+
https://www.owasp.org/index.php/Exception_handling_techniques#Swallowing_Exceptions
|
18
|
+
EOD
|
19
|
+
spec.homepage = 'https://github.com/mediamaxjapan/rubocop-swallow-exception'
|
20
|
+
spec.license = 'MIT'
|
21
|
+
|
22
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
23
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
24
|
+
# if spec.respond_to?(:metadata)
|
25
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
26
|
+
# else
|
27
|
+
# raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
28
|
+
# end
|
29
|
+
|
30
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
31
|
+
f.match(%r{^(test|spec|features)/})
|
32
|
+
end
|
33
|
+
spec.bindir = 'exe'
|
34
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ['lib']
|
36
|
+
|
37
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
38
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
39
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
40
|
+
|
41
|
+
spec.add_runtime_dependency('rubocop', '~> 0.43')
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
3
|
+
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
4
|
+
<exclude-output />
|
5
|
+
<content url="file://$MODULE_DIR$" />
|
6
|
+
<orderEntry type="jdk" jdkName="ruby-2.3.1-p112" jdkType="RUBY_SDK" />
|
7
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
8
|
+
<orderEntry type="library" scope="PROVIDED" name="ast (v2.3.0, ruby-2.3.1-p112) [gem]" level="application" />
|
9
|
+
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.13.0, ruby-2.3.1-p112) [gem]" level="application" />
|
10
|
+
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.2.5, ruby-2.3.1-p112) [gem]" level="application" />
|
11
|
+
<orderEntry type="library" scope="PROVIDED" name="parser (v2.3.1.4, ruby-2.3.1-p112) [gem]" level="application" />
|
12
|
+
<orderEntry type="library" scope="PROVIDED" name="powerpack (v0.1.1, ruby-2.3.1-p112) [gem]" level="application" />
|
13
|
+
<orderEntry type="library" scope="PROVIDED" name="rainbow (v2.1.0, ruby-2.3.1-p112) [gem]" level="application" />
|
14
|
+
<orderEntry type="library" scope="PROVIDED" name="rake (v10.4.2, ruby-2.3.1-p112) [gem]" level="application" />
|
15
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec (v3.5.0, ruby-2.3.1-p112) [gem]" level="application" />
|
16
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.5.3, ruby-2.3.1-p112) [gem]" level="application" />
|
17
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.5.0, ruby-2.3.1-p112) [gem]" level="application" />
|
18
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.5.0, ruby-2.3.1-p112) [gem]" level="application" />
|
19
|
+
<orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.5.0, ruby-2.3.1-p112) [gem]" level="application" />
|
20
|
+
<orderEntry type="library" scope="PROVIDED" name="rubocop (v0.43.0, ruby-2.3.1-p112) [gem]" level="application" />
|
21
|
+
<orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.8.1, ruby-2.3.1-p112) [gem]" level="application" />
|
22
|
+
<orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v1.1.1, ruby-2.3.1-p112) [gem]" level="application" />
|
23
|
+
</component>
|
24
|
+
</module>
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubocop-swallow-exception
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ONDA, Takashi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-21 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.43'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.43'
|
69
|
+
description: |2
|
70
|
+
This custom Cop forbids swallowing exception.
|
71
|
+
See OWASP article.
|
72
|
+
https://www.owasp.org/index.php/Exception_handling_techniques#Swallowing_Exceptions
|
73
|
+
email:
|
74
|
+
- onda@mmj.ne.jp
|
75
|
+
executables: []
|
76
|
+
extensions: []
|
77
|
+
extra_rdoc_files: []
|
78
|
+
files:
|
79
|
+
- ".gitignore"
|
80
|
+
- ".rspec"
|
81
|
+
- ".ruby-version"
|
82
|
+
- ".travis.yml"
|
83
|
+
- Gemfile
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- bin/console
|
88
|
+
- bin/setup
|
89
|
+
- config/default.yml
|
90
|
+
- lib/rubocop-swallow-exception.rb
|
91
|
+
- lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb
|
92
|
+
- lib/rubocop/swallow_exception/inject.rb
|
93
|
+
- lib/rubocop/swallow_exception/version.rb
|
94
|
+
- rubocop-swallow-exception.gemspec
|
95
|
+
- rubocop-swallow-exception.iml
|
96
|
+
- rubocop-swallow-exception.png
|
97
|
+
homepage: https://github.com/mediamaxjapan/rubocop-swallow-exception
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.5.1
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: custom Cop forbids swallowing exception
|
121
|
+
test_files: []
|