caution 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +2 -0
- data/.rubocop.yml +23 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +53 -0
- data/LICENSE +7 -0
- data/README.md +97 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/caution.gemspec +31 -0
- data/lib/caution.rb +11 -0
- data/lib/caution/cause_injecting_error.rb +36 -0
- data/lib/caution/illegal_state_error.rb +15 -0
- data/lib/caution/version.rb +5 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5ac1001fd1b5e7504dd8b415bc915ce3c2b9ba91
|
4
|
+
data.tar.gz: 8959252e7804879bfce6e5b1711cbad0902d8573
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b1fc759ef7f3f5fb5f18ac118e0b374c3253ddfddcab1fa105e9a4031ea5554d06dda3b6e5ac3483e45238cb68a36ee9504342cc3cbefbe06c47f72a3e02f8a
|
7
|
+
data.tar.gz: fb228a07b111cf43a3a0a792a428df721a662f6b7f85734d502450516e918ec8b8644753579b3dab4636e429688b598ad502eb0e7706b7a6de8bad41440bff3c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'bin/**/*'
|
4
|
+
- 'vendor/**/*'
|
5
|
+
DisplayCopNames: true
|
6
|
+
TargetRubyVersion: 2.3.8
|
7
|
+
|
8
|
+
Metrics/BlockLength:
|
9
|
+
Enabled: true
|
10
|
+
Exclude:
|
11
|
+
- spec/**/*_spec.rb
|
12
|
+
|
13
|
+
Metrics/LineLength:
|
14
|
+
Max: 100
|
15
|
+
|
16
|
+
Style/TrailingCommaInArguments:
|
17
|
+
EnforcedStyleForMultiline: comma
|
18
|
+
|
19
|
+
Style/TrailingCommaInArrayLiteral:
|
20
|
+
EnforcedStyleForMultiline: comma
|
21
|
+
|
22
|
+
Style/TrailingCommaInHashLiteral:
|
23
|
+
EnforcedStyleForMultiline: comma
|
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.3.8
|
5
|
+
- 2.4.5
|
6
|
+
- 2.5.3
|
7
|
+
cache: bundler
|
8
|
+
before_install: gem install bundler -v 1.17.2
|
9
|
+
script:
|
10
|
+
- bundle exec rspec
|
11
|
+
- bundle exec rubocop
|
12
|
+
notifications:
|
13
|
+
hipchat:
|
14
|
+
rooms:
|
15
|
+
secure: QAkfosLUyeazHpg215UKvMonOHMsV2QRxN7hPvrdhIujNme7xm1EU5sLjAf8NFS5qrLBK9qqZpUcR32dqM/uQ+Sodi9FjZ29Q2YpFGHxy5N+yHLo1bla7dW+Uj8soLh6Sd12nO45xDbQM5N5t+dZAfQPyewxr//PR5bmC/HuhW5tBcsRZX+lkp1YPs8h17u4lnNcf8g5EtBq0CdughBdgIjDJGiERx6OclyDh8HQ2BZhhFITsWs2/dDAOPRfSk6CcRVard47aybFnEFvOu21/r3J3UNN0dy1pf9o0L7qBOEoinm5UAbllEHq+EyYHVeNa5YnjX6m51k/X13D+2BzaiLZ06VS95v5Irj0mWJj+vUJ2WY0ED0ywyRf2F6VIJXzL8FXVz/mIFIvMmyinm1slv7G+6jFeJaHhm3HiYoAwBfbvYoT20cUxYl0Lsu3h0yLbt7YvxwoURCpCFAW/wl7gLIL3nD0lncHFTAScsL2QMbOe5eX/CBLR1TBe+5ZjNe1NsWyvPuWeS8V6fllg3ve1AIMRaI4PZXbjXqFusAGVttZ8a8P7YxachsXkVdYKGJ3Dr3EDXh2cYwxlG80XIWaHulVJPyC5RyX2U0qwdFuWaPUdDfH8rQ2o4YmakG69Zoy7cqbK/rukFHhCc9RA8yn4g2gqwVyGY/10eWH+LDGFrU=
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
caution (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
jaro_winkler (1.5.1)
|
12
|
+
parallel (1.12.1)
|
13
|
+
parser (2.5.3.0)
|
14
|
+
ast (~> 2.4.0)
|
15
|
+
powerpack (0.1.2)
|
16
|
+
rainbow (3.0.0)
|
17
|
+
rake (12.3.1)
|
18
|
+
rspec (3.8.0)
|
19
|
+
rspec-core (~> 3.8.0)
|
20
|
+
rspec-expectations (~> 3.8.0)
|
21
|
+
rspec-mocks (~> 3.8.0)
|
22
|
+
rspec-core (3.8.0)
|
23
|
+
rspec-support (~> 3.8.0)
|
24
|
+
rspec-expectations (3.8.2)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.8.0)
|
27
|
+
rspec-mocks (3.8.0)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.8.0)
|
30
|
+
rspec-support (3.8.0)
|
31
|
+
rubocop (0.60.0)
|
32
|
+
jaro_winkler (~> 1.5.1)
|
33
|
+
parallel (~> 1.10)
|
34
|
+
parser (>= 2.5, != 2.5.1.1)
|
35
|
+
powerpack (~> 0.1)
|
36
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
|
+
ruby-progressbar (~> 1.7)
|
38
|
+
unicode-display_width (~> 1.4.0)
|
39
|
+
ruby-progressbar (1.10.0)
|
40
|
+
unicode-display_width (1.4.0)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
bundler
|
47
|
+
caution!
|
48
|
+
rake
|
49
|
+
rspec
|
50
|
+
rubocop
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
1.17.2
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2018 Blue Marble Payroll, LLC
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# ⚠️ Caution ⚠️
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/bluemarblepayroll/caution.svg?branch=master)](https://travis-ci.org/bluemarblepayroll/caution)
|
4
|
+
|
5
|
+
Essential general purpose Ruby error classes.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Two error classes are provided. The first is IllegalStateError which is intended to be raised from inside a method when the current internal state would prevent a successful operation. For those coming from Java or .Net, this is analogous to an IllegalStateError or an InvalidOperationException.
|
10
|
+
|
11
|
+
### Caution::IllegalStateError Example
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
def delete
|
15
|
+
raise Caution::IllegalStateError, 'can not delete when not persisted' unless persisted?
|
16
|
+
|
17
|
+
# logic here to actually perform the delete
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
The second class is CauseInjectingError which is intended for cases when wrapping a low level exception with a higher level exception. It is intended to be subclassed with an error class which fits the application's domain. The benefit it provides is that information about the lower level exception is available in the CauseInjectingError's message. This can be helpful when using application frameworks which are not aware of Ruby's fairly recent [Exception#cause](https://ruby-doc.org/core-2.6.0.preview2/Exception.html#method-i-cause) method.
|
22
|
+
|
23
|
+
### Caution::CauseInjectingError Example
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# Define your own subclass:
|
27
|
+
class HighLevelError < Caution::CauseInjectingError; end
|
28
|
+
|
29
|
+
# Then use the error to wrap a low level error:
|
30
|
+
def process_records
|
31
|
+
records.each do |record|
|
32
|
+
begin
|
33
|
+
# some low level code to process the record which could raise a LowLevelError
|
34
|
+
rescue LowLevelError
|
35
|
+
# Like all Ruby 2 exceptions, the LowLevelError is captured in the cause:
|
36
|
+
raise HighLevelError, 'Could not process record #{record.id}'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def main
|
42
|
+
process_records
|
43
|
+
rescue => error
|
44
|
+
# The HighLevelError message is printed along with the LowLevelError
|
45
|
+
# message and stack trace. Without CauseInjectingError, only HighLevelError
|
46
|
+
# information is included in the message.
|
47
|
+
$stderr.puts("An unexpected error occured: #{error.message}")
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
## Installation
|
52
|
+
|
53
|
+
To install through Rubygems:
|
54
|
+
|
55
|
+
````
|
56
|
+
gem install install caution
|
57
|
+
````
|
58
|
+
|
59
|
+
You can also add this to your Gemfile:
|
60
|
+
|
61
|
+
````
|
62
|
+
bundle add caution
|
63
|
+
````
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
### Development Environment Configuration
|
68
|
+
|
69
|
+
Basic steps to take to get this repository set up:
|
70
|
+
|
71
|
+
1. Install [Ruby](https://www.ruby-lang.org/en/documentation/installation/) (check caution.gemspec for versions supported)
|
72
|
+
2. Install bundler (`gem install bundler`)
|
73
|
+
3. Clone the repository (`git clone git@github.com:bluemarblepayroll/caution.git`)
|
74
|
+
4. Navigate to the root folder (`cd caution`)
|
75
|
+
5. Install dependencies (`bundle`)
|
76
|
+
|
77
|
+
### Running Tests
|
78
|
+
|
79
|
+
To execute the test suite run:
|
80
|
+
|
81
|
+
````
|
82
|
+
rspec
|
83
|
+
````
|
84
|
+
|
85
|
+
Also be sure to run Rubocop:
|
86
|
+
|
87
|
+
```
|
88
|
+
rubocop
|
89
|
+
```
|
90
|
+
|
91
|
+
### Pull Requests
|
92
|
+
|
93
|
+
Pull requests are welcome! Please ensure that the tests and Rubocop are passing before submission.
|
94
|
+
|
95
|
+
## License
|
96
|
+
|
97
|
+
This project is MIT Licensed.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "caution"
|
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/caution.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'caution/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'caution'
|
9
|
+
spec.version = Caution::VERSION
|
10
|
+
spec.authors = ['Ryan Gerry']
|
11
|
+
spec.email = ['rgerry@bluemarblepayroll.com']
|
12
|
+
|
13
|
+
spec.summary = 'Essential general purpose Ruby error classes.'
|
14
|
+
spec.description = 'Provides several usefull error classes for common use-cases.'
|
15
|
+
spec.homepage = 'https://github.com/bluemarblepayroll/caution'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.required_ruby_version = '>= 2.3'
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
21
|
+
f.match(%r{^(test|spec|features)/})
|
22
|
+
end
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler'
|
28
|
+
spec.add_development_dependency 'rake'
|
29
|
+
spec.add_development_dependency 'rspec'
|
30
|
+
spec.add_development_dependency 'rubocop'
|
31
|
+
end
|
data/lib/caution.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'caution/version'
|
4
|
+
|
5
|
+
require 'caution/cause_injecting_error'
|
6
|
+
require 'caution/illegal_state_error'
|
7
|
+
|
8
|
+
# Defines the root level module for the gem. See the README.md for an overview.
|
9
|
+
module Caution
|
10
|
+
# Your code goes here...
|
11
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Caution
|
4
|
+
# This provide some context as to which lower level exception casued the error.
|
5
|
+
# Nested exceptions are a relatively new idea to the Ruby ecosystem most things do not
|
6
|
+
# print out the entire exception chain. This works around that by injecting the original
|
7
|
+
# stack trace into the error message.
|
8
|
+
#
|
9
|
+
# Usage:
|
10
|
+
#
|
11
|
+
# begin
|
12
|
+
# # Some code which may raise an exception
|
13
|
+
# end
|
14
|
+
# rescue
|
15
|
+
# raise CauseInjectingError, 'An helpful message'
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# CauseInjectingError will automatically save the original error using the "cause" functionality
|
19
|
+
# of Exception.
|
20
|
+
#
|
21
|
+
# See http://www.virtuouscode.com/2013/12/25/exception-causes-in-ruby-2-1/ for more
|
22
|
+
# information.
|
23
|
+
class CauseInjectingError < StandardError
|
24
|
+
def message
|
25
|
+
if cause
|
26
|
+
"#{super}\n" \
|
27
|
+
"Caused by: #{cause.class}: #{cause.message}\n" \
|
28
|
+
"The original stack trace was:\n" \
|
29
|
+
"#{cause.backtrace.join("\n")}\n" \
|
30
|
+
"END original stack trace \n\n"
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Caution
|
4
|
+
# An error to raise when an object or system is in an unsupported state.
|
5
|
+
# Inspired by Java's IllegalStateException
|
6
|
+
# (https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/IllegalStateException.html)
|
7
|
+
# and .Net's InvalidOperationException
|
8
|
+
# (https://docs.microsoft.com/en-us/dotnet/api/system.invalidoperationexception?view=netframework-4.7.2).
|
9
|
+
#
|
10
|
+
# Note that this is named IllegalStateError and not IllegalStateException as it inherits from
|
11
|
+
# StandardError and all of the classes which inherit from StandardError in the Ruby Standard
|
12
|
+
# Library end in Error.
|
13
|
+
class IllegalStateError < StandardError
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: caution
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Gerry
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-12 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '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'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Provides several usefull error classes for common use-cases.
|
70
|
+
email:
|
71
|
+
- rgerry@bluemarblepayroll.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CHANGELOG.md
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- LICENSE
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/console
|
87
|
+
- bin/setup
|
88
|
+
- caution.gemspec
|
89
|
+
- lib/caution.rb
|
90
|
+
- lib/caution/cause_injecting_error.rb
|
91
|
+
- lib/caution/illegal_state_error.rb
|
92
|
+
- lib/caution/version.rb
|
93
|
+
homepage: https://github.com/bluemarblepayroll/caution
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '2.3'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.5.2.3
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: Essential general purpose Ruby error classes.
|
117
|
+
test_files: []
|