awesome_errors 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0951933e2b1086c2892f460f6b9638ef8efe84ad46c1e6ac7dc6c24a466ba948'
4
+ data.tar.gz: d3e4e426890d0bafa9abca9cff9582cf774c21c66803d32175597c3496a212a8
5
+ SHA512:
6
+ metadata.gz: 70517d7f974670e8d3f77fabacd2ac4d95b2c37cf33e77273a3206fdb373f4c219d0ef5b1363254b54658f4f082b793109e7576584ce5a4cce4df4b7d2ae977c
7
+ data.tar.gz: 7688f227d3b823506f3abd43f3298b43313baf9bf5692e967217a7e1db8840013dee70bc35c0348fafce0b007eb0f5b46e912d838a5807cad02ce16c94b91fd2
data/.byebug_history ADDED
@@ -0,0 +1,9 @@
1
+ exit
2
+ c
3
+ @errors.size
4
+ @errors << error
5
+ @errors << errpr
6
+ @errors.size
7
+ errors.first == error
8
+ errors.first
9
+ error
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,39 @@
1
+ inherit_mode:
2
+ merge:
3
+ - Exclude
4
+
5
+ require:
6
+ - rubocop-performance
7
+
8
+ inherit_from:
9
+ - .rubocop_rspec.yml
10
+
11
+ AllCops:
12
+ NewCops: enable
13
+ SuggestExtensions: false
14
+ TargetRubyVersion: 2.7.2
15
+
16
+ Style/Documentation:
17
+ Enabled: false
18
+
19
+ Style/StringLiterals:
20
+ Enabled: true
21
+ EnforcedStyle: double_quotes
22
+
23
+ Style/StringLiteralsInInterpolation:
24
+ Enabled: true
25
+ EnforcedStyle: double_quotes
26
+
27
+ Layout/LineLength:
28
+ Max: 120
29
+
30
+ Lint/AmbiguousBlockAssociation:
31
+ Exclude:
32
+ - 'spec/**/**/**'
33
+
34
+ Metrics/BlockLength:
35
+ Exclude:
36
+ - 'Rakefile'
37
+ - '**/*.rake'
38
+ - 'spec/**/*.rb'
39
+ - 'awesome_errors.gemspec'
@@ -0,0 +1,15 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ RSpec/MultipleExpectations:
5
+ Enabled: false
6
+
7
+ RSpec/ExampleLength:
8
+ Enabled: false
9
+
10
+ RSpec/Dialect:
11
+ PreferredMethods:
12
+ setup: before
13
+
14
+ RSpec/NestedGroups:
15
+ Max: 6
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in awesome_errors.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem "pry-byebug"
10
+ end
11
+
12
+ group :test do
13
+ gem "rspec-github", require: false
14
+ gem "simplecov", "~> 0.21.2", require: false
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ awesome_errors (0.1.0)
5
+ zeitwerk (~> 2.5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ byebug (11.1.3)
12
+ coderay (1.1.3)
13
+ diff-lcs (1.5.0)
14
+ docile (1.4.0)
15
+ json (2.6.2)
16
+ method_source (1.0.0)
17
+ parallel (1.22.1)
18
+ parser (3.1.2.1)
19
+ ast (~> 2.4.1)
20
+ pry (0.13.1)
21
+ coderay (~> 1.1)
22
+ method_source (~> 1.0)
23
+ pry-byebug (3.9.0)
24
+ byebug (~> 11.0)
25
+ pry (~> 0.13.0)
26
+ rainbow (3.1.1)
27
+ rake (12.3.3)
28
+ regexp_parser (2.5.0)
29
+ rexml (3.2.5)
30
+ rspec (3.11.0)
31
+ rspec-core (~> 3.11.0)
32
+ rspec-expectations (~> 3.11.0)
33
+ rspec-mocks (~> 3.11.0)
34
+ rspec-core (3.11.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-expectations (3.11.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.11.0)
39
+ rspec-github (2.3.1)
40
+ rspec-core (~> 3.0)
41
+ rspec-mocks (3.11.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.11.0)
44
+ rspec-support (3.11.0)
45
+ rubocop (1.35.1)
46
+ json (~> 2.3)
47
+ parallel (~> 1.10)
48
+ parser (>= 3.1.2.1)
49
+ rainbow (>= 2.2.2, < 4.0)
50
+ regexp_parser (>= 1.8, < 3.0)
51
+ rexml (>= 3.2.5, < 4.0)
52
+ rubocop-ast (>= 1.20.1, < 2.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 3.0)
55
+ rubocop-ast (1.21.0)
56
+ parser (>= 3.1.1.0)
57
+ rubocop-performance (1.14.3)
58
+ rubocop (>= 1.7.0, < 2.0)
59
+ rubocop-ast (>= 0.4.0)
60
+ rubocop-rspec (2.12.1)
61
+ rubocop (~> 1.31)
62
+ ruby-progressbar (1.11.0)
63
+ simplecov (0.21.2)
64
+ docile (~> 1.1)
65
+ simplecov-html (~> 0.11)
66
+ simplecov_json_formatter (~> 0.1)
67
+ simplecov-html (0.12.3)
68
+ simplecov_json_formatter (0.1.4)
69
+ unicode-display_width (2.2.0)
70
+ zeitwerk (2.6.0)
71
+
72
+ PLATFORMS
73
+ x86_64-darwin-19
74
+ x86_64-linux
75
+
76
+ DEPENDENCIES
77
+ awesome_errors!
78
+ bundler (~> 2.3)
79
+ pry-byebug
80
+ rake (~> 12.3, >= 12.3.3)
81
+ rspec (~> 3.0)
82
+ rspec-github
83
+ rubocop (~> 1.7)
84
+ rubocop-performance
85
+ rubocop-rspec
86
+ simplecov (~> 0.21.2)
87
+
88
+ BUNDLED WITH
89
+ 2.3.8
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Rui Freitas
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,96 @@
1
+ # AwesomeErrors
2
+
3
+ Easily add errors to your service objects and classes. `AwesomeErrors` is inspired by the `ActiveModel::Errors` API, but ensures that each error contains an error `code` attribute and is not concerned with translating error messages.
4
+
5
+ ## Installation
6
+
7
+ Install from RubyGems by adding it to your `Gemfile`, then bundling.
8
+
9
+ ```ruby
10
+ # Gemfile
11
+ gem 'awesome_errors'
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ Include `AwesomeErrors` in your class:
17
+
18
+ ```ruby
19
+ class ApplicationService
20
+ include AwesomeErrors
21
+
22
+ # ...
23
+ end
24
+
25
+ service = ApplicationService.new
26
+ service.errors # => []
27
+ ```
28
+
29
+ or set up the errors object manually
30
+
31
+ ```ruby
32
+ class ApplicationService
33
+ def errors
34
+ @errors ||= AwesomeErrors::Errors.new
35
+ end
36
+ # ...
37
+ end
38
+
39
+ service = ApplicationService.new
40
+ service.errors # => []
41
+ ```
42
+
43
+ By default, errors are added with the an `:invalid` code and `"is invalid"` message:
44
+
45
+ ```ruby
46
+ errors = AwesomeErrors::Errors.new
47
+ errors.add(:name)
48
+ errors.first.key # => :name
49
+ errors.first.code # => :invalid
50
+ errors.first.message # => "is invalid"
51
+ ```
52
+
53
+ But you can specify a custom `code`, `message` and `metadata`
54
+
55
+ ```ruby
56
+ errors = AwesomeErrors::Errors.new
57
+ errors.add(:name, code: :too_long, message: "is too long", metadata: { line: 1, column: 6 })
58
+ errors.first.key # => :name
59
+ errors.first.code # => :too_long
60
+ errors.first.message # => "is too long"
61
+ errors.first.metadata # => { line: 1, column: 6 }
62
+ ```
63
+
64
+ ## Example
65
+
66
+ ```ruby
67
+ class SendApiRequest
68
+ include AwesomeErrors
69
+
70
+ def call
71
+ if api_response.success?
72
+ # do something
73
+ else
74
+ errors.add(:api, code: :failed_request, message: "failed request")
75
+ end
76
+ end
77
+ end
78
+ ```
79
+
80
+ ## Why this gem?
81
+
82
+ I have used `ActiveModel::Errors` in many service objects and Ruby classes and enjoy its API, but often I am forced to add methods related to the translation of messages that I wouldn't need. Also, `ActiveModel::Errors` allows you to add errors where the message will be used as the error `type`, and I wanted to ensure a more consistent experience of always having an error `code` that is `Symbol` or an `Integer` so that services and objects can exchange errors while being able to rely on the `code` to better construct a user-facing error message.
83
+
84
+ ## Development
85
+
86
+ 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.
87
+
88
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/awesome_errors.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwesomeErrors
4
+ class Error
5
+ attr_reader :key, :code, :message, :metadata
6
+
7
+ def initialize(key:, code: :invalid, message: "is invalid", metadata: {})
8
+ raise ArgumentError unless code.is_a?(Symbol) || code.is_a?(Integer)
9
+
10
+ @key = key
11
+ @code = code
12
+ @message = message
13
+ @metadata = metadata
14
+
15
+ freeze
16
+ end
17
+
18
+ def full_message(include_key: true)
19
+ if include_key
20
+ [key, message].compact.join(" ")
21
+ else
22
+ message
23
+ end
24
+ end
25
+
26
+ def to_hash(full_message: false)
27
+ message_method = full_message ? :full_message : :message
28
+ {
29
+ key: key,
30
+ code: code,
31
+ message: public_send(message_method),
32
+ metadata: metadata
33
+ }
34
+ end
35
+
36
+ def ==(other)
37
+ return false unless other.is_a?(AwesomeErrors::Error)
38
+
39
+ to_hash == other.to_hash
40
+ end
41
+
42
+ def eql?(other)
43
+ self == other
44
+ end
45
+
46
+ def hash
47
+ to_hash.hash
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module AwesomeErrors
6
+ class Errors
7
+ include Enumerable
8
+
9
+ extend Forwardable
10
+
11
+ def_delegators :@errors, :each, :clear, :empty?, :size, :uniq!
12
+
13
+ attr_reader :errors
14
+
15
+ def initialize
16
+ @errors = Set.new
17
+ end
18
+
19
+ def [](key)
20
+ @errors.filter_map { |error| error.message if error.key == key }
21
+ end
22
+
23
+ def keys
24
+ @errors.map(&:key).uniq.freeze
25
+ end
26
+
27
+ def add(key, **options)
28
+ error = Error.new(key: key.to_sym, **options)
29
+ @errors << error
30
+
31
+ error
32
+ end
33
+
34
+ def merge!(other)
35
+ other.errors.each do |error|
36
+ import(error)
37
+ end
38
+ end
39
+
40
+ def import(error)
41
+ @errors << error
42
+ end
43
+
44
+ def as_json(options = {})
45
+ to_hash(full_messages: options[:full_messages] || false)
46
+ end
47
+
48
+ def to_hash(full_messages: false)
49
+ message_method = full_messages ? :full_message : :message
50
+ group_by_key.transform_values do |err_objects|
51
+ err_objects.map(&message_method)
52
+ end
53
+ end
54
+
55
+ def group_by_key
56
+ errors.group_by(&:key)
57
+ end
58
+
59
+ def messages
60
+ hash = to_hash
61
+ hash.default = [].freeze
62
+ hash.freeze
63
+ hash
64
+ end
65
+
66
+ def messages_for(key)
67
+ @errors.filter_map { |error| error.message if error.key == key }
68
+ end
69
+
70
+ def full_messages
71
+ errors.map(&:full_message)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AwesomeErrors
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+
5
+ Zeitwerk::Loader.for_gem.tap do |loader|
6
+ loader.ignore(
7
+ "#{__dir__}/awesome_errors/version.rb"
8
+ )
9
+ end.setup
10
+
11
+ module AwesomeErrors
12
+ require_relative "awesome_errors/version"
13
+
14
+ def self.included(base)
15
+ base.include(InstanceMethods)
16
+ end
17
+
18
+ module InstanceMethods
19
+ def errors
20
+ @errors ||= AwesomeErrors::Errors.new
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: awesome_errors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rui Freitas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zeitwerk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.3'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 12.3.3
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '12.3'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 12.3.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rubocop
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.7'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.7'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop-performance
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-rspec
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: AwesomeErrors is a simply way to add errors to your Ruby objects and
118
+ classes.
119
+ email:
120
+ - rui.ferreira.freitas@gmail.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".byebug_history"
126
+ - ".rspec"
127
+ - ".rubocop.yml"
128
+ - ".rubocop_rspec.yml"
129
+ - CHANGELOG.md
130
+ - Gemfile
131
+ - Gemfile.lock
132
+ - LICENSE.txt
133
+ - README.md
134
+ - Rakefile
135
+ - lib/awesome_errors.rb
136
+ - lib/awesome_errors/error.rb
137
+ - lib/awesome_errors/errors.rb
138
+ - lib/awesome_errors/version.rb
139
+ homepage: https://github.com/rodloboz/awesome_errors
140
+ licenses:
141
+ - MIT
142
+ metadata:
143
+ homepage_uri: https://github.com/rodloboz/awesome_errors
144
+ source_code_uri: https://github.com/rodloboz/awesome_errors
145
+ changelog_uri: https://github.com/rodloboz/awesome_errors/blob/main/CHANGELOG.md
146
+ rubygems_mfa_required: 'true'
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: 2.7.0
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubygems_version: 3.1.4
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: Easily add errors to your service objects and Ruby classes.
166
+ test_files: []