jsonapi_errors_handler 0.1.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df8269ff040273b0368d86c043bf1cd50fb29503be3d4b5207ffba0aa89d27b8
4
- data.tar.gz: 7d85f4979969cf3e914145633c11abc0c6d3ff325671bbe788c614ebb5292d59
3
+ metadata.gz: ae6d9355a82a2ff27bf1681dba4d37579d5018da3fd6f219305b5169b18541d1
4
+ data.tar.gz: 8e06b88766e7c18ebf9f9a12f145edbee8cf60e357d5c464f7df4dac45f75b3a
5
5
  SHA512:
6
- metadata.gz: 0d18a2f65e6a93525ec2c45a04714efd01fc8894a87f4d54ffce2652e73c6320ae5ed5447db4499c085a4e335a9162186c1a757d8a6ef6fb8c96bf0c7e6fc625
7
- data.tar.gz: 2a3491ecaa2b1c1fcffdbc4d6bcaa430fb80c77c6daa9af55121b4d2ff5532b14781fb07c9a174aeec0e17599700fd50f3629988f99501c1ff0d0f23773186dd
6
+ metadata.gz: 5f47b8542ca61fd1f331c010f5aa7e9d94e9105d2808a97c5464ae49d3152add962da1126bd1b74cf8d88ecc67747319b458d6fa3f3781128194a788d7ec3b10
7
+ data.tar.gz: f67a3b5ab34dca5177797ccbf0eeab38319e4cea39e6937eafce62cba21aebe1b4b7aaccf2b0d90c3bd8123bd04512fa7ac538d4a15d2b797079cf62c6bb5014
@@ -0,0 +1,31 @@
1
+ name: Test & Release
2
+
3
+ on:
4
+ push:
5
+ branches: [ release ]
6
+
7
+ jobs:
8
+ build:
9
+ name: Build + Publish
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Set up Ruby 2.7.1
15
+ uses: actions/setup-ruby@v1
16
+ with:
17
+ ruby-version: 2.7.1
18
+ - name: Run tests
19
+ run: |
20
+ bundle install
21
+ bundle exec rspec
22
+ - name: Publish to RubyGems
23
+ run: |
24
+ mkdir -p $HOME/.gem
25
+ touch $HOME/.gem/credentials
26
+ chmod 0600 $HOME/.gem/credentials
27
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
28
+ gem build *.gemspec
29
+ gem push *.gem
30
+ env:
31
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -0,0 +1,29 @@
1
+ name: Run tests
2
+
3
+ on:
4
+ push:
5
+ branches-ignore: [ release ]
6
+ pull_request:
7
+ branches: [ master release ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Test
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby 2.7.1
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: 2.7.1
20
+ - name: Run tests
21
+ run: |
22
+ bundle install
23
+ CI=true bundle exec rspec
24
+ - name: Upload report
25
+ run: |
26
+ mv ./coverage/coverage.xml cobertura.xml
27
+ bash <(curl -Ls https://coverage.codacy.com/get.sh) report
28
+ env:
29
+ CODACY_PROJECT_TOKEN: ${{secrets.CODACY_PROJECT_TOKEN}}
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4.0
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - "spec/**/*_spec.rb"
7
+ - "jsonapi_errors_handler.gemspec"
8
+
9
+ Metrics/ModuleLength:
10
+ Exclude:
11
+ - "spec/**/*_spec.rb"
@@ -1 +1 @@
1
- 2.6.3
1
+ 2.7.1
@@ -1,21 +1,29 @@
1
1
  ## Contributing
2
2
 
3
- Bug reports and pull requests are welcome on GitHub at https://github.com/driggl/jsonapi_errors_handler.
3
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/driggl/jsonapi_errors_handler>.
4
4
 
5
5
  **How to contribute:**
6
6
 
7
- 1. Create Issue on Github.
8
- 2. Fork repository
9
- 3. Install [Rubocop](https://github.com/rubocop-hq/rubocop) - make sure you run it before commiting changes
10
- 4. Commit changes
11
- - Keep commits small and atomic
12
- - Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)
13
- - Keep commits imperative style
14
- - Mention issue number (i.e "Resolves: #3")
15
- 5. Make sure you cover your change with tests.
16
- 6. Create Pull Request
7
+ 1. Create Issue on Github.
8
+
9
+ 2. Fork repository
10
+
11
+ 3. Install [Rubocop](https://github.com/rubocop-hq/rubocop) - make sure you run it before commiting changes
12
+
13
+ 4. Commit changes
14
+ * Keep commits small and atomic
15
+
16
+ * Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)
17
+
18
+ * Keep commits imperative style
19
+
20
+ * Mention issue number (i.e "Resolves: #3")
21
+
22
+ 5. Make sure you cover your change with tests.
23
+
24
+ 6. Create Pull Request
17
25
 
18
26
  **Coding Guidelines**
19
27
 
20
- - We use [Rubocop](https://github.com/rubocop-hq/rubocop) and Ruby style guides for linting `*.rb` files.
21
- - We use [Remark Lint](https://github.com/remarkjs/remark-lint) for linting `*.md` files.
28
+ * We use [Rubocop](https://github.com/rubocop*hq/rubocop) and Ruby style guides for linting `*.rb` files.
29
+ * We use [Remark Lint](https://github.com/remarkjs/remark-lint) for linting `*.md` files.
@@ -1,35 +1,44 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jsonapi_errors_handler (0.1.6)
4
+ jsonapi_errors_handler (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.3)
10
- rake (10.5.0)
11
- rspec (3.8.0)
12
- rspec-core (~> 3.8.0)
13
- rspec-expectations (~> 3.8.0)
14
- rspec-mocks (~> 3.8.0)
15
- rspec-core (3.8.0)
16
- rspec-support (~> 3.8.0)
17
- rspec-expectations (3.8.2)
9
+ diff-lcs (1.4.4)
10
+ docile (1.3.2)
11
+ rake (13.0.1)
12
+ rspec (3.9.0)
13
+ rspec-core (~> 3.9.0)
14
+ rspec-expectations (~> 3.9.0)
15
+ rspec-mocks (~> 3.9.0)
16
+ rspec-core (3.9.2)
17
+ rspec-support (~> 3.9.3)
18
+ rspec-expectations (3.9.2)
18
19
  diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.8.0)
20
- rspec-mocks (3.8.0)
20
+ rspec-support (~> 3.9.0)
21
+ rspec-mocks (3.9.1)
21
22
  diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.8.0)
23
- rspec-support (3.8.0)
23
+ rspec-support (~> 3.9.0)
24
+ rspec-support (3.9.3)
25
+ simplecov (0.18.5)
26
+ docile (~> 1.1)
27
+ simplecov-html (~> 0.11)
28
+ simplecov-cobertura (1.4.0)
29
+ simplecov (~> 0.8)
30
+ simplecov-html (0.12.2)
24
31
 
25
32
  PLATFORMS
26
33
  ruby
27
34
 
28
35
  DEPENDENCIES
29
- bundler (~> 1.16)
36
+ bundler
30
37
  jsonapi_errors_handler!
31
- rake (~> 10.0)
32
- rspec (~> 3.0)
38
+ rake
39
+ rspec
40
+ simplecov
41
+ simplecov-cobertura
33
42
 
34
43
  BUNDLED WITH
35
- 1.17.3
44
+ 2.1.4
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
- [![Codacy Badge](https://api.codacy.com/project/badge/Grade/542259a14a8f4b2894a39850c5031ffa)](https://app.codacy.com/app/swilgosz/jsonapi_errors_handler?utm_source=github.com&utm_medium=referral&utm_content=driggl/jsonapi_errors_handler&utm_campaign=Badge_Grade_Dashboard)
2
1
  [![Gem Version](https://badge.fury.io/rb/jsonapi_errors_handler.svg)](https://badge.fury.io/rb/jsonapi_errors_handler)
3
- [![CircleCI](https://circleci.com/gh/driggl/jsonapi_errors_handler/tree/master.svg?style=svg)](https://circleci.com/gh/driggl/jsonapi_errors_handler/tree/master)
2
+ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/6dba6e4556354789809185e0decde4f8)](https://www.codacy.com/gh/driggl/jsonapi_errors_handler?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=driggl/jsonapi_errors_handler&amp;utm_campaign=Badge_Grade)
3
+ [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/6dba6e4556354789809185e0decde4f8)](https://www.codacy.com/gh/driggl/jsonapi_errors_handler?utm_source=github.com&utm_medium=referral&utm_content=driggl/jsonapi_errors_handler&utm_campaign=Badge_Coverage)
4
+ ![Run tests](https://github.com/driggl/jsonapi_errors_handler/workflows/Run%20tests/badge.svg?branch=master)
4
5
 
5
6
  # JsonapiErrorsHandler
6
7
 
@@ -16,11 +17,15 @@ Add this line to your application's Gemfile:
16
17
 
17
18
  And then execute:
18
19
 
19
- $ bundle
20
+ ```shell
21
+ $ bundle
22
+ ```
20
23
 
21
24
  Or install it yourself as:
22
25
 
23
- $ gem install jsonapi_errors_handler
26
+ ```shell
27
+ $ gem install jsonapi_errors_handler
28
+ ```
24
29
 
25
30
  ## Usage
26
31
 
@@ -36,7 +41,7 @@ From this point you'll have default html errors being serialized. JsonapiErrorsH
36
41
  * [JsonapiErrorsHandler::Errors::Invalid](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/invalid.rb)
37
42
  * [JsonapiErrorsHandler::Errors::Forbidden](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/forbidden.rb)
38
43
  * [JsonapiErrorsHandler::Errors::NotFound](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
39
- * [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
44
+ * [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/unauthorized.rb)
40
45
 
41
46
  If you rise any of errors above in any place of your application, client gets the nicely formatted error message instead of 500
42
47
 
@@ -103,7 +108,7 @@ By default, we deliver hardcoded responses. You can check out the defined error
103
108
  * [JsonapiErrorsHandler::Errors::Invalid](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/invalid.rb)
104
109
  * [JsonapiErrorsHandler::Errors::Forbidden](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/forbidden.rb)
105
110
  * [JsonapiErrorsHandler::Errors::NotFound](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
106
- * [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
111
+ * [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/unauthorized.rb)
107
112
 
108
113
  If you want to have custom error responses being delivered, just create your own `Api::Errors` that inherits from `JsonapiErrorsHandler::StandardError`
109
114
 
@@ -139,16 +144,17 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
139
144
 
140
145
  ## Contributing
141
146
 
142
- Bug reports and pull requests are welcome on GitHub at [https://github.com/driggl/jsonapi_errors_handler](https://github.com/driggl/jsonapi_errors_handler).
147
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/driggl/jsonapi_errors_handler>.
143
148
 
144
149
  **How to contribute:**
145
150
 
146
151
  1. Fork repository
147
152
  2. Install [Rubocop](https://github.com/rubocop-hq/rubocop) - make sure you run it before commiting changes
148
153
  3. Commit changes
149
- * Keep commits small and atomic
150
- * Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)
151
- * Keep commits imperative style
154
+ * Keep commits small and atomic
155
+ * Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)
156
+ * Keep commits imperative style
157
+
152
158
  4. Create Pull Request
153
159
 
154
160
  ## License
@@ -2,6 +2,7 @@
2
2
 
3
3
  lib = File.expand_path('lib', __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
5
6
  require 'jsonapi_errors_handler/version'
6
7
 
7
8
  Gem::Specification.new do |spec|
@@ -31,6 +32,8 @@ Gem::Specification.new do |spec|
31
32
  'public gem pushes.'
32
33
  end
33
34
 
35
+ spec.required_ruby_version = '>= 2.4.0'
36
+
34
37
  # Specify which files should be added to the gem when it is released.
35
38
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
36
39
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
@@ -40,7 +43,9 @@ Gem::Specification.new do |spec|
40
43
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
41
44
  spec.require_paths = ['lib']
42
45
 
43
- spec.add_development_dependency 'bundler', '~> 1.16'
44
- spec.add_development_dependency 'rake', '~> 10.0'
45
- spec.add_development_dependency 'rspec', '~> 3.0'
46
+ spec.add_development_dependency 'bundler'
47
+ spec.add_development_dependency 'rake'
48
+ spec.add_development_dependency 'rspec'
49
+ spec.add_development_dependency 'simplecov'
50
+ spec.add_development_dependency 'simplecov-cobertura'
46
51
  end
@@ -1,32 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'irb'
4
3
  require 'jsonapi_errors_handler/version'
5
4
  require 'jsonapi_errors_handler/configuration'
6
5
  require 'jsonapi_errors_handler/errors'
7
6
  require 'jsonapi_errors_handler/error_mapper'
8
7
  require 'jsonapi_errors_handler/error_serializer'
9
8
 
9
+ # Allows to handle ruby errors and return the serialized JSON:API output
10
+ #
10
11
  module JsonapiErrorsHandler
12
+ PREDEFINED_HASH = {
13
+ 'JsonapiErrorsHandler::Errors::Invalid' =>
14
+ 'JsonapiErrorsHandler::Errors::Invalid',
15
+ 'JsonapiErrorsHandler::Errors::Forbidden' =>
16
+ 'JsonapiErrorsHandler::Errors::Forbidden',
17
+ 'JsonapiErrorsHandler::Errors::NotFound' => '
18
+ JsonapiErrorsHandler::Errors::NotFound',
19
+ 'JsonapiErrorsHandler::Errors::Unauthorized' =>
20
+ 'JsonapiErrorsHandler::Errors::Unauthorized'
21
+ }.freeze
22
+
11
23
  def self.included(base)
12
24
  base.class_eval do
13
- ErrorMapper.map_errors!(
14
- 'JsonapiErrorsHandler::Errors::Invalid' => 'JsonapiErrorsHandler::Errors::Invalid',
15
- 'JsonapiErrorsHandler::Errors::Forbidden' => 'JsonapiErrorsHandler::Errors::Forbidden',
16
- 'JsonapiErrorsHandler::Errors::NotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
17
- 'JsonapiErrorsHandler::Errors::Unauthorized' => 'JsonapiErrorsHandler::Errors::Unauthorized'
18
- )
25
+ ErrorMapper.map_errors!(PREDEFINED_HASH)
19
26
  end
20
27
  end
21
28
 
22
29
  def handle_error(error)
30
+ log_error(error) if respond_to?(:log_error)
31
+ # Handle every error which inherits from
32
+ # JsonapiErrorsHandler::Errors::StandardError
33
+ #
34
+ if JsonapiErrorsHandler::ErrorMapper.mapped_error?(error.class.superclass.to_s)
35
+ return render_error(error)
36
+ end
37
+
23
38
  mapped = ErrorMapper.mapped_error(error)
24
39
  mapped ? render_error(mapped) : handle_unexpected_error(error)
25
40
  end
26
41
 
27
42
  def handle_unexpected_error(error)
28
- return raise error unless config.handle_unexpected?
29
- log_error(error) if respond_to?(:log_error)
43
+ config = JsonapiErrorsHandler::Configuration.instance
44
+ raise error unless config.handle_unexpected?
45
+
46
+ notify_handle_unexpected_error(error) if respond_to?(:notify_handle_unexpected_error)
47
+
30
48
  render_error(::JsonapiErrorsHandler::Errors::StandardError.new)
31
49
  end
32
50
 
@@ -35,11 +53,6 @@ module JsonapiErrorsHandler
35
53
  end
36
54
 
37
55
  def self.configure(&block)
38
- config = Configuration.instance
39
- config.configure(&block)
40
- end
41
-
42
- def config
43
- Configuration.instance
56
+ Configuration.instance.configure(&block)
44
57
  end
45
58
  end
@@ -3,15 +3,23 @@
3
3
  require 'singleton'
4
4
 
5
5
  module JsonapiErrorsHandler
6
+ # Configuration class allowing to set up the initial behavior for the gem
7
+ #
6
8
  class Configuration
7
9
  include Singleton
8
10
 
9
11
  attr_writer :handle_unexpected
10
12
 
13
+ # Allows to override the configuration options
14
+ # @param [Block] - list of options to be overwriten
15
+ #
11
16
  def configure
12
17
  yield(self) if block_given?
13
18
  end
14
19
 
20
+ # Checks if unexpected errors should be handled by the gem
21
+ # @return [Boolean]
22
+ #
15
23
  def handle_unexpected?
16
24
  @handle_unexpected
17
25
  end
@@ -1,14 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonapiErrorsHandler
4
+ # Maps any of the given error classes into the serializable errors
5
+ # from the predefined collection
6
+ #
4
7
  class ErrorMapper
5
- @@mapped_errors = {}
6
- def self.mapped_errors
7
- @@mapped_errors
8
+ @mapped_errors = {}
9
+
10
+ class << self
11
+ attr_reader :mapped_errors
8
12
  end
9
13
 
10
14
  def self.map_errors!(errors_hash = {})
11
- @@mapped_errors.merge!(errors_hash)
15
+ @mapped_errors.merge!(errors_hash)
12
16
  end
13
17
 
14
18
  def self.mapped_error?(error_klass)
@@ -22,11 +26,13 @@ module JsonapiErrorsHandler
22
26
  root_class = error_class.split('::').first
23
27
  mapped = mapped_errors[error_class] || mapped_errors[root_class]
24
28
  return unless mapped
29
+
25
30
  Object.const_get(mapped).new
26
31
  end
27
32
 
28
33
  def self.descendant_of_predefined?(error)
29
34
  return false if error.is_a?(Class)
35
+
30
36
  error.class < JsonapiErrorsHandler::Errors::StandardError
31
37
  end
32
38
  end
@@ -3,6 +3,8 @@
3
3
  require 'json'
4
4
 
5
5
  module JsonapiErrorsHandler
6
+ # Serializes the given error to match the JSON:API specification
7
+ #
6
8
  class ErrorSerializer
7
9
  def initialize(error)
8
10
  @error = error
@@ -20,7 +22,7 @@ module JsonapiErrorsHandler
20
22
 
21
23
  def serializable_hash
22
24
  {
23
- errors: [error.serializable_hash]
25
+ errors: [error.serializable_hash].flatten
24
26
  }
25
27
  end
26
28