business_error 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/business_error.gemspec +2 -0
- data/lib/business_error.rb +40 -20
- data/lib/business_error/config.rb +5 -7
- data/lib/business_error/error.rb +10 -3
- data/lib/business_error/version.rb +3 -1
- metadata +16 -3
- data/Gemfile.lock +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16354b17905dbbf9aa01fc5e27b684397e0c57f4
|
4
|
+
data.tar.gz: d5f4827588df4529019a69c3b6be71f4ba0798e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b498e72e19e195a50da503f2eb970c1eaebd036e35318a3b8efef0cf90d42d98100c5e867d3f0587c1ea33b3979234ddb35455e63ff7165a4ed7f7f5903e4734
|
7
|
+
data.tar.gz: e9de33d63b68499e5058096329e81b25f938bdbfaec8e83b25c66a134d5bc23ecc8e29ab5de56e4884aba2b07e73ebe3c564922d253ccfad00f8f09304f42752
|
data/.gitignore
CHANGED
data/business_error.gemspec
CHANGED
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
|
+
spec.add_dependency "request_store"
|
25
|
+
|
24
26
|
spec.add_development_dependency "bundler", "~> 1.16"
|
25
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
28
|
spec.add_development_dependency "rspec", "~> 3.0"
|
data/lib/business_error.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'request_store'
|
3
|
+
|
1
4
|
require 'business_error/version'
|
2
5
|
require 'business_error/error'
|
3
6
|
require 'business_error/config'
|
@@ -6,12 +9,18 @@ module BusinessError
|
|
6
9
|
cattr_accessor(:defs_tree) { { } }
|
7
10
|
attr_accessor :defs
|
8
11
|
|
9
|
-
def mattr_reader name,
|
10
|
-
|
12
|
+
def mattr_reader name,
|
13
|
+
message = name.to_s.humanize,
|
14
|
+
code = _get_code,
|
15
|
+
http: _get_http,
|
16
|
+
group: _get_group,
|
17
|
+
format: @format
|
18
|
+
define_singleton_method(name) do |locale = _get_locale|
|
19
|
+
msg = message.is_a?(Hash) ? (message[locale] || message[:en]) : message
|
20
|
+
Error.new(name, msg, code, http, format)
|
21
|
+
end
|
11
22
|
|
12
|
-
define_singleton_method(name) {
|
13
|
-
# TODO raise Error, name, message, code
|
14
|
-
define_singleton_method("#{name}!") { raise Error.new(name, message, code, http) }
|
23
|
+
define_singleton_method("#{name}!") { send(name).throw! }
|
15
24
|
|
16
25
|
defs_tree[self.name] ||= { }
|
17
26
|
(defs_tree[self.name][group] ||= [ ]) << { name: name, msg: message, code: code, http: http }
|
@@ -20,20 +29,11 @@ module BusinessError
|
|
20
29
|
|
21
30
|
alias_method :define, :mattr_reader
|
22
31
|
|
23
|
-
def group group_name = :private, code_start_at = @code, http: _get_http, &block
|
24
|
-
@group_name, @code, @http,
|
32
|
+
def group group_name = :private, code_start_at = @code, http: _get_http, format: @format, &block
|
33
|
+
@group_name, @code, @http, @format, group_name, code_start_at, http, format =
|
34
|
+
group_name, code_start_at, http, format, @group_name, @code, @http, @format
|
25
35
|
instance_eval(&block)
|
26
|
-
@group_name, @code, @http = group_name, code_start_at, http
|
27
|
-
end
|
28
|
-
|
29
|
-
def _get_group
|
30
|
-
@group_name || :public
|
31
|
-
end
|
32
|
-
|
33
|
-
def _get_code
|
34
|
-
raise ArgumentError, 'Should give a code to define your business error' if (code = @code).nil?
|
35
|
-
@code = @code < 0 ? (code - 1) : (code + 1)
|
36
|
-
code
|
36
|
+
@group_name, @code, @http, @format = group_name, code_start_at, http, format
|
37
37
|
end
|
38
38
|
|
39
39
|
def code_start_at code
|
@@ -44,8 +44,8 @@ module BusinessError
|
|
44
44
|
@http_status = status_code
|
45
45
|
end
|
46
46
|
|
47
|
-
def
|
48
|
-
@
|
47
|
+
def format template
|
48
|
+
@format = template
|
49
49
|
end
|
50
50
|
|
51
51
|
def define_px name, message = '', code = _get_code, http: _get_http
|
@@ -72,4 +72,24 @@ module BusinessError
|
|
72
72
|
def all
|
73
73
|
puts defs_tree.stringify_keys.to_yaml.gsub(' :', ' ')
|
74
74
|
end
|
75
|
+
|
76
|
+
# ===
|
77
|
+
|
78
|
+
def _get_group
|
79
|
+
@group_name || :public
|
80
|
+
end
|
81
|
+
|
82
|
+
def _get_code
|
83
|
+
raise ArgumentError, 'Should give a code to define your business error' if (code = @code).nil?
|
84
|
+
@code = @code < 0 ? (code - 1) : (code + 1)
|
85
|
+
code
|
86
|
+
end
|
87
|
+
|
88
|
+
def _get_http
|
89
|
+
@http_status || Config.default_http_status
|
90
|
+
end
|
91
|
+
|
92
|
+
def _get_locale
|
93
|
+
RequestStore.store[:err_locale] ||= 'en'
|
94
|
+
end
|
75
95
|
end
|
@@ -1,11 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module BusinessError
|
2
4
|
class Config
|
3
|
-
cattr_accessor :formats
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
cattr_accessor :default_http_status do
|
8
|
-
200
|
9
|
-
end
|
5
|
+
cattr_accessor :formats, default: { }
|
6
|
+
cattr_accessor :default_format, default: nil
|
7
|
+
cattr_accessor :default_http_status, default: 200
|
10
8
|
end
|
11
9
|
end
|
data/lib/business_error/error.rb
CHANGED
@@ -1,18 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'business_error/config'
|
2
4
|
|
3
5
|
module BusinessError
|
4
6
|
class Error < StandardError
|
5
|
-
attr_accessor :name, :msg, :code, :http_status
|
7
|
+
attr_accessor :name, :msg, :code, :http_status, :format
|
6
8
|
|
7
|
-
def initialize(name, msg, code, http_status = Config.default_http_status)
|
9
|
+
def initialize(name, msg, code, http_status = Config.default_http_status, format = Config.default_format)
|
8
10
|
msg = name.to_s.humanize if msg.blank?
|
9
11
|
@name, @msg, @code, @http_status = name, msg, code, http_status
|
12
|
+
self.format = format
|
10
13
|
end
|
11
14
|
|
12
15
|
def info
|
13
16
|
@info ||= { code: @code, msg: @msg, http: @http_status }
|
14
17
|
end
|
15
18
|
|
19
|
+
def throw!
|
20
|
+
format ? format!(format) : (raise self)
|
21
|
+
end
|
22
|
+
|
16
23
|
def format!(template, **addition_content)
|
17
24
|
content = Config.formats[template].each_with_index.map { |k, i| [k, info.values[i]] }.to_h
|
18
25
|
@info = { only: content.merge(addition_content) }
|
@@ -22,7 +29,7 @@ module BusinessError
|
|
22
29
|
alias render! format!
|
23
30
|
|
24
31
|
def with!(**addition_content)
|
25
|
-
info.merge!(addition_content)
|
32
|
+
info.merge!(data: addition_content)
|
26
33
|
raise self
|
27
34
|
end
|
28
35
|
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: business_error
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zhandao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: request_store
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,7 +78,6 @@ files:
|
|
64
78
|
- ".travis.yml"
|
65
79
|
- CODE_OF_CONDUCT.md
|
66
80
|
- Gemfile
|
67
|
-
- Gemfile.lock
|
68
81
|
- LICENSE.txt
|
69
82
|
- README.md
|
70
83
|
- Rakefile
|
data/Gemfile.lock
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
business_error (1.0.2)
|
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.6
|