business_error 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +233 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/business_error.gemspec +27 -0
- data/lib/business_error/config.rb +11 -0
- data/lib/business_error/error.rb +31 -0
- data/lib/business_error/version.rb +3 -0
- data/lib/business_error.rb +83 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 53723454814f8dbe82e668cae8fa2c2eb22bf5f4
|
4
|
+
data.tar.gz: a4d9f799ade0725f769c291c035ac9d5e67bf434
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e614af109c9ee257592d23d2f56c04fccf332aef695d3ba0571f7e8462ee5a7b55fd1a6050471dc647c83aa90d590408f503eef9473fcf9d20c789fb82f8bbb6
|
7
|
+
data.tar.gz: 51316491a4169abe2fb52402812e00eb1df0b971882d4144555c28bb9e00fcd83a981bcba30eb4d5fcc290c775efbd7f150d5d44d26bdf4e60cf96a1540b19af
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at xy@xiayouwang.net. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
business_error (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.7.0)
|
12
|
+
rspec-core (~> 3.7.0)
|
13
|
+
rspec-expectations (~> 3.7.0)
|
14
|
+
rspec-mocks (~> 3.7.0)
|
15
|
+
rspec-core (3.7.1)
|
16
|
+
rspec-support (~> 3.7.0)
|
17
|
+
rspec-expectations (3.7.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.7.0)
|
20
|
+
rspec-mocks (3.7.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.7.0)
|
23
|
+
rspec-support (3.7.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
business_error!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 zhandao
|
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,233 @@
|
|
1
|
+
# BusinessError
|
2
|
+
|
3
|
+
Business Error Management by using OOP
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'business_error'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### 1. Config file
|
20
|
+
|
21
|
+
initializer `business_error.rb`
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
BusinessError::Config.tap do |config|
|
25
|
+
config.default_http_status = 200
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
### 2. About `BusinessError::Error`
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# new an error
|
33
|
+
e = BusinessError::Error.new(
|
34
|
+
name = :invalid_token,
|
35
|
+
msg = 'your token is invalid',
|
36
|
+
code = 1001,
|
37
|
+
http_status = 400 # it is optional
|
38
|
+
)
|
39
|
+
|
40
|
+
e.info # => { code: 1001, msg: '...', http: 400 }
|
41
|
+
e.message # => "{ code: 1001, msg: '...', http: 400 }"
|
42
|
+
```
|
43
|
+
|
44
|
+
### 3. Define Error
|
45
|
+
|
46
|
+
#### 3.a Recommended Practice
|
47
|
+
|
48
|
+
1. Create new directories `_docs/error` in the `/app` directory.
|
49
|
+
|
50
|
+
2. Add `config.eager_load_paths << "#{Rails.root}/app/_docs"` to your `application.rb`.
|
51
|
+
|
52
|
+
3. Create a base error class, like `api.rb`:
|
53
|
+
```ruby
|
54
|
+
# app/_docs/error/api.rb
|
55
|
+
class Error::Api
|
56
|
+
extend BusinessError
|
57
|
+
|
58
|
+
define :invalid_token, 'Your token is invalid', 1001
|
59
|
+
end
|
60
|
+
```
|
61
|
+
The class method `define` will define an `BusinessError::Error` by the given
|
62
|
+
name, msg and code.
|
63
|
+
|
64
|
+
4. Now you can create more error class inherited from `Error::Api`:
|
65
|
+
```ruby
|
66
|
+
class Error::Foo < Error::Api
|
67
|
+
define :bar, 'bar', 2002
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
And then, try it in your console!
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
Error::Api.invalid_token # => an instance of BusinessError::Error initialized by the given params
|
75
|
+
Error::Foo.invalid_token # Yes, this error comes from inheritance!
|
76
|
+
Error::Foo.bar # This error is defined by itself
|
77
|
+
|
78
|
+
# How to raise an error? -- Bang with the same name
|
79
|
+
Error::Api.invalid_token! # => will raise an BusinessError::Error with defined message
|
80
|
+
|
81
|
+
# Methods for getting all of error definitions
|
82
|
+
# Get the error class's error definitions
|
83
|
+
Error::Api.print # It will print a YAML for showing it's groups and their error definitions
|
84
|
+
# Get this error class AND it's ancestors and descendants' error definition
|
85
|
+
Error::Api.tree # YAML also
|
86
|
+
```
|
87
|
+
|
88
|
+
#### 3.b Preventing error definition inheritance via grouping them
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
# Error::Api
|
92
|
+
group :group_name do
|
93
|
+
define :foo, 'foo', 1
|
94
|
+
define :bar, 'bar', 2
|
95
|
+
end
|
96
|
+
# Then
|
97
|
+
Error::Api.foo # ok
|
98
|
+
Error::Foo.foo # NoMethodError!
|
99
|
+
|
100
|
+
# method signature
|
101
|
+
group group_name = :private, code_start_at = nil, http: 200, &block
|
102
|
+
```
|
103
|
+
|
104
|
+
#### 3.c Using the same name to define?
|
105
|
+
|
106
|
+
NOT supported currently. It leads to method override,
|
107
|
+
the last definition will leave.
|
108
|
+
|
109
|
+
#### 3.x Skills
|
110
|
+
|
111
|
+
1. Use `mattr_reader` instead of `define` (alias) IF you're using Rubymine.
|
112
|
+
|
113
|
+
It makes Rubymine auto completion more perfect.
|
114
|
+
|
115
|
+
2. Use `define_px` (define an error and group it into the group named that the prefix of error name)
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
define_px :create_failed, '', -1
|
119
|
+
# the same as below:
|
120
|
+
group :create do
|
121
|
+
define :create_failed, '', -1
|
122
|
+
end
|
123
|
+
# or
|
124
|
+
define :create_failed, '', -1, group: :create # or call `mattr_reader`
|
125
|
+
```
|
126
|
+
|
127
|
+
3. Passing blank message:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
define :create_failed, '', -1
|
131
|
+
# then, the message of this error will be:
|
132
|
+
:create_failed.to_s.humanize
|
133
|
+
```
|
134
|
+
|
135
|
+
4. `code_start_at`
|
136
|
+
|
137
|
+
```
|
138
|
+
code_start_at 0
|
139
|
+
define ... # code is 0
|
140
|
+
define ... # code is 1
|
141
|
+
define ... # code is 2
|
142
|
+
|
143
|
+
code_start_at -1
|
144
|
+
define ... # code is -1
|
145
|
+
define ... # code is -2
|
146
|
+
define ... # code is -3
|
147
|
+
```
|
148
|
+
|
149
|
+
5. `http`
|
150
|
+
|
151
|
+
```
|
152
|
+
http 500
|
153
|
+
define ... # http is 500
|
154
|
+
define ... # http is 500
|
155
|
+
|
156
|
+
http :forbidden
|
157
|
+
define ... # http is :forbidden (403)
|
158
|
+
```
|
159
|
+
|
160
|
+
### 4. Raise Error
|
161
|
+
|
162
|
+
Just: error_name + bang!
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
Error::Api.invalid_token! # => BusinessError::Error! with invalid_token's message
|
166
|
+
```
|
167
|
+
|
168
|
+
#### 4.a `with!` for error info customization
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
Error::Api.invalid_token.with!(hello: 'world')
|
172
|
+
# it will raise an invalid_token error with info:
|
173
|
+
# { code: 1001, msg: '...', http: 400, hello: 'world' }
|
174
|
+
```
|
175
|
+
|
176
|
+
#### 4.b `format!` in order to be compatible with different info format requirements
|
177
|
+
|
178
|
+
error.info have a hash format defaults to:
|
179
|
+
`{ code: @code, msg: @msg, http: @http_status }`
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
# Suppose we need a format called "old"
|
183
|
+
# initializer
|
184
|
+
config.formats[:old] = %i[ status message http ]
|
185
|
+
|
186
|
+
# If:
|
187
|
+
Error::Api.invalid_token.format!(:old)
|
188
|
+
# it will raise an invalid_token error with info:
|
189
|
+
# { only: { status: 1001, message: '...', http: 400 } }
|
190
|
+
|
191
|
+
Error::Api.invalid_token.format!(:old, hello: 'world') # it's ok
|
192
|
+
```
|
193
|
+
|
194
|
+
the key `only` is for [`output`]()
|
195
|
+
|
196
|
+
More complex formatting is to be done:
|
197
|
+
```ruby
|
198
|
+
config.formats[:old] = { format: {
|
199
|
+
status: 0,
|
200
|
+
foo: {
|
201
|
+
bar: 'default value',
|
202
|
+
msg: 'success'
|
203
|
+
},
|
204
|
+
http: 200
|
205
|
+
}, code: [:status], msg: [:foo, :msg], http: :http }
|
206
|
+
```
|
207
|
+
|
208
|
+
`format!` has an alias `render!`
|
209
|
+
|
210
|
+
### 5. Rescue Error and render response by [`OutPut`]()
|
211
|
+
|
212
|
+
Just do:
|
213
|
+
```ruby
|
214
|
+
output Error::Api.invalid_token
|
215
|
+
```
|
216
|
+
|
217
|
+
## Development
|
218
|
+
|
219
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
220
|
+
|
221
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
222
|
+
|
223
|
+
## Contributing
|
224
|
+
|
225
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/business_error. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
226
|
+
|
227
|
+
## License
|
228
|
+
|
229
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
230
|
+
|
231
|
+
## Code of Conduct
|
232
|
+
|
233
|
+
Everyone interacting in the BusinessError project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/business_error/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "business_error"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "business_error/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "business_error"
|
8
|
+
spec.version = BusinessError::VERSION
|
9
|
+
spec.authors = ["zhandao"]
|
10
|
+
spec.email = ["x@skippingcat.com"]
|
11
|
+
|
12
|
+
spec.summary = 'Business Error Management by using OOP'
|
13
|
+
spec.description = 'Business Error Management by using OOP'
|
14
|
+
spec.homepage = "http://github.com"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'business_error/config'
|
2
|
+
|
3
|
+
module BusinessError
|
4
|
+
class Error < StandardError
|
5
|
+
attr_accessor :name, :msg, :code, :http_status
|
6
|
+
|
7
|
+
def initialize(name, msg, code, http_status = 200)
|
8
|
+
msg = name.to_s.humanize if msg.blank?
|
9
|
+
@name, @msg, @code, @http_status = name, msg, code, http_status
|
10
|
+
end
|
11
|
+
|
12
|
+
def info
|
13
|
+
@info ||= { code: @code, msg: @msg, http: @http_status }
|
14
|
+
end
|
15
|
+
|
16
|
+
def format!(template, **addition_content)
|
17
|
+
content = Config.formats[template].each_with_index.map { |k, i| [k, info.values[i]] }.to_h
|
18
|
+
@info = { only: content.merge(addition_content) }
|
19
|
+
raise self
|
20
|
+
end
|
21
|
+
|
22
|
+
alias render! format!
|
23
|
+
|
24
|
+
def with!(**addition_content)
|
25
|
+
info.merge!(addition_content)
|
26
|
+
raise self
|
27
|
+
end
|
28
|
+
|
29
|
+
def message; info.to_s end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'business_error/version'
|
2
|
+
require 'business_error/error'
|
3
|
+
require 'business_error/config'
|
4
|
+
|
5
|
+
module BusinessError
|
6
|
+
cattr_accessor :defs_tree do
|
7
|
+
{ }
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :defs
|
11
|
+
|
12
|
+
def mattr_reader name, message = '', code = _get_code, http: _get_http, group: _get_group
|
13
|
+
message = name.to_s.humanize if message.blank?
|
14
|
+
|
15
|
+
define_singleton_method(name) { Error.new(name, message, code, http) }
|
16
|
+
# TODO raise Error, name, message, code
|
17
|
+
define_singleton_method("#{name}!") { raise Error.new(name, message, code, http) }
|
18
|
+
|
19
|
+
info = { name: name, msg: message, code: code, http: http }
|
20
|
+
if group == :public
|
21
|
+
(defs_tree[:public] ||= [ ]) << info
|
22
|
+
else
|
23
|
+
((defs_tree[self.name] ||= { })[group] ||= [ ]) << info
|
24
|
+
((@defs ||= { })[group] ||= []) << name
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method :define, :mattr_reader
|
29
|
+
|
30
|
+
def group group_name = :private, code_start_at = nil, http: 200, &block
|
31
|
+
@group_name, @code, @http, group_name, code_start_at, http = group_name, code_start_at, http, @group_name, @code, @http
|
32
|
+
instance_eval(&block)
|
33
|
+
@group_name, @code, @http, group_name, code_start_at, http = group_name, code_start_at, http, @group_name, @code, @http
|
34
|
+
end
|
35
|
+
|
36
|
+
def _get_group
|
37
|
+
@group_name || :public
|
38
|
+
end
|
39
|
+
|
40
|
+
def _get_code
|
41
|
+
code = @code
|
42
|
+
raise ArgumentError, 'Should give a code to define your business error' if code.nil?
|
43
|
+
@code = @code < 0 ? (code - 1) : (code + 1)
|
44
|
+
code
|
45
|
+
end
|
46
|
+
|
47
|
+
def code_start_at code
|
48
|
+
@code = code
|
49
|
+
end
|
50
|
+
|
51
|
+
def http status_code
|
52
|
+
@http_status = status_code
|
53
|
+
end
|
54
|
+
|
55
|
+
def _get_http
|
56
|
+
@http_status || Config.default_http_status
|
57
|
+
end
|
58
|
+
|
59
|
+
def define_px name, message = '', code = _get_code, http: _get_http
|
60
|
+
group_name = name.to_s.split('_').first.to_sym
|
61
|
+
group group_name do
|
62
|
+
mattr_reader name, message, code, http: http, group: group_name
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def inherited(subclass)
|
67
|
+
defs_tree[self.name]&.keys&.each do |group|
|
68
|
+
defs_tree[self.name][group].each do |name:, **_|
|
69
|
+
# TODO: how to undef class method?
|
70
|
+
subclass.define_singleton_method(name) { raise NoMethodError }
|
71
|
+
subclass.define_singleton_method(name.to_s + '!') { raise NoMethodError }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def print
|
77
|
+
puts defs_tree[self.name].stringify_keys.to_yaml.gsub(' :', ' ')
|
78
|
+
end
|
79
|
+
|
80
|
+
def all
|
81
|
+
puts defs_tree.stringify_keys.to_yaml.gsub(' :', ' ')
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: business_error
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- zhandao
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-14 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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
|
+
description: Business Error Management by using OOP
|
56
|
+
email:
|
57
|
+
- x@skippingcat.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- business_error.gemspec
|
74
|
+
- lib/business_error.rb
|
75
|
+
- lib/business_error/config.rb
|
76
|
+
- lib/business_error/error.rb
|
77
|
+
- lib/business_error/version.rb
|
78
|
+
homepage: http://github.com
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.6.12
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Business Error Management by using OOP
|
102
|
+
test_files: []
|